Data cache stores some PHP variables in the cache and then retrieves them from the cache. For this purpose, the basic class CCache of the cache component provides two common methods: set () and get (). Store a variable in the cache...
Data cache stores some PHP variables in the cache and then retrieves them from the cache. For this purpose, the basic class CCache of the cache component provides two common methods: set () and get ().
To store a variable in the cache$value, We select a unique ID and call set () to store it:
Yii::app()->cache->set($id, $value);
Cached data will remain in the cache unless it is cleared due to some cache policies (such as the cache space is full and old data is deleted. To change this behavior, we can provide an expiration parameter while calling set (), so that the cache data will be cleared after the set time period:
// Value $ value can be reserved in the cache for up to 30 seconds. Yii: app ()-> cache-> set ($ id, $ value, 30 );
Later, when we need to access this variable (in the same or different Web requests), we can call get () by ID to retrieve it from the cache. If the returned value is false, it indicates that the value is not available in the cache. we should generate it again.
$ Value = Yii: app ()-> cache-> get ($ id); if ($ value = false) {// because $ value is not found in the cache, generate it again, // and store it in the cache for future use: // Yii: app () -> cache-> set ($ id, $ value );}
When selecting an ID for the variable to be saved to the cache, make sure that this ID is unique to all other variables stored in the cache in the application. This ID does not need to be unique between different applications. The cache component is smart enough to distinguish IDs of different applications.
Some cache storages, such as MemCache and APC, support obtaining multiple cache values in batch mode. This reduces the overhead of obtaining cached data. From Version 1.0.8, Yii provides a new method named mget. It can use this function. If the underlying cache memory does not support this function, mget () can simulate it.
To clear a cache value from the cache, call delete (); to clear all the data in the cache, call flush (). Be careful when calling flush () because it will clear the cache in other applications at the same time.
Tip:Because CCache implementsArrayAccessThe cache component can also be used like an array. The following are examples:
$ Cache = Yii: app ()-> cache; $ cache ['var1'] = $ value1; // equivalent to: $ cache-> set ('var1 ', $ value1); $ value2 = $ cache ['var2']; // equivalent to: $ value2 = $ cache-> get ('var2 ');
Cache dependency
Besides the expiration settings, cached data may also become invalid due to changes in the dependency conditions. For example, if we cache the content of some files and these files change, we should invalidate the cached data and read the latest content from the file rather than from the cache.
We represent a dependency as an instance of CCacheDependency or its subclass. When set () is called, the data to be cached is passed in together with the data to be cached.
// This value will expire 30 seconds later // It may also become faster and become invalid due to changes in the dependent file. Yii: app ()-> cache-> set ($ id, $ value, 30, new CFileCacheDependency ('filename '));
Now, if we call get () to obtain$valueThe dependency will be checked. if the dependency changes, we will get a false value, indicating that the data needs to be regenerated.
The following is a brief description of available cache dependencies:
-
CFileCacheDependency: If the last modification time of the file changes, the dependency changes.
-
CDirectoryCacheDependency: If the directory and the files in its subdirectories change, the dependency changes.
-
CDbCacheDependency: if the query result of a specified SQL statement changes, the dependency changes.
-
CGlobalStateCacheDependency: if the specified global status changes, the dependency changes. The global status is a cross-request and cross-session variable in the application. It is defined by CApplication: setGlobalState.
-
CChainedCacheDependency: If any dependency in the chain changes, the dependency changes.
-
CExpressionDependency: if the result of the specified PHP expression changes, the dependency changes. This class is available from version 1.0.4.
The above is the Yii Framework official guide series 29-cache: data cache content. For more information, see PHP Chinese website (www.php1.cn )!