How to ThinkPHP5 Framework cache queries

Source: Internet
Author: User
This article mainly introduces the THINKPHP5 framework cache query operation, combined with the instance form analysis of thinkPHP5 cache settings, use, query and other related operation skills, the need for friends can refer to the following

This example describes the THINKPHP5 framework cache query operation. Share to everyone for your reference, as follows:

Cache Settings and use:

1. Set the following in \application\config.php:

' Cache ' = = [  //using composite cache type  ' type ' = ' complex ',  //default cache  ' = ' = ' =/    /Drive mode    ' Type '  = = ' File ',   //!! In this setting the substitution cache mode    //Cache Save Directory    ' path '  = Cache_path,  ],  //File cache  ' files '  = [    / /drive Mode    ' type '  = ' file ',    //Set different cache to save directory    ' path '  = Runtime_path. ' file/',  ],  //Redis cache  ' Redis '  = [    //Drive mode    ' type '  = ' Redis ',    // Server address    ' host '    = ' 127.0.0.1 ',  ],],

2, the Controller

Use \think\cache;

3. Use 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 the data query in model, you need to use the DB Class)

Public Function Cacheselect () {  $_data = db (' table name ')->cache ()->find ();  Dump ($_data);  60s internal cache to extract data, no need to query from the database}

2, set the specified cache identity, make the query more efficient, more convenient to use:

Public Function Cacheselect () {  $result = db (' table name ')->cache (' Random character ')->where (' id ', ' < ', ten)->select ();} The CacheKey method is any method of the arbitrary controller Public Function CacheKey () {  //In other places directly invoke the found data, avoid querying again:  $data = \think\cache::get (' Random characters ');  Dump ($data);}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.