码迷,mamicode.com
首页 > Web开发 > 详细

PHP redis 简单封装

时间:2018-04-01 21:50:24      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:cti   清除   etl   封装   ace   数组   public   project   gpo   

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

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);
    }
}
?>

 

PHP redis 简单封装

标签:cti   清除   etl   封装   ace   数组   public   project   gpo   

原文地址:https://www.cnblogs.com/fyandy/p/8688147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!