Analysis of Cache Query Operation in ThinkPHP5 Framework
- 2021-10-13 06:54:57
- OfStack
This article illustrates the cache query operation of ThinkPHP5 framework. Share it for your reference, as follows:
Cache setup and use:
1. Set the following in\ application\ config.php:
'cache' => [
// Using compound cache types
'type' => 'complex',
// Cache used by default
'default' => [
// Driving mode
'type' => 'File', // ! ! Set the cache mode of substitution here
// Cache save directory
'path' => CACHE_PATH,
],
// File cache
'file' => [
// Driving mode
'type' => 'file',
// Set different cache storage directories
'path' => RUNTIME_PATH . 'file/',
],
// redis Cache
'redis' => [
// Driving mode
'type' => 'redis',
// Server address
'host' => '127.0.0.1',
],
],
2. In the controller
use \think\Cache;
3. Used in the controller
Cache::set('name', 'tom',3600);
Cache::get('name');
Cache query:
1. Simple cache query:
In any controller (if you want to complete data query in model, you need to use Db class)
public function cacheSelect()
{
$_data = db(' Table name ')->cache(60)->find();
dump($_data);
//60s Extract data from the internal cache without querying from the database
}
2. Set the specified cache identity to make the query more efficient and convenient to use:
public function cacheSelect()
{
$result = db(' Table name ')->cache(' Random character ')->where('id','<', 10)->select();
}
//cacheKey The method is any method of any controller
public function cacheKey(){
// Call the found data directly elsewhere to avoid querying again:
$data = \think\Cache::get(' Random character ');
dump($data);
}
For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.