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


转载于:https://my.oschina.net/u/2430721/blog/625477

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值