ThinkPHP5 Configuring the Redis cache

Source: Internet
Author: User
Tags redis server

The thinkphp uses the cache class to provide caching support, which is driven by the need to initialize before using the cache. Supported cache types include file, Memcache, Wincache, SQLite, Redis, and XCache, etc.

The file type is by default, and the Redis cache can be configured with a single Redis or multiple cache types at the same time. The configuration is as follows: First, only the Redis cache is configured, and the cache settings are modified in the Config file (app/config.php) as follows:
' Cache '                  = [    //drive    //' type '   = ' File ',    //Cache Save Directory//    ' path '   = = Cache_path,    //cache prefix    //' prefix ' + ',    //cache validity 0 means permanent cache    //' expire ' = + 0,    ' redis '   =  [ c16/>//Drive mode        ' type '   = ' redis ',/        /server address        ' host '   = ' 192.168.1.xxx ',  // Redis server IP    ],

  

Second, configure multiple cache types, using the same cache type, configured as follows:

' Cache ' = =  [    //using composite cache type    ' type ' = ' complex '  ,    //default cache used ' '    default '   =  [        //Drive mode        ' type '   = ' File ',        //Cache Save Directory        ' path '   = Cache_path,    ],    Files Cache    ' file '   = =  [        //Drive mode        ' type ' = ' file   ',        //Set different cache to save directory        ' path '   = = Runtime_path. ' file/',    ],    //Redis cache    ' Redis '   +  [        //Drive mode        ' type '   = ' Redis ',        //server address        ' host '   = ' 192.168.1.xxx ',  //redis server IP    ],

  

When you use a cache-compliant 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 is used for the default cache identity. If you need to switch to a different cache identity operation, 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, query an article first from the Redis query, if not found the information from the database to query the results, and stored in Redis.
$article =cache::store (' Redis ')->get (' artcicle ' $id), if (! $article) {$article = Db::name (' article ')->where ('  Status ', 1)->cache (True)->find ($id); Cache::store (' Redis ')->set (' article '. $id, $article);}

  

ThinkPHP5 Configuring the Redis cache

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.