php对redis的string(字符串)操做

<?php

/**
* Class cache
* string 类型操做
* wanghang
* time:16/02/26
*/
class cache{

  protected $resis;
  public function __construct(){
     $this->redis = new Redis();
     $this->redis->connect('192.168.1.162', 6380);
  }

  /**
   * redis 中写入值
   *
   * @param $key string int
   * @param $value string
   * @return bool true or false
   */
  public function set($key,$value){

   return $this->redis->set($key,$value);
  }

  /**
   * redis中读取值
   *
   * @param $key  string
   * @return bool|string 没有值就返回false
   */
  public function get($key){

     $value=$this->redis->get($key);
     return $value;
     //return unserialize($value);

  }

  /**
   * 带生存时间的写入值
   *
   * @param $key string
   * @param $time  int
   * @param $value string
   * @return bool true or flase
   */
  public function setex($key,$time,$value){

     return $this->redis->setex($key,$time,$value);
  }

  /**
   * 判断是否重复的,写入值
   *
   * @param $key string
   * @param $value string
   * @return bool true(不重复) or false(重复)
   */
  public function setnx($key,$value){

     return $this->redis->setnx($key,$value);
  }

  /**
   * 删除指定key的值
   *
   * @param $key array array($key1,$key2)
   * return int 已经删除key的个数
   */
  public function delete($key){

     return $this->redis->delete($key);
  }

  /**
   * 获得一个key的生存时间 (永久数据返回-1,找不到值返回-2)
   *
   * @param $key string 对应的键值
   * @return int 剩余生成时间
   */
  public function ttl($key){

     return $this->redis->ttl($key);
  }

  /**
   * 移除Key的生存时间,使之成为永久的数据
   *
   * @param $key string
   * @return bool true|false
   */
  public function persist($key){

     return $this->redis->persist($key);
  }

  /**
   * 同时给多个key赋值
   *
   * @param $key_array array array('key'=>'value','key1'=>'value1)
   * @return bool true|false
   */
  public function mset($key_array){

     return $this->redis->mset($key_array);
  }

  /**
   * 判断 key 值是否存在
   * @param $key string
   * @return bool true|false
   */
  public function exists($key){

     return $this->redis->exists($key);
  }

  /**
   * 通过测试,在redisincr $key 默认的key的值进行自增1  incrby $key $valuekey中增长对应的值
   * php扩展中incr也可进行incrBy的操做(作加法)
   *
   * @param $key string
   * @return int
   */
  public function incr($key,$value=''){

     if(is_int($value)){
        return $this->redis->incr($key,$value);//incrBy
     }else{
        return $this->redis->incr($key);
     }
  }

  /**
   * 向指定键的键值增长对相应的值
   *
   * @param $key string
   * @param $value int
   * @return int
   */
  public function incrBy($key,$value){

     return $this->redis->incrBy($key,$value);
  }

  /**
   * *通过测试,在redisdecr $key 默认的key的值进行自增1  decrby $key $valuekey中增长对应的值
   * php扩展中decr也可进行decrBy的操做(作减法)
   *
   * @param $key
   * @param $value
   * @return int
   */
  public function decr($key,$value){
     if(is_int($value)){
        return $this->redis->decr($key,$value);
     }else{
        return $this->redis->decr($key);
     }
  }

  /**
   * 向指定键的键值减去对相应的值
   *
   * @param $key string
   * @param $value int
   * @return int
   */
  public function decrBy($key,$value){

     return $this->redis->decrBy($key,$value);
  }

  /**
   * 返回原来key中的值,并将value写入key
   *
   * @param $key string
   * @param $value string
   * @return string
   */
  public function getSet($key,$value){

     return $this->redis->getSet($key,$value);
  }

  /**
   * string,名称为keystring的值在后面加上value
   *
   * @param $key string
   * @param $value string
   * @return int 追加后字符串的长度
   */
  public function append($key,$value){

     return $this->redis->append($key,$value);
  }

  /**
   * 获取key的字符串的长度
   *
   * @param $key string
   * @return int
   */
  public function strlen($key){

     return $this->redis->strlen($key);
  }
}
相关文章
相关标签/搜索