您的位置:首页 > 数据库 > Redis

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

2016-02-28 00:00 645 查看
摘要: 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);
}

/**
* 经过测试,在redis中 incr $key 默认的key的值进行自增1  incrby $key $value向key中增加对应的值
* 在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);
}

/**
* *经过测试,在redis中 decr $key 默认的key的值进行自增1  decrby $key $value向key中增加对应的值
* 在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,名称为key的string的值在后面加上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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php redis