PHP Redis

原创
2016-07-29 09:12:10 843浏览

   1 if (!defined('BASEPATH')) exit('No direct script access allowed'); 
   2class Myredis
   3{
   4//redis所有已知命令:
   5    //append,auth,bgrewriteaof,bgsave,bitcount,bitop,blpop,brpop,brpoplpush,client-kill,client-list,client-getname, client-setname,config-get,config-set,config-resetstat,dbsize,debug-object,debug-segfault,decr,decrby,del,discard, dump,echo,eval,evalsha,exec,exists,expire,expireat,flushall,flushdb,get,getbit,getrange,getset,hdel,hexists,hget, hgetall,hincrby,hincrbyfloat,hkeys,hlen,hmget,hmset,hset,hsetnx,hvals,incr,incrby,incrbyfloat,info,keys,lastsave, lindex,linsert,llen,lpop,lpush,lpushx,lrange,lrem,lset,ltrim,mget,migrate,monitor,move,mset,msetnx,multi,object, persist,pexpire,pexpireat,pfadd,pfcount,pfmerge,ping,psetex,psubscribe,pttl,publish,punsubscribe,quit,randomkey,rename,renamenx,restore, rpop,rpoplpush,rpush,rpushx,sadd,save,scard,script-exists,script-flush,script-kill,script-load,sdiff,sdiffstore, select,set,setbit,setex,setnx,setrange,shutdown,sinter,sinterstore,sismember,slaveof,slowlog,smembers,smove,sort, spop,srandmember,srem,strlen,subscribe,sunion,sunionstore,sync,time,ttl,type,unsubscribe,unwatch,watch,zadd,zcard, zcount,zincrby,zinterstore,zrange,zrangebyscore,zrank,zrem,zremrangebyrank,zremrangebyscore,zrevrange, zrevrangebyscore,zrevrank,zscore,zunionstore,pubsub,config-rewrite,client-pause,hscan,scan,sscan,zscan   6   7privatestatic$redis;
   8   9publicfunction __construct($arConfig = array('host'=>'127.0.0.1', 'port'=> 6379))
  10    {
  11$this->host    =    $arConfig['host'];
  12$this->port    =    $arConfig['port'];
  13         self::$redis    =    new    Redis();
  14         self::$redis->connect($this->host, $this->port);
  15return self::$redis;
  16    }
  17  18publicfunction __call($sMethod, $arParam)
  19    {
  20returncall_user_func_array(array(self::$redis, $sMethod), $arParam);
  21    }
  22  23/******************* key *********************/  24  25/**
  26     * 查找所有符合给定模式 pattern 的 key 。
  27     * KEYS * 匹配数据库中所有 key 。
  28     * KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
  29     * KEYS h*llo 匹配 hllo 和 heeeeello 等。
  30     * KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。
  31     * 特殊符号用 \ 隔开。
  32     * @author    zhaoyingnan    2015-10-16 17:33
  33     * @param    string        $sPattern    匹配模式
  34     * @return    array
  35     **/  36publicfunction keys($sPattern = '*')
  37    {
  38//echo $sPattern;  39return self::$redis->keys($sPattern);
  40    }
  41  42/**
  43     * 返回key是否存在。
  44     * @author    zhaoyingnan    2015-10-16 17:40
  45     * @param    string        $sKey        要检测的 key
  46     * @return    bool
  47     **/  48publicfunction exists($sKey)
  49    {
  50if(!$sKey)returnFALSE;
  51return self::$redis->exists($sKey);
  52    }
  53  54/**
  55     * 设置key的过期时间。如果key已过期,将会被自动删除。设置了过期时间的key被称之为volatile。
  56     * 在key过期之前可以重新更新他的过期时间,也可以使用PERSIST命令删除key的过期时间。
  57     * @author    zhaoyingnan    2015-10-16 17:46
  58     * @param    string        $sKey        key
  59     * @param    int            $iSecond    生存周期(秒)
  60     * @return    bool
  61     **/  62publicfunction expire($sKey, $iSecond = 60)
  63    {
  64if(!$sKey)returnFALSE;
  65return self::$redis->expire($sKey, $iSecond);
  66    }
  67  68/**
  69     * 这个命令和 EXPIRE 命令的作用类似,但是它以毫秒为单位设置 key 的生存时间,而不像 EXPIRE 命令那样,以秒为单位。
  70     * @author    zhaoyingnan    2015-10-19 16:00
  71     * @param    string        $sKey        key
  72     * @param    int            $iSecond    生存周期(秒)
  73     * @return    bool
  74     **/  75publicfunction pexpire($sKey, $iMilliseconds = 60000)
  76    {
  77if(!$sKey)returnFALSE;
  78return self::$redis->pexpire($sKey, $iMilliseconds);
  79    }
  80  81/**
  82     * EXPIREAT 的作用和 EXPIRE类似,都用于为 key 设置生存时间。不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳 Unix timestamp 。
  83     * @author    zhaoyingnan    2015-10-16 18:03
  84     * @param    string        $sKey        key
  85     * @param    int            $iUnixtimestamp    UNIX 时间戳(秒)
  86     * @return    bool
  87     **/  88publicfunction expireat($sKey, $iUnixtimestamp)
  89    {
  90if(!$sKey || !$iUnixtimestamp)returnFALSE;
  91return self::$redis->expireat($sKey, $iUnixtimestamp);
  92    }
  93  94/**
  95     * PEXPIREAT 这个命令和 EXPIREAT命令类似,但它以毫秒为单位设置 key 的过期 unix 时间戳,而不是像 EXPIREAT 那样,以秒为单位。
  96     * EXPIREAT 的作用和 EXPIRE类似,都用于为 key 设置生存时间。不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳 Unix timestamp 。
  97     * @author    zhaoyingnan    2015-10-16 18:03
  98     * @param    string        $sKey        key
  99     * @param    int            $iMilliseconds    UNIX 时间戳(毫秒)
 100     * @return    bool
 101     **/ 102publicfunction pexpireat($sKey, $iMilliseconds)
 103    {
 104if(!$sKey || !$iMilliseconds)returnFALSE;
 105return self::$redis->pexpireat($sKey, $iMilliseconds);
 106    }
 107 108/**
 109     * 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
 110     * @author    zhaoyingnan    2015-10-16 17:52
 111     * @param    string        $sKey        key
 112     * @return    int            当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key 的剩余生存时间。
 113     **/ 114publicfunction ttl($sKey)
 115    {
 116if(!$sKey)return -2;
 117return self::$redis->ttl($sKey);
 118    }
 119 120/**
 121     * 这个命令类似于 TTL 命令,但它以毫秒为单位返回 key 的剩余生存时间,而不是像 TTL 命令那样,以秒为单位。
 122     * @author    zhaoyingnan    2015-10-19 16:06
 123     * @param    string        $sKey        key
 124     * @return    int            当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key 的剩余生存时间。
 125     **/ 126publicfunction pttl($sKey)
 127    {
 128if(!$sKey)return -2;
 129return self::$redis->pttl($sKey);
 130    }
 131 132/**
 133     * 将 key 原子性地从当前实例传送到目标实例的指定数据库上,一旦传送成功, key 保证会出现在目标实例上,而当前实例上的 key 会被删除。
 134     * 这个命令是一个原子操作,它在执行的时候会阻塞进行迁移的两个实例,直到以下任意结果发生:迁移成功,迁移失败,等到超时。
 135     * @author    zhaoyingnan    2015-10-16 18:24
 136     * @param    string        $sHost        目标 ip
 137     * @param    int            $iPort        端口
 138     * @param    string        $sKey        要操作的 key
 139     * @param    int            $iDb        目标实例的数据库的编号
 140     * @param    int            $iTimeout    timeout 参数以毫秒为格式,指定当前实例和目标实例进行沟通的最大间隔时间。这说明操作并不一定要在 timeout 毫秒内完成,只是说数据传送的时间不能超过这个 timeout 数。
 141     * @return    bool        注:当目标实例的指定的数据库中存在指定的 key 返回 FALS,当前实例中的 key 并没有被删除,也没有移动到目标实例上,目标实例上的 key 还是原来的
 142     **/ 143publicfunction migrate($sHost, $iPort, $sKey, $iDb, $iTimeout)
 144    {
 145if(!$sHost || !$iPort || !$sKey || !$iDb || !$iTimeout)returnFALSE;
 146return self::$redis->migrate($sHost, $iPort, $sKey, $iDb, $iTimeout);
 147    }
 148 149/**
 150     * 将当前数据库的 key 移动到给定的数据库 db 当中。
 151     * 如果当前数据库(源数据库)和给定数据库(目标数据库)有相同名字的给定 key ,或者 key 不存在于当前数据库,那么 MOVE 没有任何效果。
 152     * @author    zhaoyingnan    2015-10-19 15:42
 153     * @param    string        $sKey    key
 154     * @param    int            $iDb    移动到给定的数据库 id
 155     * @return    bool
 156     **/ 157publicfunction move($sKey, $iDb)
 158    {
 159if(!$sKey || !$iDb)returnFALSE;    
 160return self::$redis->move($sKey, $iDb);
 161    }
 162 163/**
 164     * 移除给定key的生存时间,将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )。
 165     * @author    zhaoyingnan    2015-10-19 15:55
 166     * @param    string        $sKey    key
 167     * @return    bool        当生存时间移除成功时,返回 1  如果 key 不存在或 key 没有设置生存时间,返回 0
 168     **/ 169publicfunction persist($sKey)
 170    {
 171if(!$sKey)returnFALSE;    
 172return self::$redis->persist($sKey);
 173    }
 174 175/**
 176     * 从当前数据库返回一个随机的key。
 177     * @author    zhaoyingnan    2015-10-19 16:08
 178     * @return    string        如果数据库没有任何key,返回nil,否则返回一个随机的key。    
 179     **/ 180publicfunction randomkey()
 181    {
 182return self::$redis->randomkey();
 183    }
 184 185/**
 186     * 将key重命名为newkey,如果key与newkey相同,将返回一个错误。如果newkey已经存在,则值将被覆盖。
 187     * @author    zhaoyingnan    2015-10-19 16:12
 188     * @param    string        $sKey    key
 189     * @param    string        $sNewKey    重命名后的 key 值    
 190     * @return    bool
 191     **/ 192publicfunctionrename($sKey, $sNewKey)
 193    {
 194if(!$sKey || !$sNewKey)returnFALSE;    
 195return self::$redis->rename($sKey, $sNewKey);
 196    }
 197 198/**
 199     * 当且仅当 newkey 不存在时,将 key 改名为 newkey 。当 key 不存在时,返回一个错误。
 200     * @author    zhaoyingnan    2015-10-19 16:16
 201     * @param    string        $sKey    key
 202     * @param    string        $sNewKey    重命名后的 key 值    
 203     * @return    bool
 204     **/ 205publicfunction renamenx($sKey, $sNewKey)
 206    {
 207if(!$sKey || !$sNewKey)returnFALSE;    
 208return self::$redis->renamenx($sKey, $sNewKey);
 209    }
 210 211/**
 212     * 返回 key 所储存的值的类型。
 213     * @author    zhaoyingnan    2015-10-19 16:25
 214     * @param    string        $sKey    key
 215     * @return    string        none (key不存在) string (字符串) list (列表) set (集合) zset (有序集) hash (哈希表)
 216     **/ 217publicfunction type($sKey)
 218    {
 219return self::$redis->type($sKey);
 220    }
 221 222/******************* string *********************/ 223 224/**
 225     * 将key和value对应。如果key已经存在了,它会被覆盖,而不管它是什么类型。
 226     * @author    zhaoyingnan    2015-10-19 16:35
 227     * @param    string        $sKey        key
 228     * @param    string        $sValue        key 对应的值
 229     * @return    bool
 230     **/ 231publicfunction set($sKey, $sValue)
 232    {
 233if(!$sKey)returnFALSE;
 234return self::$redis->set($sKey, $sValue);
 235    }
 236 237/**
 238     * 设置key对应字符串value,并且设置key在给定的 seconds 时间之后超时过期。
 239     * @author    zhaoyingnan    2015-11-03 11:25
 240     * @param    string        $sKey        被操作的 key
 241     * @param    int            $iSecond    生命周期(秒)
 242     * @param    string        $sValue        key 对应的 value
 243     * @return    bool
 244     **/ 245publicfunction setex($sKey, $iSecond, $sValue)
 246    {
 247if(!$sKey || !$sValue)returnFALSE;
 248$iSecond = $iSecond ? abs((intval($iSecond))) : 30;
 249return self::$redis->setex($sKey, $iSecond, $sValue);
 250    }
 251 252/**
 253     * 设置key对应字符串value,并且设置key在给定的 milliseconds 时间之后超时过期。
 254     * @author    zhaoyingnan    2015-11-03 11:25
 255     * @param    string        $sKey        被操作的 key
 256     * @param    int            $iMillSecond    生命周期(毫秒)
 257     * @param    string        $sValue        key 对应的 value
 258     * @return    bool
 259     **/ 260publicfunction psetex($sKey, $iMilliseconds , $sValue)
 261    {
 262if(!$sKey || !$sValue)returnFALSE;
 263$iMilliseconds = $iMilliseconds ? abs((intval($iMilliseconds))) : 30;
 264return self::$redis->psetex($sKey, $iMilliseconds, $sValue);
 265    }
 266 267/**
 268     * 自动将key对应到value并且返回原来key对应的value。如果key存在但是对应的value不是字符串,就返回错误。
 269     * @author    zhaoyingnan    2015-10-19 18:10
 270     * @param    string        $sKey        key
 271     * @param    string        $sValue        设置的新的值
 272     * @return    string
 273     **/ 274publicfunction getset($sKey, $sValue)
 275    {
 276if(!$sKey)return '';
 277return self::$redis->getset($sKey, $sValue);
 278    }
 279 280/**
 281     * 对应给定的keys到他们相应的values上。MSET会用新的value替换已经存在的value,就像普通的SET命令一样。
 282     * 如果你不想覆盖已经存在的values,请参看命令MSETNX。
 283     * MSET是原子的,所以所有给定的keys是一次性set的。客户端不可能看到这种一部分keys被更新而另外的没有改变的情况。
 284     * @author    zhaoyingnan    2015-11-03 11:04
 285     * @param    array        $arArray    被设置的关联数组
 286     * @return    bool
 287     **/ 288publicfunction mset($arArray = array())
 289    {
 290if(!$arArray)returnFALSE;
 291return self::$redis->mset($arArray);
 292    }
 293 294/**
 295     * 对应给定的keys到他们相应的values上。
 296     * 只要有一个key已经存在,MSETNX一个操作都不会执行。 
 297     * 由于这种特性,MSETNX可以实现要么所有的操作都成功,要么一个都不执行,这样可以用来设置不同的key,来表示一个唯一的对象的不同字段。
 298     * MSETNX是原子的,所以所有给定的keys是一次性set的。客户端不可能看到这种一部分keys被更新而另外的没有改变的情况。
 299     * @author    zhaoyingnan    2015-11-03 11:11
 300     * @param    array        $arArray    被设置的关联数组
 301     * @return    bool        TRUE 所有的key被set, FALSE 没有key被set(至少其中有一个key是存在的)
 302     **/ 303publicfunction    msetnx($arArray = array())
 304    {
 305if(!$arArray)returnFALSE;
 306return self::$redis->msetnx($arArray);
 307    }
 308 309/**
 310     * 如果key不存在,就设置key对应字符串value。
 311     * 在这种情况下,该命令和SET一样。当key已经存在时,就不做任何操作。
 312     * SETNX是"SET if Not eXists"。
 313     * @author    zhaoyingnan    2015-11-03 11:49
 314     * @param    string        $sKey        key
 315     * @param    string        $sValue        值
 316     * @return    bool        TRUE key被set, FALSE key没有被set
 317     **/ 318publicfunction setnx($sKey, $sValue)
 319    {
 320if(!$sKey)returnFALSE;
 321return self::$redis->setnx($sKey, $sValue);
 322    }
 323 324/**
 325     * 返回key的value。如果key不存在,返回特殊值nil。如果key的value不是string,就返回错误,因为GET只处理string类型的values。
 326     * @author    zhaoyingnan    2015-10-19 17:57
 327     * @param    string        $sKey        key
 328     * @return    string
 329     **/ 330publicfunction get($sKey)
 331    {
 332if(!$sKey)return '';
 333return self::$redis->get($sKey);
 334    }
 335 336/**
 337     * 返回所有指定的key的value。对于每个不对应string或者不存在的key,都返回特殊值nil。正因为此,这个操作从来不会失败。
 338     * @author    zhaoyingnan    2015-11-03 10:55
 339     * @param    array        $arKey        要获取的 key 的数组
 340     * @return    array        redis返回的是以数字为索引的数组,这里返回的是一个关联数组
 341     **/ 342publicfunction mget($arKey = array())
 343    {
 344if(!$arKey)returnarray();
 345$arResult = self::$redis->mget($arKey);
 346returnarray_combine($arKey, $arResult);
 347    }
 348 349/**
 350     * 如果 key 已经存在,并且值为字符串,那么这个命令会把 value 追加到原来值(value)的结尾。
 351     * 如果 key 不存在,那么它将首先创建一个空字符串的key,再执行追加操作,这种情况 APPEND 将类似于 SET 操作。
 352     * @author    zhaoyingnan    2015-10-19 16:34
 353     * @param    string        $sKey        key
 354     * @param    string        $sValue        追加的值
 355     * @return    int
 356     **/ 357publicfunction append($sKey, $sValue)
 358    {
 359if(!$sKey)returnFALSE;
 360return self::$redis->append($sKey, $sValue);
 361    }
 362 363/**
 364     * 对key对应的数字做加1操作。如果key不存在,那么在操作之前,这个key对应的值会被置为0。
 365     * 如果key有一个错误类型的value或者是一个不能表示成数字的字符串,就返回错误。
 366     * 这个操作最大支持在64位有符号的整型数字。
 367     * @author    zhaoyingnan    2015-10-19 17:44
 368     * @param    string        $sKey        key
 369     * @return    string
 370     **/ 371publicfunction incr($sKey)
 372    {
 373if(!$sKey)return '';
 374return self::$redis->incr($sKey);
 375    }
 376 377/**
 378     * 将key对应的数字加decrement。如果key不存在,操作之前,key就会被置为0。
 379     * 如果key的value类型错误或者是个不能表示成数字的字符串,就返回错误。这个操作最多支持64位有符号的正型数字。
 380     * @author    zhaoyingnan    2015-10-19 17:44
 381     * @param    string        $sKey        key
 382     * @param    int            $iIncrement    步进值
 383     * @return    string
 384     **/ 385publicfunction incrby($sKey, $iIncrement)
 386    {
 387if(!$sKey || !$iIncrement)return '';
 388return self::$redis->incrby($sKey, $iIncrement);
 389    }
 390 391/**
 392     * 将key对应的数字加decrement。如果key不存在,操作之前,key就会被置为0。
 393     * 如果key的value类型错误或者是个不能表示成数字的字符串,就返回错误。这个操作最多支持64位有符号的正型数字。
 394     * @author    zhaoyingnan    2015-10-19 17:44
 395     * @param    string        $sKey        key
 396     * @param    fload        $floatIncrement    步进值
 397     * @return    string
 398     **/ 399publicfunction incrbyfloat($sKey, $floatIncrement)
 400    {
 401if(!$sKey || !$floatIncrement)return '';
 402return self::$redis->incrbyfloat($sKey, $floatIncrement);
 403    }
 404 405/**
 406     * 对key对应的数字做减1操作。如果key不存在,那么在操作之前,这个key对应的值会被置为0。
 407     * 如果key有一个错误类型的value或者是一个不能表示成数字的字符串,就返回错误。这个操作最大支持在64位有符号的整型数字。
 408     * @author    zhaoyingnan    2015-10-19 17:44
 409     * @param    string        $sKey        key
 410     * @return    string
 411     **/ 412publicfunction decr($sKey)
 413    {
 414if(!$sKey)return '';
 415return self::$redis->decr($sKey);
 416    }
 417 418/**
 419     * 将key对应的数字减decrement。如果key不存在,操作之前,key就会被置为0。
 420     * 如果key的value类型错误或者是个不能表示成数字的字符串,就返回错误。这个操作最多支持64位有符号的正型数字。
 421     * @author    zhaoyingnan    2015-10-19 17:44
 422     * @param    string        $sKey        key
 423     * @param    int            $iIncrement    步进值
 424     * @return    string
 425     **/ 426publicfunction decrby($sKey, $iIncrement)
 427    {
 428if(!$sKey || !$iIncrement)return '';
 429return self::$redis->decrby($sKey, $iIncrement);
 430    }
 431 432/**
 433     * 这个命令是被改成GETRANGE的,在小于2.0的Redis版本中叫SUBSTR。 
 434     * 返回key对应的字符串value的子串,这个子串是由start和end位移决定的(两者都在string内)。
 435     * 可以用负的位移来表示从string尾部开始数的下标。所以-1就是最后一个字符,-2就是倒数第二个,以此类推。
 436     * 这个函数处理超出范围的请求时,都把结果限制在string内。
 437     * @author    zhaoyingnan    2015-10-19 18:04
 438     * @param    string        $sKey        key
 439     * @pause    int            $iStart        开始位置
 440     * @pause    int            $iEnd        结束位置
 441     * @return    string
 442     **/ 443publicfunction getrange($sKey, $iStart = 0, $iEnd = -1)
 444    {
 445if(!$sKey)return '';
 446return self::$redis->getrange($sKey, $iStart, $iEnd);
 447    }
 448 449/**
 450     * 返回key的string类型value的长度。如果key对应的非string类型,就返回错误。
 451     * @author    zhaoyingnan    2015-11-03 11:40
 452     * @param    string        $sKey
 453     * @return    
 454     **/ 455publicfunctionstrlen($sKey)
 456    {
 457if(!$sKey)returnFALSE;
 458return self::$redis->strlen($sKey);
 459    }
 460 461/******************* list *********************/ 462 463/**
 464     * 将所有指定的值插入到存于 key 的列表的头部。如果 key 不存在,那么在进行 push 操作前会创建一个空列表。 
 465     * 如果 key 对应的值不是一个 list 的话,那么会返回一个错误。
 466     * 可以使用一个命令把多个元素 push 进入列表,只需在命令末尾加上多个指定的参数。
 467     * 元素是从最左端的到最右端的、一个接一个被插入到 list 的头部。 所以对于这个命令例子 LPUSH mylist a b c,返回的列表是 c 为第一个元素, b 为第二个元素, a 为第三个元素。
 468     * @author    zhaoyingnan    2015-11-03 11:59
 469     * @param    string        $sKey
 470     * @param    array        $arValue    需要 push 到 key 中的值的数组
 471     * @return    int            在 push 操作后的 list 长度。
 472     **/ 473publicfunction lpush($sKey, $arValue = array())
 474    {
 475if(!$sKey || !$arValue)return 0;
 476foreach($arValueas$val)
 477             self::$redis->lpush($sKey, $val);
 478return self::llen($sKey);
 479    }
 480 481/**
 482     * 只有当 key 已经存在并且存着一个 list 的时候,在这个 key 下面的 list 的头部插入 value。 
 483     * 与 LPUSH 相反,当 key 不存在的时候不会进行任何操作。
 484     * @author    zhaoyingnan    2015-11-03 13:21
 485     * @param    string        $sKey
 486     * @param    array        $arValue    需要 push 到 key 中的值的数组
 487     * @return    int
 488     **/ 489publicfunction lpushx($sKey, $arValue = array())
 490    {
 491if(!$sKey || !$arValue)return 0;
 492foreach($arValueas$val)
 493             self::$redis->lpushx($sKey, $val);
 494return self::llen($sKey);
 495    }
 496 497/**
 498     * 向存于 key 的列表的尾部插入所有指定的值。如果 key 不存在,那么会创建一个空的列表然后再进行 push 操作。 
 499     * 当 key 保存的不是一个列表,那么会返回一个错误。
 500     * 可以使用一个命令把多个元素打入队列,只需要在命令后面指定多个参数。
 501     * 元素是从左到右一个接一个从列表尾部插入。 比如命令 RPUSH mylist a b c 会返回一个列表,其第一个元素是 a ,第二个元素是 b ,第三个元素是 c。
 502     * @author    zhaoyingnan    2015-11-03 12:15
 503     * @param    string        $sKey
 504     * @param    array        $arValue    需要 push 到 key 中的值的数组
 505     * @return    int            在 push 操作后的 list 长度。
 506     **/ 507publicfunction rpush($sKey, $arValue = array())
 508    {
 509if(!$sKey || !$arValue)return 0;
 510foreach($arValueas$val)
 511             self::$redis->lpush($sKey, $val);
 512return self::llen($sKey);
 513    }
 514 515/**
 516     * 将值 value 插入到列表 key 的表尾, 当且仅当 key 存在并且是一个列表。 
 517     * 和 RPUSH 命令相反, 当 key 不存在时,RPUSHX 命令什么也不做。
 518     * @author    zhaoyingnan    2015-11-03 13:23
 519     * @param    string        $sKey
 520     * @param    array        $arValue    需要 push 到 key 中的值的数组
 521     * @return    int            在 push 操作后的 list 长度。
 522     **/ 523publicfunction rpushx($sKey, $arValue = array())
 524    {
 525if(!$sKey || !$arValue)return 0;
 526foreach($arValueas$val)
 527             self::$redis->rpushx($sKey, $val);
 528return self::llen($sKey);
 529    }
 530 531/**
 532     * 返回存储在 key 里的list的长度。 
 533     * @author    zhaoyingnan    2015-11-03 12:12
 534     * @param    string        $sKey
 535     * @return    bool        如果 key 不存在,那么就被看作是空list,并且返回长度为 0。 当存储在 key 里的值不是一个list的话,会返回error。
 536     **/ 537publicfunction llen($sKey)
 538    {
 539if(!$sKey)return 0;
 540return self::$redis->llen($sKey);
 541    }
 542 543/**
 544     * 返回 key 对应的列表里的元素的索引 index 的值
 545     * 下标是从0开始索引的,所以 0 是表示第一个元素, 1 表示第二个元素,并以此类推。 
 546     * 负数索引用于指定从列表尾部开始索引的元素。在这种方法下,-1 表示最后一个元素,-2 表示倒数第二个元素,并以此往前推。
 547     * 当 key 位置的值不是一个列表的时候,会返回一个error。
 548     * @author    zhaoyingnan    2015-11-03 13:30
 549     * @param    string        $sKey
 550     * @param    array        $index        key 对应的列表中的 index 索引
 551     * @return    mix
 552     **/ 553publicfunction lindex($sKey, $index = 0)
 554    {
 555if(!$sKey)returnFALSE;
 556return self::$redis->lindex($sKey, intval($index));
 557    }
 558 559/**
 560     * 设置 index 位置的list元素的值为 value。
 561     * 下标是从0开始索引的,所以 0 是表示第一个元素, 1 表示第二个元素,并以此类推。 
 562     * 当index超出范围时会返回一个error。
 563     * @author    zhaoyingnan    2015-11-03 14:27
 564     * @param    string        $sKey
 565     * @param    int            $index        key 对应的 list 中小标为 index
 566     * @param    string        $sValue        被设置的值
 567     * @return    
 568     **/ 569publicfunction lset($sKey, $index, $sValue)
 570    {
 571if(!$sKey || !$sValue)returnFALSE;
 572return self::$redis->lset($sKey, $index, $sValue);
 573    }
 574 575/**
 576     * 把 value 插入存于 key 的列表中在基准值 pivot 的前面或后面。
 577     * 当 key 不存在时,这个list会被看作是空list,任何操作都不会发生。
 578     * 当 key 存在,但保存的不是一个list的时候,会返回error。
 579     * @author    zhaoyingnan    2015-11-03 13:42
 580     * @param    string        $sKey
 581     * @param    string        $sPosion    在基准值前面或者后面(BEFORE or AFTER)
 582     * @param    string        $pivot        列表中的基准值
 583     * @param    string        $sValue        被插入的值
 584     * @return    mix            经过插入操作后的list长度,或者当 pivot 值找不到的时候返回 -1。
 585     **/ 586publicfunction linsert($sKey, $sPosion, $pivot, $sValue)
 587    {
 588if(!$sKey || !$pivot || !$sValue)returnFALSE;
 589$sPosion = in_array($sPosion, array('BEFORE', 'AFTER')) ? strtoupper($sPosion) : 'BEFORE';
 590return self::$redis->linsert($sKey, $sPosion, $pivot, $sValue);
 591    }
 592 593/**
 594     * 从存于 key 的列表里移除前 count 次出现的值为 value 的元素。 这个 count 参数通过下面几种方式影响这个操作:
 595     * count > 0: 从头往尾移除值为 value 的元素。
 596     * count  597     * count = 0: 移除所有值为 value 的元素。
 598     * 比如, LREM list -2 "hello" 会从存于 list 的列表里移除最后两个出现的 "hello"。
 599     * 需要注意的是,如果list里没有存在key就会被当作空list处理,所以当 key 不存在的时候,这个命令会返回 0。
 600     * @author    zhaoyingnan    2015-11-03 13:53
 601     * @param    string        $sKey
 602     * @param    int            $iCount        count > 0: 从头往尾移除值为 value 的元素。 count  603     * @param    string        $sValue        要删除的值
 604     * @return    int
 605     **/ 606//public function lremu($sKey, $iCount, $sValue)
 607    //{
 608    //    var_dump($sValue);
 609    //    if(!$sKey || !$sValue)return FALSE;
 610    //    return self::$redis->lrem($sKey, intval($iCount), $sValue);
 611    //} 612 613/**
 614     * 修剪(trim)一个已存在的 list,这样 list 就会只包含指定范围的指定元素。
 615     * start 和 stop 都是由0开始计数的, 这里的 0 是列表里的第一个元素(表头),1 是第二个元素,以此类推。
 616     * @author    zhaoyingnan    2015-11-03 14:45
 617     * @param    string        $sKey
 618     * @param    int            $iStart        指定范围内的开始位置
 619     * @param    int            $iEnd        制定范围内的结束位置
 620     * @return    bool
 621     **/ 622publicfunctionltrim($sKey, $iStart, $iEnd)
 623    {
 624if(!$sKey)returnFALSE;
 625return self::$redis->ltrim($sKey, intval($iStart), intval($iEnd));
 626    }
 627 628/**
 629     * 返回存储在 key 的列表里指定范围内的元素。
 630     * start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。
 631     * 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。
 632     * @author    zhaoyingnan    2015-11-03 14:54
 633     * @param    string        $sKey
 634     * @param    int            $iStart        开始位置
 635     * @param    int            $iEnd        结束位置
 636     * @return    array
 637     **/ 638publicfunction lrange($sKey, $iStart, $iEnd)
 639    {
 640if(!$sKey)returnFALSE;
 641return self::$redis->lrange($sKey, intval($iStart), intval($iEnd));
 642    }
 643 644/**
 645     * 移除并且返回 key 对应的 list 的第一个元素。
 646     * @author    zhaoyingnan    2015-11-03 21:49
 647     * @param    string        $sKey
 648     * @return    bool
 649     **/ 650publicfunction lpop($sKey)
 651    {
 652if(!$sKey)returnFALSE;
 653return self::$redis->lpop($sKey);
 654    }
 655 656/**
 657     * 移除并返回存于 key 的 list 的最后一个元素。
 658     * @author    zhaoyingnan    2015-11-03 21:49
 659     * @param    string        $sKey
 660     * @return    bool
 661     **/ 662publicfunction rpop($sKey)
 663    {
 664if(!$sKey)returnFALSE;
 665return self::$redis->rpop($sKey);
 666    }
 667 668/******************* set *********************/ 669 670/**
 671     * 添加一个或多个指定的member元素到集合的 key中.
 672     * 指定的一个或者多个元素member 如果已经在集合key中存在则忽略.
 673     * 如果集合key 不存在,则新建集合key,并添加member元素到集合key中.
 674     * 如果key 的类型不是集合则返回错误.
 675     * @author    zhaoyingnan    2015-11-03 21:55
 676     * @param    string        $sKey
 677     * @param    array        $arMember    被添加的元素的数组
 678     * @return    int            返回新成功添加到集合里元素的数量,不包括已经存在于集合中的元素.
 679     **/ 680publicfunction sadd($sKey, $arMember = array())
 681    {
 682if(!$sKey)returnFALSE;
 683$iCount = 0;
 684foreach($arMemberas$val)
 685        {
 686if(self::$redis->sadd($sKey, $val))
 687$iCount++;
 688        }
 689return$iCount;
 690    }
 691 692/**
 693     * 返回集合存储的key的基数 (集合元素的数量).
 694     * @author    zhaoyingnan    2015-11-03 22:09
 695     * @param    string        $sKey
 696     * @return    int
 697     **/ 698publicfunction scard($sKey)
 699    {
 700if(!$sKey)return 0;
 701return self::$redis->scard($sKey);
 702    }
 703 704/**
 705     * 返回一个集合与给定集合的差集的元素.
 706     * @author    zhaoyingnan    2015-11-03 22:13
 707     * @param    array        $arKey
 708     * @return    array
 709     **/ 710publicfunction sdiff($arKey)
 711    {
 712if(!$arKey)returnarray();
 713return self::$redis->sdiff($arKey);
 714    }
 715 716/**
 717     * 该命令类似于 SDIFF, 不同之处在于该命令不返回结果集,而是将结果存放在destination集合中.
 718     * 如果destination 已经存在, 则将其覆盖重写.
 719     * @author    zhaoyingnan    2015-11-04 10:16
 720     * @param    string        $sNewKey
 721     * @param    array        $arKey
 722     * @return    int            结果集元素的个数.
 723     **/ 724publicfunction sdiffstore($sNewKey, $arKey)
 725    {
 726if(!$arKey || !$sNewKey)return 0;
 727if($arResult = self::$redis->sdiff($arKey))
 728return$this->sadd($sNewKey, $arResult);
 729return 0;
 730    }
 731 732/**
 733     * 返回指定所有的集合的成员的交集.
 734     * @author    zhaoyingnan    2015-11-04 10:18
 735     * @param    array        $arKey
 736     * @return    array
 737     **/ 738publicfunction sinter($arKey)
 739    {
 740if(!$arKey)returnarray();
 741return self::$redis->sinter($arKey);
 742    }
 743 744/**
 745     * 这个命令与SINTER命令类似, 但是它并不是直接返回结果集,而是将结果保存在 destination集合中.
 746     * 如果destination 集合存在, 则会被重写.
 747     * @author    zhaoyingnan    2015-11-04 10:23
 748     * @param    string        $sNewKey
 749     * @param    array        $arKey
 750     * @return    int            结果集元素的个数.
 751     **/ 752publicfunction sinterstore($sNewKey, $arKey)
 753    {
 754if(!$arKey || !$sNewKey)return 0;
 755if($arResult = self::$redis->sinter($arKey))
 756return$this->sadd($sNewKey, $arResult);
 757return 0;
 758    }
 759 760/**
 761     * 返回成员 member 是否是存储的集合 key的成员.
 762     * @author    zhaoyingnan    2015-11-04 10:25
 763     * @param    string        $sKey
 764     * @param    string        $member
 765     * @return    int            如果member元素是集合key的成员,则返回1,如果member元素不是key的成员,或者集合key不存在,则返回0
 766     **/ 767publicfunction sismember($sKey, $member)
 768    {
 769if(!
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。