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); } }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- redis安装问题小结
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- 使用 Redis 和 Python 构建一个共享单车的应用程序
- Redis偶发连接失败案例实战记录
- Redis中实现查找某个值的范围
- Redis和Memcached的区别详解
- 分割超大Redis数据库例子
- Redis总结笔记(一):安装和常用命令
- Redis sort 排序命令详解
- 用Redis实现微博关注关系
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法