当前位置:网站首页>PHP - laravel cache
PHP - laravel cache
2022-07-07 13:23:00 【CaseyWei】
Laravel Medium cache Three caching mechanisms are provided for us .
Redis,memcache, And the file cache of the framework .
What we mainly look at here is cache File cache in .
One : Access multiple cache stores
Use Cache The facade , You can use store Method to access different cache memory , Pass in store The key of the method is cache In profile stores Configure the corresponding memory listed in the array :
$value = Cache::store('file')->get('foo');
Cache::store('redis')->put('bar', 'baz', 600); // 10 minute
Two : Get the data from the cache
1: Get the data and set the default value
(1): Normal value
$value = Cache::get('key');
(2): If it doesn't exist , With default value
$value = Cache::get('key', 'default');
(3): Use closure operations , With default value
$value = Cache::get('key', function() {
return DB::table(...)->get();
});
2: Check whether the cache item exists
has Method is used to determine whether the cache item exists , If the value is null or false This method returns false:
if (Cache::has('key')) {
//
}
3: The value increases / Reduce
increment and decrement Method can be used to adjust integer values in the cache . Both methods can receive a second parameter to indicate the number of cache item values increasing and decreasing :
Cache::increment('key');
Cache::increment('key', $amount);
Cache::decrement('key');
Cache::decrement('key', $amount);
4: obtain & Storage
Sometimes you may want to get cache entries , But if the requested cache item does not exist, store a default value for it . for example , You may want to get all users from the cache , Or if they don't exist , Get them from the database and add them to the cache , You can use Cache::remember Method realization :
$value = Cache::remember('users', $seconds, function() {
return DB::table('users')->get();
});
If the cache entry does not exist , Pass to remember The closure of the method is executed and the result is stored in the cache .
You can still use it rememberForever Method to get data from the cache or store it permanently :
$value = Cache::rememberForever('users', function() {
return DB::table('users')->get();
});
5: obtain & Delete
If you need to get cache entries from the cache and delete , You can use pull Method , and get The method is the same , If the cache item does not exist, return null:
$value = Cache::pull('key');
3、 ... and : Store data in cache
1: Getting stored data
You can use Cache On the facade put Method stores data in the cache . When you store data in the cache , You need to specify the time when the data is cached ( Number of seconds ):
Cache::put('key', 'value', $seconds);
If the cache time is not delivered put Method , Then the cache entry is permanently valid :
Cache::put('key', 'value');
In addition to passing cache item expiration time , You can also pass a that represents the effective time of the cache item PHP Datetime example :
$expiresAt = Carbon::now()->addMinutes(10);
Cache::put('key', 'value', $expiresAt);
2: Store data when cache does not exist
add Method will only add data to the cache if the cache item does not exist , Returns if the data is successfully added to the cache true, otherwise , return false:
Cache::add('key', 'value', $seconds);
3: Permanently store data
forever Method is used to persist stored data to the cache , These values must pass forget Method to manually remove from the cache :
Cache::forever('key', 'value');
Four : Remove data from cache
have access to Cache On the facade forget Method to remove cached item data from the cache :
Cache::forget('key');
You can also set the cache validity to 0 Or negative numbers to remove cache entries :
Cache::put('key', 'value', 0);
Cache::put('key', 'value', -5);
If you want to clear all caches , Can pass flush Method :
Cache::flush();
The above is basically laravel The operation method of the framework for file caching
link :https://www.jianshu.com/p/48d44d91ec93/
边栏推荐
- Go语言学习笔记-结构体(Struct)
- error LNK2019: 无法解析的外部符号
- JS function 返回多个值
- LED light of single chip microcomputer learning notes
- 单片机学习笔记之点亮led 灯
- 人均瑞数系列,瑞数 4 代 JS 逆向分析
- Common text processing tools
- How to make the new window opened by electorn on the window taskbar
- 服务器到服务器 (S2S) 事件 (Adjust)
- 【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
猜你喜欢
随机推荐
Some principles of mongodb optimization
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
Test next summary
[untitled]
User management summary of mongodb
Practical case: using MYCAT to realize read-write separation of MySQL
About the problem of APP flash back after appium starts the app - (solved)
Day26 IP query items
DrawerLayout禁止侧滑显示
如何让join跑得更快?
Scrapy教程经典实战【新概念英语】
Mongodb meets spark (for integration)
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
单片机原理期末复习笔记
Cinnamon Applet 入门
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
mysql 局域网内访问不到的问题
Read PG in data warehouse in one article_ stat
Cmu15445 (fall 2019) project 2 - hash table details