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

php对redis的list(列表)操作

2016-02-28 00:00 477 查看
摘要: php对非关系型数据库redis的list操作

<?php
require('redistest.php');
class listcache extends cache{
public function __construct(){

parent::__construct();
}

/**
* 向列表的左边添加元素,返回值表示添加元素后列表的长度
*
* @param $key string
* @param $value string
* @return int
*/
public function lpush($key,$value){

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

/**
* 向列表的右边添加元素,返回值表示添加元素后列表的长度
*
* @param $key string
* @param $value string
* @return mixed int
*/
public function rpush($key,$value){

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

public function lpushx($key,$value){

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

public function rpushx($key,$value){

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

/**
* 从列表左边弹出一个元素,1、将列表左边元素从列表中移除,2、返回被移除的元素值
* (删除操作)
* @param $key string
* @return string
*/
public function lPop($key){

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

/**
* 从列表右边弹出一个元素
*
* @param $key string
* @return string
*/
public function rPop($key){

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

//$redis->blPop('key1', 'key2', 10);

/**
* 返回列表中元素的个数
*
* @param $key string
* @return int
*/
public function lSize($key){

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

/**
* 获取列表中指定位置的值
*
* @param $key
* @return mixed
*/
public function lGet($key,$location=0){

return $this->redis->lGet($key,$location);

}

/**
* 给元素指定位置赋相应的值
* @param $key string
* @param $location  int
* @param $value string
* @return bool true|false
*/
public function lSet($key,$location,$value){

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

/**
* 获取列表的片段
* @param $key string 键名
* @param $start  int
* @param $end   int (-1表示返回所有)
* @return  array
*/
public function lRange($key,$start,$end){

return $this->redis->lRange($key,$start,$end);
}

/**
* 只保留列表指定的片段
*
* @param $key string
* @param $start int
* @param $end int
* @return bool true|false
*/
public function lTrim($key,$start,$end){

return $this->redis->lTrim($key,$start,$end);
}

/**
* 删除count个名称为key的list中值为value的元素。count为0,删除所有值为value的元素,
* count>0从头至尾删除count个值为value的元素,count<0从尾到头删除|count|个值为value的元素
*
* @param $key
* @param $value
* @param $count
* @return mixed
*/
public function lRem($key,$value,$count){

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

/**
* LINSERT命令首先会在列表中从左到右查找值为pivot的元素,然后根据第二个参数是
*  BEFORE还是AFTER来决定将value插入到该元素的前面还是后面。
*
* @param $key string 列表名字
* @param $pivot string 所要查找的值
* @param $value  要插入的值
* @param int $dirct 0表示前面,1表示后面
* @return int 返回插入后列表的个数
*/
public function lInsert($key,$pivot,$value,$dirct=0){
if($dirct==0){
return $this->redis->lInsert($key,Redis::BEFORE,$pivot,$value);
}elseif($dirct==1){
return $this->redis->lInsert($key,Redis::AFTER,$pivot,$value);
}
}

/**
* 返回并删除名称为$key1的list的尾元素,并将该元素添加到名称为$key2的list的头部
*
* @param $key1 list
* @param $key2 list
* @return string
*/
public function rpoplpush($key1,$key2){

return $this->redis->rpoplpush($key1,$key2);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php redis