当前位置:网站首页>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/
边栏推荐
- COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
- About the problem of APP flash back after appium starts the app - (solved)
- 免费手机号码归属地API查询接口
- About how appium closes apps (resolved)
- Session
- 明星企业普渡科技大裁员:曾募资超10亿 腾讯红杉是股东
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
- 2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
- .Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
- 达晨与小米投的凌云光上市:市值153亿 为机器植入眼睛和大脑
猜你喜欢

10 张图打开 CPU 缓存一致性的大门

2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units

Image pixel read / write operation

Sed of three swordsmen in text processing

详细介绍六种开源协议(程序员须知)

MATLAB中polarscatter函数使用

Sequoia China completed the new phase of $9billion fund raising

“新红旗杯”桌面应用创意大赛2022

Cookie

COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
随机推荐
DHCP 动态主机设置协议 分析
The difference between cache and buffer
Practical case: using MYCAT to realize read-write separation of MySQL
SSM框架搭建的步骤
Unity build error: the name "editorutility" does not exist in the current context
“新红旗杯”桌面应用创意大赛2022
Ogre入门尝鲜
[untitled]
通过Keil如何查看MCU的RAM与ROM使用情况
为租客提供帮助
Shortcut key of Bash
Users, groups, and permissions
HZOJ #236. Recursive implementation of combinatorial enumeration
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
PACP学习笔记一:使用 PCAP 编程
File operation command
[untitled]
详解ThinkPHP支持的URL模式有四种普通模式、PATHINFO、REWRITE和兼容模式
滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]