legend3---laravel缓存操作基本模板

legend3---laravel缓存操作基本模板

一、总结

一句话总结:

将存储缓存的操作弄出来,这样一些修改的位置可以强制刷新缓存(比如课程评论数量,在用户评论课程的时候就可以强制刷新)

1、缓存数据的时候,缓存的数据的结果不要是null?

因为我们判断缓存是否存在的时候判断的是这个缓存的结果是不是null,推荐缓存数组

if($lesson_chapters!==null)

二、laravel缓存操作基本模板

博客对应课程的视频位置:

将存储缓存的操作弄出来,这样一些修改的位置可以强制刷新缓存(比如课程评论数量,在用户评论课程的时候就可以强制刷新)

 1 <?php
 2 
 3 namespace AppModelCacheHomeLesson;
 4 
 5 use AppModelAdminCommentLesson;
 6 use IlluminateDatabaseEloquentModel;
 7 use Cache;
 8 
 9 class LessonCommentNum extends Model
10 {
11     //1、缓存课程评论数量
12     public static function cache_lessonCommentNum($lesson_id){
13         $cache_str='Lesson_commentNum_'.$lesson_id;
14         $lesson_comment_num = Cache::get($cache_str);
15 
16         if($lesson_comment_num!==null){
17             return $lesson_comment_num;
18         }else{
19             return self::update_cache_lessonCommentNum($lesson_id);
20         }
21     }
22 
23     //2、更新缓存中的课程评论数量
24     public static function update_cache_lessonCommentNum($lesson_id){
25         $cache_str='Lesson_commentNum_'.$lesson_id;
26         $lesson_comment_num=CommentLesson::where('cl_l_id',$lesson_id)->count();
27         Cache::put($cache_str, $lesson_comment_num, 6*60);
28         return $lesson_comment_num;
29     }
30 }
 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/12638700.html