Analysis of cache of Usage of thinkPHP5 Framework Database Coherence Operation

  • 2021-09-04 23:42:34
  • OfStack

This article illustrates the use of cache () for the coherent operation of the thinkPHP5 framework database. Share it for your reference, as follows:

Introduction

The cache system in TP5 is File cache. That is, file cache. The storage address is: root\..\ runtime\ cache (root refers to public).

Compared with redis and memcached, this caching system definitely has limitations, and it is different in automatic update and complexity of cached data. However, it is very helpful for 1 simple query. For example, articles and other contents are good to use.

cache can be used for select, find, value and column methods and their derivative methods. After using cache method, the database query operation will not be carried out again within the validity period of the cache, but the data in the cache will be directly obtained. For the type and setting of data cache, please refer to the cache section.

Store cache

1. Simple storage


// Query news In the table id=10 The news of is stored in the cache Chinese and written true Default read configuration mid-cache time, db() Helper function 
db('news')->cache(true)->find(10);
// You can also customize the time, 60 Second representation 
db('news')->cache(true,60)->find(10);

2. Specify the cache identity


// A cache identity can be understood as a key, which is a token when you want to fetch a piece of data in the cache ,id=15 Deposit of cache And the subscript is given as key
db('news')->cache('key')->find(15);
// When you want to take out id=15 When this data of 
$data = \think\Cahce::get('key');

You can read this data anywhere, which is similar to session ()

3. The cache method supports setting cache labels


db('news')->cache('key',60,'tagName')->find(15);

Update cache

Now there is a problem in this way. When your project has been running for a period of time, it will produce a lot of cache files, and there are more and more files. Every time you request to find cache files, the time wasted may be slower than directly querying the database. What shall I do?

TP5 has an cache automatic update method. That is, when there is a deletion or update operation with the data, the old cache file will be automatically deleted.


// Query id=328 Deposit of cache
$list = db('news')->cache(true)->find(328);
// Now, under the test, you can manually modify the database id=328 The value of a field of the, and then proceed to the value of the 1 The second query request finds that the modified fields in the database have not changed, which is the read cache, and then 
db('news')->update(['id'=>328,'title'=>' Test ']);
// At this time, if you request again, you will find that the obtained data has changed   This is no longer a read cache, because you did an update operation and wrote it back into the cache. Of course, the above situation is based on the primary key query 

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: