Home Backend Development PHP Tutorial ThinkPHP5 framework cache query operation

ThinkPHP5 framework cache query operation

May 31, 2018 pm 04:16 PM
php thinkphp5 operate

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',
  ],
],
Copy after login

2, in the controller

use \think\Cache;
Copy after login

3, in the controller Use

Cache::set('name', 'tom',3600);
Cache::get('name');
Copy after login

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内在缓存里提取数据,不必再从数据库查询
}
Copy after login

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

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

The above is the entire content of this article, thank you for reading. Please pay attention to the PHP Chinese website for more information!

Related recommendations:

thinkPHP5 framework database coherent operation: cache() usage details

##thinkPHP5 framework detailed steps to implement paging query

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

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles