This article mainly introduces the example code of ThinkPHP to implement static caching and dynamic caching, which has certain reference value. Interested friends can refer to it
Static Cache
To use the static caching function, you need to enable the HTML_CACHE_ON parameter and set the static caching rule file using the HTML_CACHE_RULES configuration parameter.
Define static rules
1 2 3 4 5 6 7 8 9 |
|
The root directory of the static cache file is under the path defined by HTML_PATH, and only operations that define static rules will be statically cached. And static cache supports different storage types. Static caching only works under GET requests.
Static address
Global operation static rules
1 |
|
Definition of global controller static rules
1 |
|
Definition Static rules for the operation of a certain controller //Define the read operation of the Blog controller for static caching
1 |
|
Define global static caching rules
1 |
|
Static rules
Static rules are used to define the names of static files to be generated. The definition of static rules must ensure that there will be no conflicts.
Use system variables
1 2 3 |
|
Use framework-defined variables
1 |
|
Use _GET variables
1 |
|
Use functions directly
1 |
|
Mixed definition
1 2 |
|
Dynamic cache
[S method data cache]
Cache initialization
1 |
|
The system is currently Supported cache types include: Apachenote, Apc, Db, Eaccelerator, File, Memcache, Redis, Shmop, Sqlite, Wincache and Xcache. If the S method does not pass in the type parameter for initialization, the DATA_CACHE_TYPE parameter value set in the configuration file is read as the default type. In the same way, if the prefix parameter is not passed in, the DATA_CACHE_PREFIX parameter value of the configuration file will be read. If the expire parameter is not passed in, the DATA_CACHE_TIME configuration value will be read as the default.
For global caching, we generally recommend adding the prefix (cache prefix) parameter to distinguish different applications to avoid confusion.
Set up the cache
The data cache can support cache queues. Simply put, you can limit the number of caches. You only need to specify the length parameter during initialization.
1 2 3 4 |
|
Read cache
1 |
|
Delete cache
1 |
|
Note: When using each cache, ThinkPHP needs to load the corresponding driver file and set the corresponding configuration.
Usage Example
1 2 3 4 5 6 7 8 9 10 11 |
|
[Quick Cache]
The system also provides a fast cache method F for faster operations, however, The F method has no validity period. The F method can support different storage types. If it is a file type, it is saved under the DATA_PATH directory by default.
Fast cache Data
1 2 3 |
|
Get cached data
1 |
|
Delete cached data
1 |
|
[Query cache]
The query cache function supports all databases, and supports all caching methods and validity periods.
When using query cache, you only need to call the cache method of the Model class.
1 |
|
If cache(true) is used, a query cache with a unique identifier will be generated based on the current query conditions and other information during the query. If a key is specified, the name of the key will be generated directly. query cache.
1 |
|
By default, the cache method uses the cache method set by the DATA_CACHE_TYPE parameter (the system default value is File, which means file cache is used). The cache validity period is the time set by the DATA_CACHE_TIME parameter. The cache method of the query cache can also be specified separately. and validity period.
1 |
|
If the key of the query cache is specified, the content of the query cache can be directly obtained externally through the S method.
1 |
|
In addition to the select method, the query cache also supports the find and getField methods, as well as their derived methods (including statistical query and dynamic query methods).
1 |
|
This article mainly comes from the official documentation. If you have any questions, please refer to the ThinkPHP3.2 official documentation-caching
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
ThinkPHP implements one-click cache clearing method
Summary of several methods of PHP clearing cache
The above is the detailed content of Analysis on ThinkPHP's implementation of static caching and dynamic caching. For more information, please follow other related articles on the PHP Chinese website!