The following yellow Word is the code I use:
Set cache name Zgg, content is Zongguagua, effective time 60 seconds
Cache (' Zgg ', "Zongguagua", 60);
Read cache
if (Cache (' Zgg ')) {
echo Cache (' Zgg ');
}else{
Echo ' has lapsed ';
}
Another way of writing
Set the cache type to file, valid for 60 seconds, there are a lot of cache types, such as what Xcacha, it seems to install plug-ins, now with no need
Cache (Array (' type ' = ' File ', ' expire ' =>60));
Cache (' Zgg ', ' Zongguagua ');
Read cache
if (Cache (' Zgg ')) {
echo Cache (' Zgg ');
}else{
Echo ' has lapsed ';
}
The following is a excerpt of the content
In the project, a reasonable use of the cache for performance is of great help. Thinkphp provides a convenient way to cache, including data caching, static caching, and query caching, including file mode, APC, Db, Memcache, Shmop, Sqlite, Redis, Dynamic Data cache types, including Eaccelerator and XCache, as well as customizable static cache rules, and provides a quick way to access operations.
Data caching
In thinkphp cache operations, in general, do not need to directly manipulate the cache class, because the system built-in cache operations are encapsulated, the 3.1 version of the recommended data caching method is the cache method, the basic usage is:
(3.1. The 2 version of the cache method has been incorporated into the S method, so the cache method is no longer recommended, use the S method directly, and is consistent with the cache method usage described below )
1 Cache initialization
- Cache(array(' type '= 'xcache ',' expire '));
Copy Code
The parameters that can be supported by the cache initialization differ depending on the caching method, and the commonly used parameters are:
Expire |
Cache expiration Time (seconds) |
Prefix |
Cache identity Prefixes |
Type |
Cache type |
The core version is only supported by the file cache, and other caching methods support the need to download a separate cache driver and put it under the extend/driver/cache/of the system directory, otherwise there will be an error message that does not support the cache type.
Some caching methods have their own special parameters, such as the memcache cache, and other parameters that need to be configured:
- The cache(array(' type ' = ' memcache ',' host ' = '192.168.1.10 ', ' port '=' 11211 ',' prefix '= 'think ',' expire '=60 ));
Copy Code
For global caching, we generally recommend adding prefix (cache prefix) parameters to differentiate between different applications to avoid confusion.
2 Cache Settings
- Cache(' A ',$value);
Copy Code
The data is cached according to the parameters at the time of the cache initialization, and parameters can be changed when the cache is set, for example:
- Cache(' A ',$value,+); Cache data 300 seconds
Copy Code
Even change the previous cache mode or more parameters:
- Cache(' A ',$value,array(' type '= ' file ',' expire '= >)); //cache data in file mode for 300 seconds
Copy Code
If you use the above array method to pass in the parameter when the cache is set, it will affect the subsequent cache access.
3 Cache Reads
- $value = cache(' a ');
Copy Code
The cache reads the values of the preceding cache settings, which are affected by the parameters passed in when the cache is initialized or 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
- Cache(' A ',null);
Copy Code
Delete the cache data that the cache identifies as name.
If you want to switch the cache mode, you can do the cache initialization again or use the following method:
- $cache = cache(array(' type ' = ' XCache ',' prefix '= ' think ') ,' expire '=+));
- $cache, name = ' value '; //Set cache
- $value = $cache, name; //Get cache
- unset($cache,name); //Delete cache
Copy Code
If you set a cache prefix, the corresponding cache operation will only be identified for the cache prefix and does not affect other caches.
The data cache can support the cache queue, simply to limit the number of caches, just specify the length parameter at initialization time:
- Cache(array(' type '= 'xcache ',' length '= ' =', ' expire ' ( =));
Copy Code
When the length parameter is set, only the last 100 cached data is cached.
Fast Cache
If you just want to cache some simple data in a file, and there is no concept of expiration, then the system also provides a quick caching method F can be used for faster operation.
Cache data quickly, saved by default in the Data_path directory
- F(' data ',$Data);
Copy Code
Cache data quickly and save to the specified directory
- F(' data ',$Data,temp_path);
Copy Code
Get Cached data
- $Data = F(' Data ');
Copy Code
Delete cached data
- F(' data ',NULL);
Copy Code
The F method supports automatic creation of cache subdirectories, caches data under the Data_path directory, and automatically creates if the user subdirectory does not exist:
- F(' User/data ',$Data);
Copy Code
3.1.2 Version start the F method supports the use of the wildcard bulk deletion feature, using the following:
- F(' user/* ',NULL);
Copy Code
Represents the deletion of Data_path. ' The data cache under the user/' directory.
The system built-in data field information cache is a fast caching mechanism.
Query cache
For data queries that don't require high timeliness, we can use the query caching feature to improve performance without having to cache and get it ourselves using caching methods.
The query caching feature supports all databases and supports all caching methods and expiration dates.
When using the query cache, you only need to call the model class's cache method, for example:
- $Model,cache(true),select();
Copy Code
If the cache (true) is used, the query cache is generated at the same time as the query is based on the current query SQL, which is cached by default in the Data_cache_type parameter setting (the system default is file-cached) and the cache validity period is data _cache_time the time that the parameter is set, you can also make a separate cache method and validity period for the query cache:
- $Model,cache(true,"XCache"),select();
Copy Code
Indicates that the current query cache is cached in XCache and the cache is valid for 60 seconds.
The same query, if the cache method is not used, does not fetch or generate any caches, even if the cache method was previously called.
The query cache is for internal invocation only, and if you want the query cache to be open to other program calls, you can specify a key for the query cache, for example:
- $Model, Cache(' cache_name '),select();
Copy Code
The contents of the query cache can be obtained externally through the S method.
- $value = S(' cache_name ');
Copy Code
In addition to the Select method, the query cache supports the find and GetField methods, as well as their derived methods (including statistical queries and dynamic query methods). The application can choose the cache mode and the cache expiration date as needed.
Static caching
To use the static caching feature, you need to turn on the html_cache_on parameter and set the static cache rule file with the Html_cache_rules configuration parameter.
Static rules are defined in the following way:
- ' html_cache_on '= =true, //Turn on static cache
- ' Html_file_suffix ' = ' . shtml ', //Set static cache suffix to. shtml
- ' html_cache_rules '= array(
- ' actionname (lowercase) ' => array ( ' static rules ' , ' static cache validity period ' , ' additional rules '  
- ' modulename (lowercase) ' => array ( ' static rules ' , ' static cache validity period ' , ' additional rules '  
- ' modulename (lowercase): actionname (lowercase) ' Span class= "PLN" > => array ( ' static rules ' , ' static cache expiration ' , ' additional rules '
- ' * ' = array(' static rules ', ' static cache expiration ', ' additional rules '),
- //... Static rules for more operations
- )
Copy Code
The root directory of a static cache file is under the path defined by Html_path, and only operations that have a static rule defined are statically cached.
The first is to define a global operation static rule, such as a static rule that defines all of the read operations:
- ' read ' +=array(' {ID} ', ' $')
Copy Code
where {ID} means take $_get[' id '] as a static cache file name, the second parameter represents cache for 60 seconds
The second is to define the global module static rules, for example, the static rules that define all the user modules are:
- ' User: '= =array(' User/{:action}_{id} ', ' + ')
Copy Code
where {: action} indicates the current operation name
The third is a static rule that defines the operation of a module, for example, we need to define the read operation of the blog module for static caching
- ' Blog:read '= =array(' {ID} ',0)
Copy Code
There are individual special rules, such as the definition of static rules for empty modules and empty operations, which can be used in the following way:
- ' Empty:index '= =array(' {: module}_{:action} ',0) //define static rules for empty modules
- ' user:_empty '= =array(' user/{:action} ',0) //static rules for defining empty operations
Copy Code
The fourth way is to define the global static cache rules, which are used in special cases, and the operation of any module is applicable, for example
- ' * '= =array(' {$_server. REQUEST_URI|MD5} '),
Copy Code
Caching based on the current URL
Static rules are used to define the names of static files to be generated, and static rules are defined to ensure that they do not conflict, and that the wording can include the following:
1. Use system variables including _get _request _server _session _cookie
Format:
{$_xxx|function}
For example:
- {$_get. Name}
- {$_server. Request_uri| MD5}
Copy Code
2. Use frame-specific variables
For example: {: App}, {: group}, {: module}, and {: action} represent the current project name, group name, module name, and action name, respectively.
3. Use _get variable
{Var|function} means {ID} is actually equivalent to {$_get.id}
4. Direct use of functions
{|function} for example:
- {| time}
Copy Code
5, support mixed definitions, for example, we can define a static rule as:
- ' {id},{name|md5} '
Copy Code
Characters outside of {} are treated as strings, and directories are created automatically if they contain "/".
For example, define the following static rule:
- {:module}/{:Action}_{id}
Copy Code
A subdirectory of the module name is created under the static directory, and then the operation name _id.shtml file is written.
The static valid time unit is seconds. If not defined, it gets the setting value of the configuration parameter html_cache_time, which is defined as 0 to indicate a permanent cache.
Additional rules are typically used to perform function operations on static rules, such as
- ' read ' +=array(' Think{id},{name} ', ' $ ', ' MD5 ')
Copy Code
The post-translation static rule is md5 (' Think '. $_get[' id '). ', '. $_get[' name ']);
Configuration parameters related to static cache include:
- Html_cache_on whether to turn on the static cache function
- the html_file_suffix static file suffix convention configures the value to be . HTML
- Html_cache_time default static cache validity period defaults to override in static rule definition
Copy Code
Summarize
To be good at using Thinkphp's caching capabilities, it is more important to differentiate between when and how the cache is more effective. Cache is not omnipotent, no cache is absolutely impossible ^_^
thinkphp3.1 Cache configuration Settings Read