ThinkPHP Function details: cache method

Source: Internet
Author: User
Tags delete cache
The cache method is a new cache management method added since version 3.0. Note: the cache method is incorporated into the original S method after version 3.1.2. Therefore, the cache method is no longer recommended. use the S method. The cache method is a new cache management method added since version 3.0.
Note: the cache method is incorporated into the original S method after version 3.1.2. Therefore, the cache method is no longer recommended. use the S method.
Cache is used to set, retrieve, and delete cache operations.
Usage Cache ($ name, $ value = '', $ options = null)
Parameters Name (required): If an array is input, it indicates cache initialization. if it is a string, it indicates cache assignment, acquisition, or deletion.
Value (optional): the cache Value to be set. If null is input, the cache is deleted. the default Value is a null string.
Options (optional): the cache parameter to be set. if it is a number, only the validity period is set.
Return value See details (return different values based on specific usage)
Cache initialization the cache method imports an array to indicate cache initialization. for example:
  1. Cache (array ('type' => 'xcache', 'prefix' => 'think', 'expire '=> 600 ));
The configuration parameters supported by the copy code are based on different caching methods (set by the type parameter). common cache parameters include:
Parameter name Description
Expire Cache validity period (in seconds)
Length Cache queue length
Prefix Cache ID prefix
Type Cache type
If the type parameter is not set, the file cache is used by default, currently, the cache method supports File, Apachenote, Apc, Eaccelerator, Memcache, Shmop, Sqlite, Db, Redis, Xcache, and WinCache, for details about cache parameters of different cache types, refer to dynamic cache.
After the cache initialization operation, a cache instantiation object is returned.
If you do not initialize the cache, the cache method will automatically initialize before you perform the cache operation (using the default system cache method and cache parameters). The default cache configuration parameters include:
  1. /* Data cache settings */
  2. 'Data _ CACHE_TIME '=> 0, // DATA cache validity period 0 indicates permanent cache
  3. 'Data _ CACHE_COMPRESS '=> false, // whether the DATA cache compresses the cache
  4. 'Data _ CACHE_CHECK '=> false, // whether the DATA cache verifies the cache
  5. 'Data _ cache_prefix' => '', // cache prefix
  6. 'Data _ CACHE_TYPE '=> 'File', // DATA cache type
  7. 'Data _ CACHE_PATH '=> TEMP_PATH, // cache path setting (only valid for File)
  8. 'Data _ cache_subdir' => false, // use the subdirectory cache (create a subdirectory based on the hash of the cache ID)
  9. 'Data _ PATH_LEVEL '=> 1, // sub-directory cache level
Copy the code and use the specific cache parameters. we will describe them in detail in the quick start: cache. After cache settings are initialized, you can perform cache operations,
  1. Cache ('name', 'value ');
The copied code uses the currently configured cache method and the name identifier to cache the value.
You can set the validity period of the cached data separately, for example:
  1. Cache ('name', 'value', 3600 );
Copy the code to cache data for one hour.
To ensure the security of the cache, we recommend that you determine the return value. if the cache setting fails, false is returned; otherwise, true is returned.
3.1.2 or later versions, you can directly set cache parameters, for example:
  1. Cache ('name', 'value', array ('type' => 'xcache', 'prefix' => 'think', 'expire '=> 600 ));
Copy code to get cache
  1. $ Value = cache ('name ');
If the copied code does not exist or has expired, false is returned; otherwise, the cache value is returned. Delete cache
  1. Cache ('name', NULL );
Copy the code to delete the cached data marked as name.


If you want to change the cache mode, you can initialize the cache again.
Or use the following method:
  1. $ Cache = cache (array ('type' => 'xcache', 'prefix' => 'think', 'expire '=> 600 ));
  2. $ Cache-> name = 'value'; // sets the cache.
  3. $ Value = $ cache-> name; // Get the cache
  4. Unset ($ cache-> name); // deletes the cache.
If you have set a cache prefix for the copied code, the corresponding cache operation only identifies the cache prefix and does not affect other caches.

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.