Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP Redis

PHP Redis

WBOY
WBOYasal
2016-07-29 09:12:101136semak imbas

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