ThinkPHP5 Framework Database Cache () usage

Source: Internet
Author: User
This paper mainly introduces the cache () usage of the THINKPHP5 framework database, and analyzes the application scenarios of cached caches in the thinkPHP5 and the operation techniques of setting, updating and deleting the buffers in the context of the case, which can be referenced by the friends. Hope to help everyone.

Introduced

The cache system that comes with the TP5 is a file-type cache. That is, file-type caching. The storage address is: root directory \.. \runtime\cache (root directory refers to public).

This caching system is somewhat different from the complexity of the redis,memcached, the automatic update and the cache data. But for some simple queries and so on is also very helpful. For example, articles such as the use of these content is still good.

The cache can be used for the Select, find, Value, and column methods, as well as its derived methods, which, after using the cache method, do not perform a database query operation again, but rather directly get the data in the cache, and refer to the cache section for the type and settings of the data cache.

Storage cache

1. Simple Storage


Query the news table in the id=10 store in the cache, write True the default read configuration in the buffer time, db (): Helper function db (' News ')->cache (True)->find (10);//You can also customize the time, 60 seconds means db (' News ')->cache (true,60)->find (10);

2. Specify the cache identity


The cache identifier can be understood as a key, that is, when you want to remove a token of a piece of data from the cache, the id=15 caches and sets the tag to Keydb (' News ')->cache (' key ')->find (15);//When you want to take out id= 15 of this data time $data = \think\cahce::get (' key ');

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

3. Cache method supports setting caching tags


DB (' News ')->cache (' key ', ' TagName ')->find (15);

Update cache

Now there is a problem, when your project has been running for a period of time will not produce a lot of cache files, more and more files, each request to find the cache file when the time wasted may be more than the direct query database slower. What do we do?

TP5 has a method for automatically updating the cache. The old cache files are deleted automatically when the data is deleted or updated.

//query id=328 save in cache$list = db (' News ')->cache (True)->find (328);// Now for testing, you manually go to the database to modify the value of a field of id=328, and then make the first query request again to find that the database modified field is not changed, this is the read cache, then the db (' News ')->update ([' ID ' =>328, ' Title ' + ' Test ');//This time you request again will find that the data obtained changes, this time is no longer a read cache, because you did the update operation, re-write into the cache, of course, the above-mentioned situation is the premise of using the primary key query 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.