当前位置:网站首页>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/
边栏推荐
- TPG x AIDU|AI领军人才招募计划进行中!
- 测试下摘要
- 认养一头牛冲刺A股:拟募资18.5亿 徐晓波持股近40%
- JS判断一个对象是否为空
- Leetcode skimming: binary tree 20 (search in binary search tree)
- 人均瑞数系列,瑞数 4 代 JS 逆向分析
- How to reset Google browser? Google Chrome restore default settings?
- Sed of three swordsmen in text processing
- regular expression
- 2022-07-07 Daily: Ian Goodfellow, the inventor of Gan, officially joined deepmind
猜你喜欢
[crawler] avoid script detection when using selenium
About the problem of APP flash back after appium starts the app - (solved)
HZOJ #240. 图形打印四
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
关于 appium 如何关闭 app (已解决)
Go语言学习笔记-结构体(Struct)
处理链中断后如何继续/子链出错removed from scheduling
Session
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
关于 appium 启动 app 后闪退的问题 - (已解决)
随机推荐
How to continue after handling chain interruption / sub chain error removed from scheduling
Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
DETR介绍
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
事务的七种传播行为
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
初学XML
如何让electorn打开的新窗口在window任务栏上面
Users, groups, and permissions
Layer pop-up layer closing problem
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
How to reset Google browser? Google Chrome restore default settings?
TPG x AIDU|AI领军人才招募计划进行中!
Unity 构建错误:当前上下文中不存在名称“EditorUtility”
Leetcode brush questions: binary tree 19 (merge binary tree)
[untitled]
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
leecode3. 无重复字符的最长子串