The example of this article describes the usage of cache() for coherent operation of the thinkPHP5 framework database. Share it with everyone for your reference, the details are as follows:
Introduction
The cache system that comes with TP5 is a File type cache. That is, file cache. The storage address is: root directory\..\runtime\cache (the root directory refers to public).
Compared with redis, memcached of this caching system definitely has limitations, and there are differences in the complexity of automatic updates and cached data. But it is very helpful for some simple queries. For example, articles and other content are good to use.
cache can be used for select, find, value and column methods, as well as their derivative methods. After using the cache method, the database query operation will not be performed again within the cache validity period, but the data in the cache will be obtained directly. , please refer to the cache section for the types and settings of data cache.
Storage cache
1. Simple storage
//Query the news with id=10 in the news table and store it in the cache. Write true to read the configured cache by default. Time, db(): Assistant function
db('news')->cache(true)->find(10); //你也可以自定义时间,60秒表示 db('news')->cache(true,60)->find(10);
2. Specify the cache identifier
//The cache identifier can be understood as a key, that is, when you If you want to retrieve the token of a certain piece of data in the cache, store the token with id=15 into the cache and give the subscript as key
db('news')->cache('key')->find(15);
//When you want to retrieve the id =15 of this data
$data = \think\Cahce::get('key');
You can read this data anywhere, it is similar to session()
3. The cache method supports setting cache tags
db('news')->cache('key',60,'tagName')->find(15);
Update cache
Now there is a problem, when your project is running After a period of time, a lot of cache files will be generated, and there will be more and more files. The time wasted every time you request to find the cache file may be slower than directly querying the database. What should I do?
TP5 has a cache automatic update method. That is, when the same data is deleted or updated, the old cache file will be automatically deleted.
/Query the cache with id=328
$list = db('news')->cache(true)->find(328);
//Now for testing, you manually go to the database to modify the value of a field with id=328, and then make the first query request again and find that the modified field in the database has not changed. At this time, it is the read cache, and then
db('news')->update(['id'=>328,'title'=>'Test']);
//At this time you request again You will find that the obtained data has changed, and it is no longer in the cache for reading, because you have done an update operation and re-written it into the cache. Of course, the premise of the above mentioned situation is that the primary key query is used
The example of this article describes the usage of cache() for coherent operation of the thinkPHP5 framework database. For more related content, please pay attention to the PHP Chinese website.
Related recommendations:
How to obtain PHP Tutorial on the start date and end date of the first week of a certain year
php string reversal questions often encountered in interviews
The above is the detailed content of thinkPHP5 framework database coherent operation: cache() usage details. For more information, please follow other related articles on the PHP Chinese website!