Cache MEMCACHE php calls

In the project, when large amount of access is involved, reasonable use of cache can reduce the pressure of database and improve the user experience. That is, under the premise of non real-time demand, for a short period of time (several seconds), the data used to display is obtained from the cache, instead of directly reading the database, which can effectively reduce the reading pressure of the database. Here is a record of the use of memcache in php:

First, we set up a memcache pool, which can be read according to different configurations and generate different memcache instances. Use $memcache - > addserver ($host, $port, $flag); add a memcache server to the connection pool. The code example is as follows

class memcachePool{
     private static $instance;
     private $memcacheList = array();
    private function __construct(){

    }
     public static function getInstance(){
         if(self::$instance != null)
             return self::$instance;
         self::$instance = new memcachePool();
         return self::$instance;
     }
    /**
     * get memcache object from pool
     * @param  [type] $host The server
     * @param  [type] $port port
     * @param  [type] $flag Controls whether persistent connections are used. Default TRUE
     * @return [type]
     */
     public function getMemcache($host,$port,$flag){
         if(isset($this->memcacheList[$host.$port]))
             return $this->memcacheList[$host.$port];

        $memcache = new Memcache();
        // Add a to the connection pool memcache The server
        $memcache->addServer($host,$port,$flag);
        //Turn on automatic compression of large value,The first parameter represents the critical point of processing data size, and the second parameter represents the compression ratio, which is 0 by default.2
        $memcache->setCompressThreshold(2000,0.2);
        $this->memcacheList[$host.$port] = $memcache;
        return $memcache;
     }
 }

Then we implement a method class that contains common methods of memcache, such as add,set,get,flush,delete, etc., which is named dlufmemcache here

class dlufMemcache{
     private $memcache = null;
     function __construct($host,$port){

       $this->memcache = memcachepool::getInstance()->getMemcache($host,$port,true);
     }
    /**
     * memcache set value
     * @param [type]  $key key
     * @param [type]  $value value
     * @param integer $expire  Expiration time. If this value is set to 0, it means that this data will never expire
     * @param integer $flag Flag bits use Memcache? Compressed to specify compression of values (using zlib)
     * @param [type]  $serializetype
     */
     public function set($key,$value,$expire=0,$flag=0,$serializetype=null){
        if($serializetype == 'json' && is_array($value)){
            $value = json_encode($value);
        }
         $this->memcache->set($key,$value,$flag,$expire);
     }
    /**
     * Find elements from the server
     * @param  [type] $key
     * @return [type]
     */
     public function get($key){
         return $this->memcache->get($key);
     }
    /**
     * Add an entry to the cache server
     * @param [type]  $key
     * @param [type]  $value
     * @param integer $expire
     * @param integer $flag
     * @param [type]  $serializetype
     */
    public function add($key,$value,$expire=0,$flag=0,$serializetype=null){
        if($serializetype == 'json' && is_array($value)){
            $value = json_encode($value);
        }
        $ret = $this->memcache->add($key,$value,$flag,$expire);
        return $ret;
    }
    /**
     * Clean (delete) all stored elements
     * @return [type]
     */
    public function flush(){
        return $this->memcache->flush();
    }
    /**
     *  Remove an element from the server
     * @param  [type] delete Parameter: key the key of the element to be deleted the execution time of the element to be deleted timeout if the value is 0, the element will be deleted immediately.
     * @return [type]
     */
    public function delete($key){
        $ret = $this->memcache->delete($key,0);
        return $ret;
    }
 }

Then call dlufmemcache:

1 $memcache = new dlufMemcache('127.0.0.1',11211);
2  $memcache->set('memcache','come on dluf&baidu !!!!!!');
3  $ret = $memcache->get('memcache');
4  echo print_r($ret,true);

Operation output is visible:

Keywords: PHP Database JSON zlib

Added by errtu on Sat, 30 Nov 2019 22:00:41 +0200