Laravel 中的快取,屬性沒了,類沒了如何處理?

kuibatian發表於2019-12-25

如果出現 屬性沒有了。使用fillmodel

如果出現類沒了,需要再次呼叫new 這個類,傳引數。

如下面程式碼:


    /** 
    * topic  
    * 載入列表頁資料自動判斷型別與分頁 
    * 此處方法為相容版本 擔心其他地方呼叫報錯
    * @access public 
    * @param mixed $arg1 列表頁資料型別 
    * @param int $arg2 頁碼 預設為空 
    * @date 2019-11-01
    * @author bobo<1576554465@qq.com>
    * @version     $1.1$ 
    * @since 1.0 
    * @return html 
    */ 
    public function topic($topicName,$page=1)
    {   
        $count = 10;
        $cache_simple = new BaseCacheSimple();
        $suffix_cache_name = '_'.$topicName;

        $cache_name = 'TOPIC_LIST';
        // 這裡存入採取 hash 欄位為page 
        $cache_simple->prepareCache( $cache_name, 2, $page, $suffix_cache_name )->getRedisWarp()->setDecodeArray(false);// true 得到陣列
        $cacheData = $cache_simple->readCache();
        // dd($cacheData);
        if ($cacheData)
        {
            $datas = collect();
            // 迴圈填充data 下的model
            foreach ( $cacheData->data as $k=>$v )
            {
                $model = app(Article::class);
                BaseCacheTrait::fillModel($model,(array)$v);
                $datas[$k] = $model;

            }
            $cacheData->data = $datas; 
            // 因為外圍 還有一個 分頁類,所以繼續new 分頁類
            $articles = new AcademyPaginator($cacheData->data, $cacheData->total, $cacheData->per_page, $cacheData->current_page,[
                'path' => $cacheData->path,
                'pageName' => 'page',
            ]);
            // 這裡是 專門追加的屬性,作為前端模板判斷
            $articles->clazz = 'page';
        }
        else
        {
            // 加上 $page 這裡* 50 是 因為我們需要拿50頁
            $articles = $this->paging($this->relations([$topicName], $count*50), $count, $page);
            if ( $articles && $articles->isNotEmpty() )
            {
                //寫快取
                $cache_simple->writeCache( $articles );
            }
        }

        // 加上page
        // $articles = $this->paging($this->relations([$topicName], $count*50), $count,$page);
        $urlPattern = $this->linkFactory->articleTopic($topicName);
        return $this->render("live-topic.article", $urlPattern, [
            "articles" => $articles,
            "topicName" => $topicName,
            "matchType" => $this->getMatchType($topicName),
            "tdk" => [
                "topic" => $this->findTdkTags($topicName),
            ],
        ],$articles);
    }

相關文章