thinkphp using Memcache cache server

Source: Internet
Author: User
Tags delete cache

(1) The default caching method for thinkphp is file mode, and a lot of cache files are generated under/runtime/temp.

Server installed memcached and want to change into Memecache way

Add in conf/config.php

' Data_cache_type ' = ' Memcache ',

' Memcache_host ' = ' tcp://127.0.0.1:11211 ',

' Data_cache_time ' = ' 3600 ',

(2) thinkphp official download extension thinkphp_extend_3.1.2/extend/driver/cache/cachememcache.class.php save to Thinkphp/lib/driver/cache /cachememcache.class.php

(3) Test: S (' Test ', ' memcache '); $test = s (' Test '); echo $test;//output memcache test succeeded.

(4) memcached use test:

$test = S (' test '); if (! $test {     $test = ' cache information ';     S (' test ',$test, +);      Echo $test. ' -From the database ';} Else {     echo$test. ' -From memcached ';}

Attached: s function code

/** * Cache management * @param mixed $name cache name, if the array represents cache settings * @param mixed $value Cached values * @param mixed $options Cache parameters * @return Mixe D*/functionS$name,$value= ",$options=NULL) {    Static $cache= ' '; if(Is_array($options) &&Empty($cache)){        //simultaneous initialization of cache operations        $type=isset($options[' type '])?$options[' type ']: '; $cache= Cache::getinstance ($type,$options); }ElseIf(Is_array($name)) {//Cache Initialization        $type=isset($name[' type '])?$name[' type ']: '; $cache= Cache::getinstance ($type,$name); return $cache; }ElseIf(Empty($cache)) {//Automatic Initialization        $cache= Cache::getinstance (); }    if(' = = =$value){//Get Cache        return $cache->get ($name); }ElseIf(Is_null($value)) {//Delete Cache        return $cache->RM ($name); }Else{//Cache Data        if(Is_array($options)) {            $expire=isset($options[' expire '])?$options[' Expire ']:NULL; }Else{            $expire=Is_numeric($options)?$options:NULL; }        return $cache->set ($name,$value,$expire); }}

thinkphp using Memcache cache server

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.