首頁 > 後端開發 > php教程 > PHP Redis

PHP Redis

WBOY
發布: 2016-07-29 09:12:10
原創
1201 人瀏覽過

<span> 1</span> <?php <span>if (!<span>defined</span>('BASEPATH')) <span>exit</span>('不允許直接腳本存取'<span>);
</span><span> 2</span><span>類</span><span> Myredis
</span><span>3</span><span>{
</span><span> 4</span><span>//</span><span>redis所有已知指令:
</span><span> 5</span><span> //追加,auth,bgrewriteaof,bgsave,bitcount,bitop,blpop,brpop,brpoplpush,client-kill,client-list,client-getname,client-setname,config-get,config-set,config -resetstat、dbsize、偵錯物件、偵錯段錯誤、decr、decrby、del、丟棄、轉儲、回顯、eval、evalsha、exec、存在、過期、expireat、flushall、flushdb、get、getbit、getrange、getset、 hdel ,hexists,hget, hgetall,hincrby,hincrbyfloat,hkeys,hlen,hmget,hmset,hset,hsetnx,hvals,incr,incrby,incrbyfloat,訊息,鍵,lastsave,lindex,linsert,llen,lush,lpushx,lushx, lrange ,lrem,lset,ltrim,mget,遷移,監視器,移動,mset,msetnx,多,物件,堅持,pexpire,pexpireat,pfadd,pfcount,pfmerge,ping,psetex,psubscribe,pttl,發佈,punsubscribe,退出,randomkey 、重新命名、renamenx、恢復、rpop、rpoplpush、rpush、rpushx、sadd、保存、scard、腳本存在、腳本刷新、腳本終止、腳本加載、sdiff、sdiffstore、選擇、設定、setbit、setex、setnx , setrange,shutdown,sinter,sinterstore,sismember,slaveof,slowlog,smembers,smove,排序,spop,srandmember,srem,strlen,訂閱,sunion,sunionstore,同步,時間,ttl,類型,取消訂閱,取消觀看,觀看, zadd ,zcard,zcount,zincrby,zinterstore,zrange,zrangebyscore,zrank,zrem,zremrangebyrank,zremrangebyscore,zrevrange,zrevrangebyscore,zrevrank,zscore,zunionstore,subsub,配置重寫,客戶端暫停,can, </span> 6<span></span> 7<span></span>私有<span></span>靜態<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',796)
</span><span> 10</span><span> {
</span><span> 11</span><span>$這個</span>->主機 = <span>$arConfig</span>['host'<span>];
</span><span> 12</span><span>$這個</span>->端口 = <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>->連接(<span>$這個</span>->主機,<span>$這個</span>-><span>埠);
</span><span> 15</span><span>回傳</span> self::<span>$redis</span><span>;
</span><span> 16</span><span> }
</span><span> 17</span><span> 18</span><span>public</span><span>函數</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>),Par
<span></span>21<span></span>}
<span></span> 22<span></span> 23<span></span>/*<span></span>******************** 關鍵***************** *** *<span></span>*/<span></span>24<span></span>25<span></span>/*<span></span>*
<span></span> 26<span></span> * 尋找所有符合給定模式模式的鍵。
<span></span> 27<span></span> * KEYS * 匹配資料庫中所有鍵 。
<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> * @作者 zhaoyingnan 2015-10-16 17:33
<span></span> 33<span></span> * @param string $sPattern 匹配模式
<span></span> 34<span></span> * @return 陣列
<span>*/</span> 35<span></span> *<span></span>*/<span></span> 36<span></span>公<span></span>功能<span>鍵(</span>$sPattern<span> = '*'</span>功能<span>鍵(</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>->Pats(</span>5s(<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>function</span> exists(<span>$sKey</span><span>function</span> exists(<span>$sKey</span><span>function</span> exists(<span>$sKey
</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></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($sKey003, 0
</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></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>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>function</span> pexpireat(<span>$sKey</span><span>function</span> pexpireat(<span>$sKey</span>, illi)
<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>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>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🎜🎜 * @param int $iPort 端口
🎜🎜 138🎜🎜 * @param string $sKey 要操作的 key
🎜🎜 139🎜🎜 * @param int $iDb 目標實例的資料庫的編號
🎜🎜 140🎜🎜 * @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>, </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>;
<span></span> 146<span></span>return<span> self::</span>$redis<span>->migrate(</span>$sHost<span>, </span>$iPort<span>, </span>$sHost<span>, </span>$iPort<span>, </span>$s
<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>function<span> move(</span>$sKey, , <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>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重新命名為new​​key,如果key與newkey相同,將回傳一個錯誤。如果newkey已經存在,則值將被覆寫。<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></span>function<span></span>rename<span>(</span><span>function</span><span>rename</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>$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>function<span> renamenx(</span>$sKey<span></span>function<span> renamenx(</span>$sKey<span></span>function<span> renamenx(</span>$sKey<span></span>function<span> renamenx(</span>$sKey<span></span>function<span> renamenx(</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>function<span> set(</span>$sKeyo, , <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🎜🎜return🎜 self::🎜$redis🎜->set(🎜$sKey🎜, 🎜$sValue🎜🎜);
🎜🎜 235🎜🎜 }
🎜🎜 236🎜🎜 237🎜🎜/*🎜🎜*
🎜🎜 238🎜🎜 * 設定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>function</span> setex(<span>$sKey</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>))) ) ) )) ));
</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></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>(</span>intval<span>(</span>$iMilliseconds:30);
<span></span> 264<span></span>return<span> self::</span>$redis<span>->psetex(</span>$sKey<span>, </span>$iMilliseconds<span>, </span>$sValue);
<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>function<span> getset(</span>$sKey<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>function</span> mset(<span>$arArray</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>function</span> msetnx(<span>$arArray</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>function<span> setnx(</span>$sKey<span>, Value</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><strong> 340</strong></span> * @return array redis回傳的是以數字為<span>索引</span>的數組,這裡傳回的是一個關聯數組
<span></span> 341<span></span> *<span></span>*/<span></span> 342<span></span>public<span></span>function<span> mget(</span>$arKey<span> = </span>function<span> mget(</span>$arKey<span> = </span>array<span>)</span>)<span> = </span>array<span>)</span>)<span> = </span>array<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🎜🎜 349🎜🎜/*🎜🎜*
🎜🎜 350🎜🎜 * 如果 key 已經存在,並且值為字串,那麼這個指令會把 value 追加到原來值(value)的結尾。
🎜🎜 351🎜🎜 * 如果 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>function</span> append(<span>$sKey</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>function<span> incrby(</span>$sKey<span>,Incre
</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(Keycop$s
<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> 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>function</span> decrby(<span>$sKey</span><span>function</span> decrby(<span>$sKey</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>function<span> getrange(</span>$5
<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>$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><span>function</span><span>strlen</span>(<span></span>function<span></span>strlen<span>(</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>*
🎜🎜 464🎜🎜 * 將所有指定的值插入到存於 key 的清單的頭部。如果 key 不存在,那麼在進行 push 操作前會建立一個空列表。 
🎜🎜 465🎜🎜 * 如果 key 對應的值不是一個 list 的話,那麼會回傳一個錯誤。
🎜🎜 466🎜🎜 * 可以使用一個指令把多個元素 push 進入列表,只需在指令末尾加上多個指定的參數。
🎜🎜 467🎜🎜 * 元素是從最左端的到最右端的、一個接一個被插入到 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>function</span> lpush(<span>$sKey</span><span>function</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>function<span> lpushx(</span>$sKey<span></span>function<span> lpushx(</span>$sKey<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 保存的不是一個列表,那麼會回傳一個錯誤。
<strong></strong> 500</span><span> * 可以使用一個指令把多個元素打入</span>佇列<span>,只需要在指令後面指定多個參數。
</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($sKey<span></span>function<span> rpush(
</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>function</span> rpushx(<span>$sKey</span><span>function</span> rp
<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><strong> * 傳回 key 對應的清單裡的元素的</strong>索引</span> index 的值
<span></span> 545<span><strong> * 下標是從0開始</strong>索引</span>的,所以 0 是表示第一個元素, 1 表示第二個元素,並以此類推。 
<span></span> 546<span><strong> * 負數</strong>索引<strong>用於指定從列表尾部開始</strong>索引</span>的元素。在這個方法下,-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><strong> * @param array $index key 對應的列表中的 index </strong>索引</span>
<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>
<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>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。
<strong></strong> 561</span><span> * 下標是從0開始</span>索引<span>的,所以 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>function</span> lset(<span>$sKey,$3, </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>🎜 578🎜🎜 * 當 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>$s </span>, <span></span>)
<span></span> 587<span></span> {
<span></span> 588<span></span>if<span>(!</span>$sKey<span> || !</span>$pivot<span> || !</span>$sValue<span>)</span>returnQSE<span></span>
<span></span> 589<span></span>$sPosion<span> = </span>in_array<span>(</span>$sPosion<span>, </span>array<span>('BEFORE', 'AFTER'))) </span>array<span>('BEFORE', 'AFTER'))) ? 🜎eper</span>('BEFORE', 'AFTER'))) ? 🜎eperper錶🜎
<span></span> 590<span></span>return<span> self::</span>$redis<span>->linsert(</span>$sKey<span>, </span>$sPosion<span>, </span>$pivot<span>, </span>$sPosion<span>, </span>$pivot<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>(p
<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>5)
<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>function</span> lrange(
<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><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>function</span> sadd(
<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>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>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>🎜);
🎜🎜 714🎜🎜 }</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>$sNew</span><span>function</span> sdiffstore(
<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>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>$s
<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>function<span> sismember(</span>$sKey<span>
</span><span> 768</span><span> {
</span><span> 769</span><span>if</span>(!</span>
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板