Yii is a very hot open source framework for PHP, and the support for caching is quite comprehensive.
The Yii cache can be used at different levels and in the environment: it can be used to cache individual data (Yii data cache). Caches a page fragment (fragment cache) generated by the view script. The highest level, which stores the entire page so that it can be read directly from the cache when needed.
This article describes the configuration and implementation of page cache Level 1: Yii's own cache inherits the CCache class, and there is no difference in usage, and the cache base class CCache provides two of the most common methods: Set () and get (). To store a variable $value in the cache, we select a unique ID and call set () to store it.
The cache base class CCache provides two of the most common methods: Set () and get (). To store a variable $value in the cache, we select a unique ID and call set () to store it:
Yii::app ()->cache->set ($id, $value);
The cached data remains in the cache until it is deleted due to some caching policies (such as cache space is full and the oldest data is deleted). To change this behavior, we can also add an expiration parameter when calling set (), so that the data is automatically purged from the cache over time.
Keep this value in cache for up to 30 seconds
Yii::app ()->cache->set ($id, $value, 30);
When we need to access the variable later (whether it's not the same Web request), we call Get () (the incoming ID) to get it from the cache. If the return value is false, it means that the cache is not available and requires us to regenerate it.
$value =yii::app ()->cache->get ($id);
if ($value ===false) {
Regenerate $value because it is not found in the cache
and cache it for the next time.
Yii::app ()->cache->set ($id, $value);
}
When selecting an ID for a variable to be cached, ensure that the ID is unique in the app. It is not necessary to ensure that the IDs are unique across applications because the cache component has enough intelligence to differentiate the cache IDs of different applications. To remove a cached value from the cache, call Delete (); To clear all caches, call flush (). Be very careful when you call Flush (), because it will also empty the cache for other applications. Tip: Because CCache implements the Arrayaccess interface, you can use the cache component like an array.
$cache =yii::app ()->cache;
$cache [' var1 ']= $value 1; Equivalent: $cache->set (' var1 ', $value 1);
$value 2= $cache [' var2 ']; Equivalent to: $value 2= $cache->get (' var2 ');
It is also easy to use these caches, as long as the server is supported, and then simply modify the configuration file to use
I. Use of Memcache
1. Edit the configuration file config/main.php add memcache configuration
Array
' Components ' =>array (
' Memcache ' =>array (
' Class ' = ' System.caching.CMemCache ',
' Servers ' =>array (
Array
' Host ' = ' server1 ',
' Port ' =>11211,
' Weight ' =>60,
),
Array
' Host ' = ' Server2 ',
' Port ' =>11211,
' Weight ' =>40,
),
),
),
),
)
2. Use in the framework
Yii::app ()->memcache->set ($key, $value, $expire);
Yii::app ()->memcache->get ($key);
Yii::app ()->memcache->deletevalue ($key);
Two. Use of Yii Database cache
1. Edit the configuration file config/main.php add Dbcache Configuration
return array (
.....)
' components ' =>array (
.....
' Dbcache ' =>array (
' class ' = ' system.caching.cdbcache ',
),
' db ' =>array (
' class ' = ' system.db.CDbConnection ',
' connectionString ' = ' sqlite:/ Wwwroot/blog/protected/data/blog.db ',
' schemacachingduration ' =>3600,
),
),
);
2. Use in the framework
Yii::app ()->dbcache->set ($key, $value, $expire);
Yii::app ()->dbcache->get ($key);
Three. Use of Yii file cache
1. Edit the configuration file config/main.php add Dbcache Configuration
Application components
' Components ' =>array (
' Filecache ' =>array (
' Class ' = ' System.caching.CFileCache ',
We use CFileCache to implement the cache, and the cache files are stored in the runtime folder.
' Directorylevel ' = ' 2 ',//directory depth of cache file
),
),
2. Use in the framework
Yii::app ()->filecache->set ($key, $value, $expire);
Yii::app ()->filecache->get ($key);
Four. APC use
1. Edit the configuration file config/main.php add Dbcache Configuration
' Components ' =>array (
' Class ' = ' System.caching.CApcCache ',
),
Yii is a very hot open source framework for PHP, and the support for caching is quite comprehensive.