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:
- 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:
- /* Data cache settings */
- 'Data _ CACHE_TIME '=> 0, // DATA cache validity period 0 indicates permanent cache
- 'Data _ CACHE_COMPRESS '=> false, // whether the DATA cache compresses the cache
- 'Data _ CACHE_CHECK '=> false, // whether the DATA cache verifies the cache
- 'Data _ cache_prefix' => '', // cache prefix
- 'Data _ CACHE_TYPE '=> 'File', // DATA cache type
- 'Data _ CACHE_PATH '=> TEMP_PATH, // cache path setting (only valid for File)
- 'Data _ cache_subdir' => false, // use the subdirectory cache (create a subdirectory based on the hash of the cache ID)
- '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,
- 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:
- 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:
- Cache ('name', 'value', array ('type' => 'xcache', 'prefix' => 'think', 'expire '=> 600 ));
Copy code to get cache
- $ Value = cache ('name ');
If the copied code does not exist or has expired, false is returned; otherwise, the cache value is returned. Delete cache
- 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:
- $ Cache = cache (array ('type' => 'xcache', 'prefix' => 'think', 'expire '=> 600 ));
- $ Cache-> name = 'value'; // sets the cache.
- $ Value = $ cache-> name; // Get the cache
- 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.