Home > Backend Development > PHP Tutorial > Encapsulate Redis into singleton mode in PHP

Encapsulate Redis into singleton mode in PHP

Guanhui
Release: 2023-04-08 19:52:01
forward
3765 people have browsed it

Encapsulate Redis into singleton mode in PHP

The singleton pattern is one of the simplest forms of design patterns. The purpose of this pattern is to make an object of a class the only instance in the system. To achieve this, you start by instantiating it on the client side. Therefore, it is necessary to use a mechanism that only allows the generation of unique instances of the object class, "blocking" all access to the object that is intended to be generated. Use factory methods to limit the instantiation process. This method should be a static method (class method), because there is no point in having an instance of the class generate another unique instance.

<?php
/** 
* Created by PhpStorm. 
* User: Administrator 
* Date: 2018/7/16 
* Time: 14:19 
*/
/** 
* 实现同步实现同步redis 
*/
namespace  app\common\lib\redis;
class Predis{ 
   
/**     
* 单例模式的变量     
* @var null     
*/    
private static $_instance=null;     
public $redis = &#39;&#39;;
      
/**     
* 单例模式应用防止多次连接redis,提高性能     
* @return Predis|null     
*/    
public static function getInstance(){        
    if(is_null(self::$_instance) || empty(self::$_instance)){            
        self::$_instance = new self();        
    }        
    return self::$_instance;    
}
      
/**     
*连接redis     
*/    
private function __construct()    {        
    $this->redis = new \Redis();        
    $result = $this->redis->connect(config(&#39;redis.host&#39;), config(&#39;redis.port&#39;));       
    if($result==false){            
        throw new \Exception(&#39;redis connect fail&#39;);        
    }     
}     
/**     
*redis set方法的应用     
* @param $key     
* @param $value     
* @param int $time     
* @return bool|string     
*/    
public function set($key,$value,$time=0){        
    if(!$key){            
        return &#39;&#39;;        
    }         
    if(is_array($value)){            
        $value = json_encode($value);       
    }         
    if(!$time){            
        return $this->redis->set($key,$value);       
    }        
    return $this->redis->setex($key,$time,$value);    
}     
/**     
* redis get方法     
* @param $key     
* @return string     
*/    
public function get($key){        
    if(!$key){            
        return &#39;&#39;;        
    }        
    return $this->redis->get($key);    
}     
/**     
* 获取有序列表的结合     
* @param $key     
* @return array     
*/    
public function sMembers($key) {        
    return $this->redis->sMembers($key);    
}      
/**     
* 获取list的元素值结合     
*/    
public function lRange($key){        
    var_dump($key);        
    return $this->redis->lRange($key,0,-1);    
}     
/**     
* 魔术方法__call     
*/    
public function __call($name, $arguments) {        
    echo $name.PHP_EOL;        
    print_r($arguments);        
    if(count($arguments) != 2) {            
        return &#39;&#39;;       
    }        
    $this->redis->$name($arguments[0], $arguments[1]);   
} 
}
Copy after login

The above is the detailed content of Encapsulate Redis into singleton mode in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template