Home >Backend Development >PHP Tutorial >How to operate cache query in ThinkPHP5 framework

How to operate cache query in ThinkPHP5 framework

不言
不言Original
2018-07-03 17:02:593242browse

This article mainly introduces the cache query operation of the ThinkPHP5 framework, and analyzes the setting, use, query and other related operation skills of thinkPHP5 cache in the form of examples. Friends in need can refer to the following

The examples of this article describe ThinkPHP5 The framework caches query operations. Share it with everyone for your reference, the details are as follows:

Cache settings and usage:

1. Set in \application\config.php As follows:

'cache' => [
  // 使用复合缓存类型
  'type' => 'complex',
  // 默认使用的缓存
  'default'  => [
    // 驱动方式
    'type'  => 'File',   //!!在这设置换人的缓存方式
    // 缓存保存目录
    'path'  => CACHE_PATH,
  ],
  // 文件缓存
  'file'  => [
    // 驱动方式
    'type'  => 'file',
    // 设置不同的缓存保存目录
    'path'  => RUNTIME_PATH . 'file/',
  ],
  // redis缓存
  'redis'  => [
    // 驱动方式
    'type'  => 'redis',
    // 服务器地址
    'host'    => '127.0.0.1',
  ],
],

2, in the controller

use \think\Cache;

3, in the controller Use

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 the model, you need to use the Db class)

public function cacheSelect()
{
  $_data = db('表名')->cache(60)->find();
  dump($_data);
  //60s内在缓存里提取数据,不必再从数据库查询
}

2. Set the specified cache identifier , making the query more efficient and easier to use:

public function cacheSelect()
{
  $result = db(&#39;表名&#39;)->cache(&#39;随意字符&#39;)->where(&#39;id&#39;,&#39;<&#39;, 10)->select();
}
//cacheKey方法为任意控制器的任意方法
public function cacheKey(){
  //在其他地方直接调用查出来的数据,避免再次查询:
  $data = \think\Cache::get(&#39;随意字符&#39;);
  dump($data);
}

The above is the entire content of this article, I hope it will be useful for everyone’s learning Help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Comprehensive analysis of the source code of thinkphp3.2.0 setInc method

##thinkPHP2.1 custom tag library Introduction to the import method

Introduction to the callback method of thinkPHP framework docking Alipay instant account interface

The above is the detailed content of How to operate cache query in ThinkPHP5 framework. For more information, please follow other related articles on the PHP Chinese website!

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