Laravel 中的缓存,属性没了,类没了如何处理?
如果出现 属性没有了。使用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);
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
变量一下驼峰一下蛇形,看着不难受?
@Nick 哈哈,人家的方法就这样了。