Thinkphp 6.0 cache function

In this lesson, let's learn about the cache function provided by the system to realize the writing and reading of the cache.

one. cache function

1. The system has built-in many types of caches, except for File , others need to be combined with related products;
2. We mainly demonstrate File text caching here, and others need to learn related products;
3. The configuration file cache.php is used for cache configuration, which is generated in the runtime/cache directory by default;
4. The ::set() method can set a cache, and the third parameter is the expiration time;
Cache::set('user', 'Mr.Lee');
Cache::set('user', 'Mr.Lee', 3600);

5. The ::has() method determines whether the cache exists and returns a Boolean value;
Cache::has('user');

6. The ::get() method obtains the corresponding data from the cache, and returns null if there is no data ;
Cache::get('user');

7. ::inc() and ::dec() implement the auto-increment and auto-decrement operations of cached data;
Cache::inc('num');
Cache::inc('price', 3);
Cache::dec('num');
Cache::dec('price', 3);

8. ::push() implements the function of appending cached array data;
Cache::set('arr', [1,2,3]);
Cache::push('arr', 4);

9. The ::delete() method can delete the specified cache file;
Cache::delete('user');

10. The ::pull() method first obtains the cache value, then deletes the cache, and returns null if there is no data ;
Cache::pull('user');

11. The ::remember() method, if the data does not exist, writes the data, which can rely on injection;
Cache::remember('start_time', time());
Cache::remember('start_time', function (Request $request) {})

12. The ::clear() method can clear all caches;
Cache::clear();

13. The ::tag() tag can classify multiple caches into tags, which is convenient for unified management, such as clearing;
Cache::tag('tag')->set('user', 'Mr.Lee');
Cache::tag('tag')->set('age', 20);
Cache::tag('tag')->clear();

14. Use of helper functions: cache() ;
//设置缓存
cache('user', 'Mr.Lee', 3600);
//输出缓存
echo cache('user');
//删除指定缓存
cache('user', null);

Guess you like

Origin blog.csdn.net/qq_34820433/article/details/130035992