当前位置:网站首页>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/
边栏推荐
- 如何让join跑得更快?
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
- Common text processing tools
- Cookie and session comparison
- RecyclerView的数据刷新
- [untitled]
- Esp32 series column
- Coscon'22 community convening order is coming! Open the world, invite all communities to embrace open source and open a new world~
- Scrapy教程经典实战【新概念英语】
- Vscode编辑器ESP32头文件波浪线不跳转彻底解决
猜你喜欢
About how appium closes apps (resolved)
Analysis of DHCP dynamic host setting protocol
将数学公式在el-table里面展示出来
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
[Presto profile series] timeline use
Go language learning notes - structure
Practical example of propeller easydl: automatic scratch recognition of industrial parts
记一次 .NET 某新能源系统 线程疯涨 分析
Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
高端了8年,雅迪如今怎么样?
随机推荐
Summary of import, export, backup and recovery of mongodb
JNA learning notes 1: Concepts
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
存储过程的介绍与基本使用
Differences between MySQL storage engine MyISAM and InnoDB
How did Guotai Junan Securities open an account? Is it safe to open an account?
Pcap learning notes II: pcap4j source code Notes
ESP32构解工程添加组件
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
[untitled]
Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
OSI 七层模型
MongoDB内部的存储原理
线程池拒绝策略最佳实践
如何让join跑得更快?
详细介绍六种开源协议(程序员须知)
数字ic设计——SPI
Go语言学习笔记-结构体(Struct)
简单好用的代码规范
聊聊伪共享