• 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);
        }
    }
    ?>
  • 相关阅读:
    jquery 表单清空
    CK-Editor content.replace
    CSS DIV HOVER
    返回上一页并刷新与返回上一页不刷新代码
    Google Java编程风格指南中文版
    编程常见英语词汇
    教你如何删除tomcat服务器的stdout.log文件
    @Autowired @Resource @Qualifier的区别
    JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总
    4.11 application未注入报错解决
  • 原文地址:https://www.cnblogs.com/fyandy/p/8688147.html
Copyright © 2020-2023  润新知