The thinkphp uses the cache class to provide caching support, which is driven by the need to initialize before using the cache. Supported cache types include file, Memcache, Wincache, SQLite, Redis, and XCache, etc.
The file type is by default, and the Redis cache can be configured with a single Redis or multiple cache types at the same time. The configuration is as follows: First, only the Redis cache is configured, and the cache settings are modified in the Config file (app/config.php) as follows:
' Cache ' = [ //drive //' type ' = ' File ', //Cache Save Directory// ' path ' = = Cache_path, //cache prefix //' prefix ' + ', //cache validity 0 means permanent cache //' expire ' = + 0, ' redis ' = [ c16/>//Drive mode ' type ' = ' redis ',/ /server address ' host ' = ' 192.168.1.xxx ', // Redis server IP ],
Second, configure multiple cache types, using the same cache type, configured as follows:
' Cache ' = = [ //using composite cache type ' type ' = ' complex ' , //default cache used ' ' default ' = [ //Drive mode ' type ' = ' File ', //Cache Save Directory ' path ' = Cache_path, ], Files Cache ' file ' = = [ //Drive mode ' type ' = ' file ', //Set different cache to save directory ' path ' = = Runtime_path. ' file/', ], //Redis cache ' Redis ' + [ //Drive mode ' type ' = ' Redis ', //server address ' host ' = ' 192.168.1.xxx ', //redis server IP ],
When you use a cache-compliant type, you need to use the store method to switch the cache as needed. When using
Cache::set (' name ', ' value '); Cache::get (' name ');
, the cache configuration is used for the default cache identity. If you need to switch to a different cache identity operation, you can use:
Switch to file operation Cache::store (' file ')->set (' name ', ' value '); Cache::get (' name ');//Switch to Redis operation Cache::store (' Redis ')->set (' name ', ' value '); Cache::get (' name ');
For example, query an article first from the Redis query, if not found the information from the database to query the results, and stored in Redis.
$article =cache::store (' Redis ')->get (' artcicle ' $id), if (! $article) {$article = Db::name (' article ')->where (' Status ', 1)->cache (True)->find ($id); Cache::store (' Redis ')->set (' article '. $id, $article);}
ThinkPHP5 Configuring the Redis cache