PHP redis 简单封装

<?PHP
/*
 * Created on 2018-03-22  redis
 * Programmer : andy
 * Develop a project PHP - MySQL - Apache
 */
namespace CommonModel;

class RedisModel
{
    private $host="afddaefwewffe";
    private $port = 6379;
    private $pwd = "f444ddcscadst";
    private $redis=null;

    /**
     * 架构函数
     * @param array $options 缓存参数
     * @access public
     */
    public function __construct() {
        $this->redis = new Redis();
        if ($this->redis->connect($this->host, $port) == false) {
            die($this->redis->getLastError());
        }
        if ($this->redis->auth($this->pwd) == false) {
                 die($redis->getLastError());
        }
    }

    /**
     * 读取缓存
     * @access public
     * @param string $name 缓存变量名
     * @return mixed
     */
    public function get($name) {
        $value = $this->redis->get($name);
        return $value;
        //$jsonData  = json_decode( $value, true );
        return ($jsonData === NULL) ? $value : $jsonData;    //检测是否为JSON数据 true 返回JSON解析数组, false返回源数据
    }

    /**
     * 写入缓存
     * @access public
     * @param string $name 缓存变量名
     * @param mixed $value  存储数据
     * @param integer $expire  有效时间(秒)
     * @return boolean
     */
    public function set($name, $value,$expire=86400) {
        // var_dump($value);exit;
        // $value=json_encode(array('a'=>'sss','v'=>'jdioflg'));
        $this->redis->set($name, $value,$expire);
        //$result=$this->redis->get($name);//print_r($result);exit;
        return $result;
    }

    /**
     * 删除缓存
     * @access public
     * @param string $name 缓存变量名
     * @return boolean
     */
    public function rm($name) {
        return $this->handler->delete($this->options['prefix'].$name);
    }

    /**
     * 清除缓存
     * @access public
     * @return boolean
     */
    public function clear() {
        return $this->handler->flushDB();
    }
    public function keys($key){
        $this->redis->keys($key);
        return $result;
    }
    public function close(){
        $this->redis->close();
    }
    public function flushAll(){
        $this->redis->flushAll();
    }
    public function delete($key){
        $this->redis->delete($key);
    }
}
?>
原文地址:https://www.cnblogs.com/fyandy/p/8688147.html