zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    ?>
  • 相关阅读:
    HDOJ:1051
    新sort的用法
    python课堂整理14---函数式编程
    python课堂整理13---函数的作用域及匿名函数
    python课堂整理12---递归
    python课堂整理11---函数即变量
    python课堂整理10---局部变量与全局变量
    python课堂整理9---函数1
    python课堂整理8---字符串格式化
    python课堂整理7---集合
  • 原文地址:https://www.cnblogs.com/fyandy/p/8688147.html
Copyright © 2011-2022 走看看