class myredis {    private $redis;    /**     * @param string $host     * @param int $post     */    public function __construct($host = 'xxxx', $port = 6379) {        $this->redis = new redis();        $this->redis->connect($host, $port);        return $this->redis;    }    /**     * 设置值  构建一个字符串     * @param string $key key名称     * @param string $value  设置值     * @param int $timeout 时间  0表示无过期时间     */    public function set($key, $value, $timeout=0) {        $retres = $this->redis->set($key, $value);        if ($timeout > 0)            $redis->expire('$key', $timeout);        return $retres;    }    /*     * 构建一个集合(无序集合)     * @param string $key 集合y名称     * @param string|array $value  值     */    public function sadd($key,$value){        return $this->redis->sadd($key,$value);    }        /*     * 构建一个集合(有序集合)     * @param string $key 集合名称     * @param string|array $value  值     */    public function zadd($key,$value){        return $this->redis->zadd($key,$value);    }        /**     * 取集合对应元素     * @param string $setname 集合名字     */    public function smembers($setname){        return $this->redis->smembers($setname);    }    /**     * 构建一个列表(先进后去,类似栈)     * @param sting $key key名称     * @param string $value 值     */    public function lpush($key,$value){        echo $key - $value \n;        return $this->redis->lpush($key,$value);    }          /**     * 构建一个列表(先进先去,类似队列)     * @param sting $key key名称     * @param string $value 值     */    public function rpush($key,$value){        echo $key - $value \n;        return $this->redis->rpush($key,$value);    }    /**     * 获取所有列表数据(从头到尾取)     * @param sting $key key名称     * @param int $head  开始     * @param int $tail     结束     */    public function lranges($key,$head,$tail){        return $this->redis->lrange($key,$head,$tail);    }        /**     * hash类型     * @param string $tablename  表名字key     * @param string $key            字段名字     * @param sting $value          值     */    public function hset($tablename,$field,$value){        return $this->redis->hset($tablename,$field,$value);    }        public function hget($tablename,$field){        return $this->redis->hget($tablename,$field);    }            /**     * 设置多个值     * @param array $keyarray key名称     * @param string|array $value 获取得到的数据     * @param int $timeout 时间     */    public function sets($keyarray, $timeout) {        if (is_array($keyarray)) {            $retres = $this->redis->mset($keyarray);            if ($timeout > 0) {                foreach ($keyarray as $key => $value) {                    $this->redis->expire($key, $timeout);                }            }            return $retres;        } else {            return call   . __function__ .  method  parameter  error !;        }    }    /**     * 通过key获取数据     * @param string $key key名称     */    public function get($key) {        $result = $this->redis->get($key);        return $result;    }    /**     * 同时获取多个值     * @param ayyay $keyarray 获key数值     */    public function gets($keyarray) {        if (is_array($keyarray)) {            return $this->redis->mget($keyarray);        } else {            return call   . __function__ .  method  parameter  error !;        }    }    /**     * 获取所有key名,不是值     */    public function keyall() {        return $this->redis->keys('*');    }    /**     * 删除一条数据key     * @param string $key 删除key的名称     */    public function del($key) {        return $this->redis->delete($key);    }    /**     * 同时删除多个key数据     * @param array $keyarray key集合     */    public function dels($keyarray) {        if (is_array($keyarray)) {            return $this->redis->del($keyarray);        } else {            return call   . __function__ .  method  parameter  error !;        }    }        /**     * 数据自增     * @param string $key key名称     */    public function increment($key) {        return $this->redis->incr($key);    }        /**     * 数据自减     * @param string $key key名称     */    public function decrement($key) {        return $this->redis->decr($key);    }           /**     * 判断key是否存在     * @param string $key key名称     */    public function isexists($key){        return $this->redis->exists($key);    }    /**     * 重命名- 当且仅当newkey不存在时,将key改为newkey ,当newkey存在时候会报错哦rename        *  和 rename不一样,它是直接更新(存在的值也会直接更新)     * @param string $key key名称     * @param string $newkey 新key名称     */    public function updatename($key,$newkey){        return $this->redis->renamenx($key,$newkey);    }       /**    * 获取key存储的值类型    * none(key不存在) int(0)  string(字符串) int(1)   list(列表) int(3)  set(集合) int(2)   zset(有序集) int(4)    hash(哈希表) int(5)    * @param string $key key名称    */    public function datatype($key){        return $this->redis->type($key);    }       /**     * 清空数据     */    public function flushall() {        return $this->redis->flushall();    }         /**     * 返回redis对象     * redis有非常多的操作方法,我们只封装了一部分     * 拿着这个对象就可以直接调用redis自身方法     * eg:$redis->redisothermethods()->keys('*a*')   keys方法没封     */    public function redisothermethods() {        return $this->redis;    }}版权声明:本文为博主原创文章,未经博主允许不得转载。
                                                                    以上就介绍了redis的php封装,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。