Home  >  Article  >  Backend Development  >  Yii Framework Official Guide Supplement Series 29 - Caching: Data Caching

Yii Framework Official Guide Supplement Series 29 - Caching: Data Caching

黄舟
黄舟Original
2017-02-15 09:15:201083browse



Data caching stores some PHP variables in the cache and then retrieves them from the cache later. For this purpose, the base class CCache of caching components provides the two most commonly used methods: set() and get().

To store a variable $value in the cache, we choose a unique ID and call set() to store it:


Yii::app()->cache->set($id, $value);

The cached data will remain in the cache unless it is cleared due to some caching policy (e.g. cache space is full, old data is deleted). To change this behavior, we can provide an expiration parameter when calling set(), so that after a set period of time, the cached data will be cleared:


// 值$value 在缓存中最多保留30秒
Yii::app()->cache->set($id, $value, 30);

Later when we need to access this variable (in the same or a different web request), we can call get() with the ID to retrieve it from the cache. If false is returned, it means that this value is not available in the cache and we should regenerate it.


$value=Yii::app()->cache->get($id);
if($value===false)
{
    // 因为在缓存中没找到 $value ,重新生成它 ,
    // 并将它存入缓存以备以后使用:
    // Yii::app()->cache->set($id,$value);
}

When selecting an ID for the variable to be cached, make sure that this ID is consistent with all other cached variables in the application. Cached variables are unique. This ID does not need to be unique between different applications. The caching component is smart enough to differentiate between IDs in different applications.

Some cache memories, such as MemCache, APC, support retrieving multiple cache values ​​in batch mode. This reduces the overhead of retrieving cached data. Starting from version 1.0.8, Yii provides a new method named mget(). It can take advantage of this feature. If the underlying cache memory does not support this functionality, mget() can still simulate it.

To clear a cached value from the cache, call delete(); to clear all data in the cache, call flush(). Be careful when calling flush() as it will also clear caches in other applications.

Tips: Since CCache implements ArrayAccess, the cache component can also be used like an array. Here are a few examples:


##

$cache=Yii::app()->cache;
$cache['var1']=$value1;  // 相当于: $cache->set('var1',$value1);
$value2=$cache['var2'];  // 相当于: $value2=$cache->get('var2');

##Cache dependency

In addition to the expiration setting, cache Data may also become invalid due to changes in dependent conditions. For example, if we cache the contents of certain files and those files change, we should invalidate the cached data and read the latest contents from the file rather than from the cache.

We represent a dependency as an instance of CCacheDependency or its subclass. When calling set() we pass it in along with the data to be cached.


// 此值将在30秒后失效
// 也可能因依赖的文件发生了变化而更快失效
Yii::app()->cache->set($id, $value, 30, new CFileCacheDependency('FileName'));

Now if we get

$value

from the cache by calling get(), depend on The relationship will be checked and if it has changed, we will get a false value indicating that the data needs to be regenerated. The following is a brief description of the available cache dependencies:

    CFileCacheDependency: If the last modification time of the file changes, the dependency changes.
  • CDirectoryCacheDependency: If the files in the directory and its subdirectories change, the dependency changes.
  • CDbCacheDependency: If the query result of the specified SQL statement changes, the dependency changes.
  • CGlobalStateCacheDependency: If the specified global state changes, the dependency changes. Global state is a cross-request, cross-session variable in the application. It is defined through CApplication::setGlobalState().
  • CChainedCacheDependency: This dependency changes if any dependency in the chain changes.
  • CExpressionDependency: If the result of the specified PHP expression changes, the dependency changes. This class is available since version 1.0.4.
  • The above is the Yii Framework Official Guide Supplement Series 29 - Caching: the content of data caching. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn