Laravel中怎么使用Caching缓存数据

2023-06-28,

Laravel中怎么使用Caching缓存数据,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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

我查了一下文档,发现了有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 '对不起,只有管理员组才可进行此操作!';
}

关于Laravel中怎么使用Caching缓存数据问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注本站行业资讯频道了解更多相关知识。

《Laravel中怎么使用Caching缓存数据.doc》

下载本文的Word格式文档,以方便收藏与打印。