Laravel Cache 缓存使用

导入:use Cache;

  Cache::put('key', 'value', $minutes); 添加一个缓存

  Cache 门面的 get 方法用于从缓存中获取缓存项,如果缓存项不存在,返回 null。如果需要的话你可以传递第二个参数到 get 方法指定缓存项不存在时返回的自定义默认值:

  Cache::get('key');  

  Cache::get('key', 'default');

检查缓存项是否存在

  has 方法用于判断缓存项是否存在:

  if (Cache::has('key')) {
      //
  }
数值增加/减少

incrementdecrement 方法可用于调整缓存中的整型数值。这两个方法都可以接收第二个参数来指明缓存项数值增加和减少的数目:

  Cache::increment('key');
  Cache::increment('key', $amount);
  
  Cache::decrement('key');
  Cache::decrement('key', $amount);

获取并删除

如果你需要从缓存中获取缓存项然后删除,你可以使用 pull 方法,和 get 方法一样,如果缓存项不存在的话返回null:

  $value = Cache::pull('key');


链接:https://laravelacademy.org/post/3128.html
 

猜你喜欢

转载自www.cnblogs.com/gjh99/p/10896035.html