Home > Article > PHP Framework > Redis cache configuration of thinkphp5
thinkphp5’s Redis cache configuration
thinkphp uses the cache class to provide cache function support and adopts a driver method , initialization is required before using the cache. Supported cache types include file, memcache, wincache, sqlite, redis and xcache, etc. The default is file type. To configure redis cache, you can configure redis alone or use multiple cache types at the same time. The configuration methods are as follows:
thinkphp uses the cache class to provide cache function support and adopts the driver method. Initialization is required before using the cache. Supported cache types include file, memcache, wincache, sqlite, redis and xcache, etc. The default is file type. To configure redis cache, you can configure redis alone or use multiple cache types at the same time. The configuration methods are as follows:
1. Configure only the redis cache, and modify the cache settings in the configuration file (app/config.php) as follows:
2. Configure multiple cache types, use the Cache type, the configuration method is as follows:
'cache' => [ // 使用复合缓存类型 'type' => 'complex', // 默认使用的缓存 'default' => [ // 驱动方式 'type' => 'File', // 缓存保存目录 'path' => CACHE_PATH, ], // 文件缓存 'file' => [ // 驱动方式 'type' => 'file', // 设置不同的缓存保存目录 'path' => RUNTIME_PATH . 'file/', ], // redis缓存 'redis' => [ // 驱动方式 'type' => 'redis', // 服务器地址 'host' => '192.168.1.100', ], ],
When using the cache 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 with the default cache ID is used. If you need to switch to other cache identification operations, 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, when querying an article, first query it from redis. If the information is not found, the result will be retrieved from the database and stored in redis.
Recommended tutorial: "TP5"
The above is the detailed content of Redis cache configuration of thinkphp5. For more information, please follow other related articles on the PHP Chinese website!