当前位置:网站首页>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/
边栏推荐
- Test next summary
- OSI 七层模型
- 迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
- Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
- Write it down once Net a new energy system thread surge analysis
- Why can basic data types call methods in JS
- QQ medicine, Tencent ticket
- Cookie and session comparison
- 自定义线程池拒绝策略
- [dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
猜你喜欢
LED light of single chip microcomputer learning notes
PAcP learning note 1: programming with pcap
Smart cloud health listed: with a market value of HK $15billion, SIG Jingwei and Jingxin fund are shareholders
error LNK2019: 无法解析的外部符号
Go语言学习笔记-结构体(Struct)
1、深拷贝 2、call apply bind 3、for of for in 区别
人均瑞数系列,瑞数 4 代 JS 逆向分析
leecode3. 无重复字符的最长子串
滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
随机推荐
【Presto Profile系列】Timeline使用
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
error LNK2019: 无法解析的外部符号
[etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
[untitled]
Initialization script
工具箱之 IKVM.NET 项目新进展
【学习笔记】AGC010
LED light of single chip microcomputer learning notes
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
记一次 .NET 某新能源系统 线程疯涨 分析
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
数字ic设计——SPI
一文读懂数仓中的pg_stat
How did Guotai Junan Securities open an account? Is it safe to open an account?
MongoDB复制(副本集)总结
Analysis of DHCP dynamic host setting protocol
Practical case: using MYCAT to realize read-write separation of MySQL
[untitled]
Clion mingw64 Chinese garbled code