thinkPHP query cache method




 For data queries that do not require high timeliness, we can use the query cache function to improve performance, and we do not need to use the cache method for caching and retrieval.

The query cache function supports all databases, and supports all cache methods and validity periods.

When using query cache, you only need to call the cache method of the Model class, for example:

$Model->cache(true)->where('status=1')->select();

If it is used cache(true) , a query cache with a unique identifier will be generated according to the current query conditions and other information while querying. If a key is specified, a query cache named key will be generated directly, for example:

$Model->cache('cache_name')->select();

The way you specify the key makes the query cache more efficient.

By default, the cache mode adopts the cache mode set by the DATA_CACHE_TYPE parameter (the system default value is File, which means that the file mode is used for cache ) .

$Model->cache(true,60,'xcache')->select();

Indicates that the cache mode of the current query cache is xcache, and the cache validity period is 60 seconds.

The same query, if the cache method is not used, will not get or generate any cache, even if the Cache method is called before.

If the key of the query cache is specified, the content of the query cache can be directly obtained externally through the S method , for example:

$value = S('cache_name');

In addition to the select method, the query cache also supports the find and getField methods, as well as their derivatives (including statistical query and dynamic query methods).

// cache query data for 60 seconds $Model->where($map)->cache('key',60)->find();

The cache mode and cache validity period can be selected according to the specific application.

Post a Comment

Previous Post Next Post