当前位置:网站首页>php——laravel缓存cache
php——laravel缓存cache
2022-07-07 11:10:00 【CaseyWei】
Laravel中的cache为我们提供了三种缓存机制。
Redis,memcache,以及框架的文件缓存。
这里主要看的是cache中的文件缓存。
一:访问多个缓存存储
使用 Cache 门面,你可以使用 store 方法访问不同的缓存存储器,传入 store 方法的键就是 cache 配置文件中 stores 配置数组里列出的相应的存储器:
$value = Cache::store('file')->get('foo');
Cache::store('redis')->put('bar', 'baz', 600); // 10分钟
二:从缓存中获取数据
1:获取数据并设置默认值
(1):正常取值
$value = Cache::get('key');
(2):如果不存在,附默认值
$value = Cache::get('key', 'default');
(3):使用闭包操作,附默认值
$value = Cache::get('key', function() {
return DB::table(...)->get();
});
2:检查缓存项是否存在
has 方法用于判断缓存项是否存在,如果值为 null 或 false 该方法会返回 false:
if (Cache::has('key')) {
//
}
3:数值增加/减少
increment 和 decrement 方法可用于调整缓存中的整型数值。这两个方法都可以接收第二个参数来指明缓存项数值增加和减少的数目:
Cache::increment('key');
Cache::increment('key', $amount);
Cache::decrement('key');
Cache::decrement('key', $amount);
4:获取 & 存储
有时候你可能想要获取缓存项,但如果请求的缓存项不存在时给它存储一个默认值。例如,你可能想要从缓存中获取所有用户,或者如果它们不存在的话,从数据库获取它们并将其添加到缓存中,你可以通过使用 Cache::remember 方法实现:
$value = Cache::remember('users', $seconds, function() {
return DB::table('users')->get();
});
如果缓存项不存在,传递给 remember 方法的闭包被执行并且将结果存放到缓存中。
你还可以使用 rememberForever 方法从缓存中获取数据或者将其永久存储起来:
$value = Cache::rememberForever('users', function() {
return DB::table('users')->get();
});
5:获取 & 删除
如果你需要从缓存中获取缓存项然后删除,你可以使用 pull 方法,和 get 方法一样,如果缓存项不存在的话返回 null:
$value = Cache::pull('key');
三:缓存中存储数据
1:获取存储数据
你可以使用 Cache 门面上的 put 方法在缓存中存储数据。当你在缓存中存储数据的时候,需要指定数据被缓存的时间(秒数):
Cache::put('key', 'value', $seconds);
如果没有传递缓存时间到 put 方法,则缓存项永久有效:
Cache::put('key', 'value');
除了传递缓存项失效时间,你还可以传递一个代表缓存项有效时间的 PHP Datetime 实例:
$expiresAt = Carbon::now()->addMinutes(10);
Cache::put('key', 'value', $expiresAt);
2:缓存不存在时存储数据
add 方法只会在缓存项不存在的情况下添加数据到缓存,如果数据被成功添加到缓存返回 true,否则,返回 false:
Cache::add('key', 'value', $seconds);
3:永久存储数据
forever 方法用于持久化存储数据到缓存,这些值必须通过 forget 方法手动从缓存中移除:
Cache::forever('key', 'value');
四:从缓存中移除数据
可以使用 Cache 门面上的 forget 方法从缓存中移除缓存项数据:
Cache::forget('key');
还可以通过设置缓存有效期为 0 或负数来移除缓存项:
Cache::put('key', 'value', 0);
Cache::put('key', 'value', -5);
如果要清除所有缓存,可以通过 flush 方法:
Cache::flush();
以上基本上就是laravel框架对文件缓存的操作方法
链接:https://www.jianshu.com/p/48d44d91ec93/
边栏推荐
- 飞桨EasyDL实操范例:工业零件划痕自动识别
- 服务器到服务器 (S2S) 事件 (Adjust)
- Sed of three swordsmen in text processing
- 滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
- About the problem of APP flash back after appium starts the app - (solved)
- 人均瑞数系列,瑞数 4 代 JS 逆向分析
- 免费手机号码归属地API查询接口
- The difference between cache and buffer
- Find ID value MySQL in string
- 如何让electorn打开的新窗口在window任务栏上面
猜你喜欢

MATLAB中polarscatter函数使用

- Oui. Migration entièrement automatisée de la Sous - base de données des tableaux d'effets sous net

3D content generation based on nerf

Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]

ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics

人均瑞数系列,瑞数 4 代 JS 逆向分析

Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~

高瓴投的澳斯康生物冲刺科创板:年营收4.5亿 丢掉与康希诺合作

飞桨EasyDL实操范例:工业零件划痕自动识别

Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
随机推荐
关于 appium 如何关闭 app (已解决)
《ASP.NET Core 6框架揭秘》样章[200页/5章]
明星企业普渡科技大裁员:曾募资超10亿 腾讯红杉是股东
DrawerLayout禁止侧滑显示
[crawler] avoid script detection when using selenium
HZOJ #240. Graphic printing IV
红杉中国完成新一期90亿美元基金募集
@What is the difference between resource and @autowired?
通过Keil如何查看MCU的RAM与ROM使用情况
SSM框架搭建的步骤
Session
[learn wechat from 0] [00] Course Overview
HZOJ #236. 递归实现组合型枚举
About the problem of APP flash back after appium starts the app - (solved)
About how appium closes apps (resolved)
Differences between MySQL storage engine MyISAM and InnoDB
测试下摘要
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
JNA学习笔记一:概念
[learn microservice from 0] [01] what is microservice