Laravel中怎么使用Caching缓存数据
创始人
2024-12-10 18:02:15
0次

一. 查阅文档,找到能帮我实现需求的模块

我查了一下文档,发现了有Caching这样一个模块,顾名思义,就是缓存了,那它能否帮到我呢,看看先:

1. http://laravel.com/docs/cache/config  这里是laravel的Caching模块的实现

2. 文档中有如下描述:

The Basics     Imagine your application displays the ten most popular songs as voted on by your users. Do you really need to look up these ten songs every time someone visits your site? What if you could store them for 10 minutes, or even an hour, allowing you to dramatically speed up your application? Laravel's caching makes it simple.

我简单理解为:

假设你的应用展示了用户投票最多的10首流行歌曲,你真的需要在每个人访问你的网站的时候都去查一遍这10首歌吗?如果你想按10分钟或者是一小时的频率来缓存查询结果来加速你的应用,Laravel 的 caching缓存模块能将使工作变得异常简单.

嗯,从这段话,我已经了解到这完全符合我现在的需求了,接下来我只需要找到对应的使用方法和API,一步一步来就行了.

二.  学习相应API等

1. 还是上面文档,里面接着向下看,有如下描述:

By default, Laravel is configured to use the file system cache driver. It's ready to go out of the box with no configuration. The file system driver stores cached items as files in the cache directory. If you're satisfied with this driver, no other configuration is required. You're ready to start using it.

我简单理解为:

默认情况下,Laravel使用文件系统作为缓存的驱动, 这是不需配置就可使用的, 文件系统驱动会将缓存的数据存入缓存目录下的文件里面去, 如果你觉得合适的话不需要做任何其他的配置直接开始用就行了.

当然了, 这也是符合我的想法的, 其实我就是想把页面缓存成静态页文件, 用户再次访问时直接输出缓存的静态页就ok了, 如果需要更高级的需求, 还可以使用其他的驱动,有数据库驱动, memcached, redis驱动等, 很好很强大!

2. 接下来查看用例,找到使用方法

用例文档在这: http://laravel.com/docs/cache/usage

可以看出, 里面有 get, put, forever, remember, has, forget 等方法,这些方法使用也是基本上能 "望文生义" 就能搞定的,呵呵

具体使用方法文档里面已经说的够详细, 使用方法一目了然我就不细说了, 只在代码里面说吧

三. 具体实现

1. 我首页之前的代码

class Home_Controller extends Base_Controller {
 public function get_index() {
  $posts = Post::with('user')
     ->join('users', 'users.id', '=', 'posts.post_author')
      -> order_by('posts.created_at', 'desc')
       ->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body'));
  $data = array();
  foreach($posts as $p){
   $data[] = array(
    'id'  => $p -> id,
    'support' => $p -> support,
    'against' => $p -> against,
    'username'=> $p -> username,
    'post_author' => $p -> post_author,
    'post_title' => $p -> post_title,
    'post_body' => $p -> post_body
   );
  }
  return View::make('home.index')
    -> with('posts', $data);
 }
}

这是我首页的controller,作用只有一个, 就是从博文表里面取得所有博文, 然后输出, 每次有人访问, 都要查表, 如果没有发表新的博文, 也要查表, 的确有很多不必要的开销

2.  下面是我改装之后的代码:

class Home_Controller extends Base_Controller {
 public function get_index() {
  // 添加静态缓存支持
  // 如果不存在静态页缓存就立即缓存
  if ( !Cache::has('staticPageCache_home') ) {
   $data = array();
   $posts = Post::with('user')
      ->join('users', 'users.id', '=', 'posts.post_author')
       -> order_by('posts.created_at', 'desc')
        ->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body'));
   foreach($posts as $p){
    $data[] = array(
     'id'  => $p -> id,
     'support' => $p -> support,
     'against' => $p -> against,
     'username'=> $p -> username,
     'post_author' => $p -> post_author,
     'post_title' => $p -> post_title,
     'post_body' => $p -> post_body
    );
   }
   $res = View::make('home.index')
    -> with('posts', $data);
   Cache::forever('staticPageCache_home', $res);
  }
  // 返回缓存的数据
  return Cache::get('staticPageCache_home');
 }
}

这里我用到了三个api

1). Cache::has ,这个判断是说如果当前不存在 staticPageCache_home 这个名字的缓存, 就立即去取数据

2). Cache::forever,  这个从用例文档里面可知是"永久缓存"的意思, 因为我一般都是很勤劳的,如果发表了博文,自己再去后台立即刷新一下缓存就好了, 所以不需要设置过期啊失效时间之类的, 当然这个是要按各自的具体需求来的

3). Cache::get , 这句是从缓存里面取出 staticPageCache_home 这个名字的缓存, 然后作为响应内容返回

嗯, 就这么简单, 呵呵, 一个基本的缓存功能就完成了, laravel的确是不错地!

3. 为后台添加刷新缓存功能

还是贴代码吧, 不过也很简单:

// 刷新首页缓存(暂时只支持首页)
public function get_refreshcache() {
 /*
  @var $GID admin组id
 */
 $GID = 1;
 if ( Auth::user() -> gid === 1 ) {
  $data = array();
  $posts = Post::with('user')
    ->join('users', 'users.id', '=', 'posts.post_author')
    -> order_by('posts.created_at', 'desc')
    ->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body'));
  foreach($posts as $p){
   $data[] = array(
    'id'  => $p -> id,
    'support' => $p -> support,
    'against' => $p -> against,
    'username'=> $p -> username,
    'post_author' => $p -> post_author,
    'post_title' => $p -> post_title,
    'post_body' => $p -> post_body
   );
  }
  $res = View::make('home.index')
    -> with('posts', $data);
  Cache::forever('staticPageCache_home', $res);
  return '刷新首页缓存成功!';
 }
 return '对不起,只有管理员组才可进行此操作!';
}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...