thinkphp5函数中使用数据缓存
//获取文章分类和文章的二维数组函数
function get_article_category_and_article_list($merchantkeynum)
{
//设置数据缓存wlcache
$cache_level = config('cache')['cache_level']; //0关闭,1开启
// 使用缓存模式
$cacheKey = "client_merchant_article_category_" . $merchantkeynum;
$catcheTag = $merchantkeynum;
$result = Cache::store('redis')->get($cacheKey);
if (empty($result) || $cache_level=='0') {
// 查询文章分类
$categories = Db::table('client_merchant_article_category')
->where(['merchantkeynum' => $merchantkeynum, 'status' => 1])
->order('o asc')
->select();
// 初始化结果数组
$result = [];
// 遍历每个分类,查询该分类下的文章
foreach ($categories as $category) {
$categoryId = $category['id'];
$articles = Db::table('client_merchant_article')
->where(['merchantkeynum' => $merchantkeynum, 'cat_id' => $categoryId, 'status' => 1])
->order('o asc')
->select();
// 将分类和文章添加到结果数组中
$result[] = [
'category' => $category,
'articles' => $articles
];
}
Cache::store('redis')->tag($catcheTag)->set($cacheKey, $result, config("cache")['expire']);
}
return $result;
}
单独某个sql缓存: $plat_system_set = Db::table('plat_system_set')->where("id='1' ")->cache("plat_system_set_key", config('cache')['expire'], "plat")->find();
版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。
本文链接:thinkphp5函数中使用数据缓存 - https://wlphp.com/?post=472