Thinkphp 3.1 QuickStart: Caching

Source: Internet
Author: User
Keywords Caching QuickStart thinkphp
Tags access array cache cached caching customizable data dynamic data

In the project, a reasonable use of caching is of great help to performance. Thinkphp provides a convenient way to cache, including data caching, static caching, and query caching, and support including file mode, APC, Db, Memcache, Shmop, Sqlite, Redis, Dynamic Data cache types, including eaccelerator and XCache, and 17813.html "> Customizable static caching rules, and provide a quick way to access operations."

Data caching

Cache operations in thinkphp, generally do not need to directly manipulate the cache class, because the system built-in cache operation is encapsulated, the 3.1 version of the recommended data caching method is the cache method, the basic use is:

1 Cache initialization

Cache (Array (' type ' => ' xcache ', ' expire ' =>60); parameters that can be supported by caching initialization vary according to the caching method, and the commonly used parameters are:

Expire cache expiration (time in seconds) prefix cache identifier prefix type cache type

Core version only http://www.aliyun.com/zixun/aggregation/19352.html ">file caching support, and other caching methods to support the need to download a separate cache driver, and put into the system directory extend/ driver/cache/below, error prompts that do not support cache types appear. Some caching methods have their own special parameters, such as memcache caching, and other parameters that need to be configured:

Cache (' type ' => ' memcache ', ' Host ' => ' 192.168.1.10 ', ' Port ' => ' 11211 ', ' prefix ' => ', ' ", ' expire ' = >60); For global caching, we generally recommend adding prefix (cache prefix) parameters to distinguish between different applications to avoid confusion.

2 Cache Settings

Cache (' A ', $value), caching data according to cached initialization parameters, or changing parameters when caching settings, such as cache (' A ', $value, 300);/cached data 300 seconds or even change previous cache mode or more parameters: Cache (' A ', $value, Array (' type ' => ' file ', ' expire ' =>300);/file caching of data 300 seconds if you use the array to pass the parameter in the cache setting, Can affect subsequent cache access.

3 Cache Reads

$value = Cache (' a '); caching reads the value of the previous cache setting, which is affected by the parameters passed in when the cache is initialized or when the cache is set.
Returns False if the cache identity does not exist or has expired, otherwise the cached value is returned.

4 Cache deletion

Deletes cached data that is identified as name by the cache.

If you want to switch caching, you can either cache initialization again or use the following method: If you set the cache prefix, the corresponding cache operation is only for the cache prefix should be identified, does not affect the other cache.
The data cache can support caching queues, simply by limiting the number of caches and specifying the length parameter when initializing:

Cache (' type ' => ' xcache ', ' length ' =>100, ' expire ' =>60); When the length parameter is set, the system caches only the last 100 cached data.

Related Article

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.