PHP Redis

WBOY
Release: 2016-07-29 09:12:10
Original
1201 people have browsed it

<span>   1</span> <?php <span>if</span> (!<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 to be detected
</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> * Set the expiration time of the key. If the key has expired, it will be automatically deleted. A key with an expiration time set is called volatile.
</span><span> 56</span><span> * You can re-update the expiration time of the key before it expires, or you can use the PERSIST command to delete the expiration time of the 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 lifetime (seconds)
</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> * This command is similar to the EXPIRE command, but it sets the key's survival time in milliseconds, not in seconds like the EXPIRE command.
</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 lifetime (seconds)
</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> * The function of EXPIREAT is similar to EXPIRE, both are used to set the survival time for key. The difference is that the time parameter accepted by the EXPIREAT command is 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 timestamp (seconds)
</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 This command is similar to the EXPIREAT command, but it sets the key's expiration unix timestamp in milliseconds instead of seconds like EXPIREAT.
</span><span> 96</span><span> * The function of EXPIREAT is similar to EXPIRE, both are used to set the survival time for key. The difference is that the time parameter accepted by the EXPIREAT command is 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 timestamp (milliseconds)
</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> * Returns the remaining survival time (TTL, time to live) of the given key in seconds.
</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 When key does not exist, return -2. When the key exists but no remaining lifetime is set, -1 is returned. Otherwise, returns the key's remaining time to live, in seconds.
</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> * This command is similar to the TTL command, but it returns the remaining lifetime of the key in milliseconds instead of seconds like the TTL command.
</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 When key does not exist, return -2. When the key exists but no remaining lifetime is set, -1 is returned. Otherwise, returns the remaining lifetime of the key in seconds.
</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> * Atomically transfer the key from the current instance to the specified database of the target instance. Once the transfer is successful, the key is guaranteed to appear on the target instance, and the key on the current instance will be deleted.
</span><span> 134</span><span> * This command is an atomic operation. When executed, it will block the two instances of migration until any of the following results occur: migration succeeds, migration fails, or times out.
</span><span> 135</span><span> * @author zhaoyingnan 2015-10-16 18:24
</span><span> 136</span><span> * @param string $sHost target ip
</span><span> 137</span><span> * @param int $iPort port
</span><span> 138</span><span> * @param string $sKey The key to be operated on
</span><span> 139</span><span> * @param int $iDb The number of the database of the target instance
</span><span> 140</span><span> * @param int $iTimeout The timeout parameter is in millisecond format and specifies the maximum interval between communication between the current instance and the target instance. This means that the operation does not have to be completed within timeout milliseconds, it just means that the time for data transmission cannot exceed this timeout number.</span><span> 141</span><span> * @return bool Note: When the specified key exists in the specified database of the target instance, FALS is returned. The key in the current instance has not been deleted, nor has it been moved to the target instance. The key on the target instance is still The original
</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> * Move the key of the current database to the given database db.
</span><span> 151</span><span> * If the current database (source database) and the given database (target database) have the given key with the same name, or the key does not exist in the current database, then MOVE has no effect.
</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 Move to the given database 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> * Remove the lifetime of a given key and convert this key from "volatile" (key with lifetime) to "persistent" (a key without lifetime and never expires).
</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 When the survival time is successfully removed, return 1. If the key does not exist or the key does not have a survival time set, return 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> * Return a random key from the current database.
</span><span> 177</span><span> * @author zhaoyingnan 2015-10-19 16:08
</span><span> 178</span><span> * @return string If the database does not have any key, return nil, otherwise return a random 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> * Rename key to newkey. If key is the same as newkey, an error will be returned. If newkey already exists, the value will be overwritten.</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 value after renaming
</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> * Rename key to newkey if and only if newkey does not exist. When key does not exist, an error is returned.
</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 value after renaming
</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> * Returns the type of value stored in 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 does not exist) string (string) list (list) set (set) zset (ordered set) hash (hash table)
</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> * Match key and value. If the key already exists, it will be overwritten, regardless of its type.
</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 corresponding value
</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> * Set the key corresponding to the string value, and set the key to timeout after the given seconds.</span><span> 239</span><span> * @author zhaoyingnan 2015-11-03 11:25
</span><span> 240</span><span> * @param string $sKey The key being operated on
</span><span> 241</span><span> * @param int $iSecond life cycle (seconds)
</span><span> 242</span><span> * @param string $sValue key corresponding 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> * Set the key corresponding to the string value, and set the key to timeout after the given milliseconds time.
</span><span> 254</span><span> * @author zhaoyingnan 2015-11-03 11:25
</span><span> 255</span><span> * @param string $sKey The key being operated on
</span><span> 256</span><span> * @param int $iMillSecond life cycle (milliseconds)
</span><span> 257</span><span> * @param string $sValue key corresponding 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> * Automatically map key to value and return the value corresponding to the original key. If the key exists but the corresponding value is not a string, an error is returned.
</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 The new value set
</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> * Correspond to the given keys to their corresponding values. MSET will replace the existing value with the new value, just like the ordinary SET command.
</span><span> 282</span><span> * If you don’t want to overwrite existing values, please see the command MSETNX.
</span><span> 283</span><span> * MSET is atomic, so all given keys are set at once. It is impossible for the client to see that some keys are updated while others remain unchanged.
</span><span> 284</span><span> * @author zhaoyingnan 2015-11-03 11:04
</span><span> 285</span><span> * @param array $arArray The associative array to be set
</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> * Correspond to the given keys to their corresponding values.
</span><span> 296</span><span> * As long as a key already exists, MSETNX will not perform a single operation.</span><span> 297</span><span> * Due to this feature, MSETNX can achieve that either all operations are successful or none is executed, which can be used to set different keys to represent different fields of a unique object.
</span><span> 298</span><span> * MSETNX is atomic, so all given keys are set at once. It is impossible for the client to see that some keys are updated while others remain unchanged.
</span><span> 299</span><span> * @author zhaoyingnan 2015-11-03 11:11
</span><span> 300</span><span> * @param array $arArray The associative array to be set
</span><span> 301</span><span> * @return bool TRUE All keys are set, FALSE No keys are set (at least one key exists)
</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> * If the key does not exist, set the string value corresponding to the key.
</span><span> 311</span><span> * In this case, the command is the same as SET. When the key already exists, no operation is performed.
</span><span> 312</span><span> * SETNX is "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 value
</span><span> 316</span><span> * @return bool TRUE key is set, FALSE key is not 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> * Returns the value of key. If the key does not exist, the special value nil is returned. If the value of the key is not a string, an error is returned because GET only processes values ​​of type string.
</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> * Returns the value of all specified keys. For each key that does not correspond to a string or does not exist, the special value nil is returned. Because of this, the operation never fails.
</span><span> 338</span><span> * @author zhaoyingnan 2015-11-03 10:55
</span><span> 339</span><span> * @param array $arKey Array of keys to be obtained
</span><span> 340</span><span> * @return array redis returns an array indexed by numbers <strong></strong>, and what is returned here is an associative array
</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> * If key already exists and the value is a string, then this command will append value to the end of the original value (value).
</span><span> 351</span><span> * If key does not exist, it will first create an empty string key and then perform an append operation. In this case, APPEND will be similar to the SET operation.</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 appended value
</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> * Add 1 to the number corresponding to the key. If the key does not exist, the value corresponding to the key will be set to 0 before the operation.
</span><span> 365</span><span> * If key has a value of the wrong type or is a string that cannot be represented as a number, an error is returned.
</span><span> 366</span><span> * This operation supports up to 64-bit signed integer numbers.
</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> * Add decrement to the number corresponding to the key. If the key does not exist, the key will be set to 0 before the operation.
</span><span> 379</span><span> * If the value type of key is wrong or is a string that cannot be represented as a number, an error will be returned. This operation supports up to 64-bit signed positive numbers.
</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 step value
</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> * Add decrement to the number corresponding to the key. If the key does not exist, the key will be set to 0 before the operation.
</span><span> 393</span><span> * If the value type of key is wrong or it is a string that cannot be represented as a number, an error will be returned. This operation supports up to 64-bit signed positive numbers.
</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 step value
</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> * Subtract 1 from the number corresponding to the key. If the key does not exist, the value corresponding to the key will be set to 0 before the operation.
</span><span> 407</span><span> * If key has a value of the wrong type or is a string that cannot be represented as a number, an error is returned. This operation supports up to 64-bit signed integers.</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> * Decrement the number corresponding to the key by decrement. If the key does not exist, the key will be set to 0 before the operation.
</span><span> 420</span><span> * If the value type of key is wrong or it is a string that cannot be represented as a number, an error will be returned. This operation supports up to 64-bit signed positive numbers.
</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 step value
</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> * This command was changed to GETRANGE, and is called SUBSTR in Redis versions less than 2.0. 
</span><span> 434</span><span> * Returns the substring of the string value corresponding to the key. This substring is determined by the start and end displacements (both are within the string).
</span><span> 435</span><span> * You can use negative displacement to represent the subscript starting from the end of the string. So -1 is the last character, -2 is the second to last character, and so on.
</span><span> 436</span><span> * When this function handles out-of-range requests, it limits the results to 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 starting position
</span><span> 440</span><span> * @pause int $iEnd end position
</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> * Returns the length of the string type value of key. If the key corresponds to a non-string type, an error is returned.
</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> * Insert all specified values ​​into the head of the list stored at key. If the key does not exist, an empty list will be created before the push operation. 
</span><span> 465</span><span> * If the value corresponding to key is not a list, an error will be returned.
</span><span> 466</span><span> * You can use one command to push multiple elements into the list, just add multiple specified parameters at the end of the command.
</span><span> 467</span><span> * Elements are inserted into the head of the list one by one from the leftmost to the rightmost.So for this command example LPUSH mylist a b c, the returned list is c as the first element, b as the second element, and a as the third element.
</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 The array of values ​​that needs to be pushed to key
</span><span> 471</span><span> * @return int The length of the list after the push operation.
</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> * Only when the key already exists and a list is stored, insert value into the head of the list under the key. 
</span><span> 483</span><span> * Contrary to LPUSH, no operation will be performed when the key does not exist.
</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 The array of values ​​that needs to be pushed to 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> * Insert all specified values ​​to the end of the list stored in key. If the key does not exist, an empty list will be created and then the push operation will be performed. 
</span><span> 499</span><span> * When key is not saved as a list, an error will be returned.
</span><span> 500</span><span> * You can use one command to put multiple elements into the <strong>queue</strong>, just specify multiple parameters after the command.
</span><span> 501</span><span> * Elements are inserted one after another from the end of the list from left to right. For example, the command RPUSH mylist a b c will return a list whose first element is a , the second element is b , and the third element is 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 The array of values ​​that needs to be pushed to key
</span><span> 505</span><span> * @return int The length of the list after the push operation.
</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> * Insert value value into the end of list key if and only if key exists and is a list. 
</span><span> 517</span><span> * Contrary to the RPUSH command, the RPUSHX command does nothing when the key does not exist.
</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 The array of values ​​that needs to be pushed to key
</span><span> 521</span><span> * @return int The length of the list after the push operation.</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> * Return the length of the list stored in key. 
</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 If key does not exist, it is regarded as an empty list and the returned length is 0. When the value stored in key is not a list, an error will be returned.
</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> * Returns the value of the <strong>index</strong> index of the element in the list corresponding to key
</span><span> 545</span><span> * The subscript starts from 0<strong>index</strong>, so 0 means the first element, 1 means the second element, and so on. 
</span><span> 546</span><span> * Negative <strong>index </strong> is used to specify the element starting <strong>index</strong> from the end of the list. In this approach, -1 represents the last element, -2 represents the penultimate element, and so on.
</span><span> 547</span><span> * When the value of the key position is not a list, an error will be returned.
</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 The index in the corresponding list <strong>Index</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> * Set the value of the list element at the index position to value.
</span><span> 561</span><span> * The subscript starts from 0<strong>index</strong>, so 0 means the first element, 1 means the second element, and so on. 
</span><span> 562</span><span> * An error will be returned when the index is out of range.
</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 The corresponding list is indexed.
</span><span> 566</span><span> * @param string $sValue The value to be set
</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> * Insert value into the list stored in key before or after the pivot value.
</span><span> 577</span><span> * When key does not exist, this list will be regarded as an empty list and no operations will occur.
</span><span> 578</span><span> * When the key exists but the saved list is not a list, an error will be returned.</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 the base value (BEFORE or AFTER)
</span><span> 582</span><span> * @param string $pivot The pivot value in the list
</span><span> 583</span><span> * @param string $sValue The inserted value
</span><span> 584</span><span> * @return mix The length of the list after the insertion operation, or -1 when the pivot value cannot be found.
</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> * Remove the first count elements with value value from the list stored in key. The count parameter affects this operation in several ways:
</span><span> 595</span><span> * count > 0: Remove elements with value from the beginning to the end.
</span><span> 596</span><span> * count < 0: Remove elements with value from the end to the beginning.
</span><span> 597</span><span> * count = 0: Remove all elements with value.
</span><span> 598</span><span> * For example, LREM list -2 "hello" will remove the last two occurrences of "hello" from the list stored in list.
</span><span> 599</span><span> * It should be noted that if the key does not exist in the list, it will be treated as an empty list, so when the key does not exist, this command will return 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: Remove elements with value from beginning to end. count < 0: Remove elements with value from the end to the beginning. count = 0: Remove all elements with value value.
</span><span> 603</span><span> * @param string $sValue The value to be deleted
</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 an existing list so that the list only contains the specified elements in the specified range.
</span><span> 615</span><span> * Both start and stop count from 0, where 0 is the first element in the list (header), 1 is the second element, and so on.
</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 The starting position within the specified range
</span><span> 619</span><span> * @param int $iEnd specifies the end position within the range
</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> * Returns the elements in the specified range stored in the list of key.
</span><span> 630</span><span> * The start and end offsets are both 0-based subscripts, that is, the first element of the list is 0 (the head of the list), the second element is 1, and so on. .</span><span> 631</span><span> * The offset can also be a negative number, which means the offset is counted from the end of the list. For example, -1 is the last element of the list, -2 is the second to last, and so on.
</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 starting position
</span><span> 635</span><span> * @param int $iEnd end position
</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> * Remove and return the first element of the list corresponding to key.
</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> * Remove and return the last element of the list stored at key.</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> * Add one or more specified member elements to the key of the collection.
</span><span> 672</span><span> * If one or more specified elements member already exist in the collection key, they will be ignored.
</span><span> 673</span><span> * If the collection key does not exist, create a new collection key and add the member element to the collection key.
</span><span> 674</span><span> * If the type of key is not a collection, an error will be returned.
</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 Array of elements to be added
</span><span> 678</span><span> * @return int Returns the number of newly successfully added elements to the collection, excluding elements that already exist in the collection.
</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> * Returns the cardinality of the key stored in the collection (the number of collection elements).
</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> * Returns the elements of the difference between a set and the given set.
</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> * This command is similar to SDIFF, except that this command does not return a result set, but stores the results in the destination collection.
</span><span> 718</span><span> * If destination already exists, overwrite it.
</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 The number of elements in the result set.
</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> * Returns the intersection of all members of the specified set.
</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> * This command is similar to the SINTER command, but it does not return the result set directly, but saves the results in the destination collection.
</span><span> 746</span><span> * If the destination collection exists, it will be overwritten.
</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 The number of elements in the result set.
</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> * Return whether member member is a member of the stored set 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 If the member element is a member of the set key, it returns 1. If the member element is not a member of the key, or the set key does not exist, it returns 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>(!
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template