public function save() { if (!$this->id) { return false; } $args = Util::array_to_multibulk($this->encodeFields()); $args['id']; call_user_func_array(array('Redis', 'hmset'), array_merge(array($this->key()), $args)); Redis::zadd(get_class($this), microtime(true), $this->id); }
/** * Change state * * @param $state * @return $this */ public function state($state) { $this->emit($state); $this->removeState(); // Keep "FIFO!" $score = $this->injectors['timing'] + $this->injectors['priority']; $this->set('state', $state); $this->client->zadd('q:jobs', $score, $this->injectors['id']); $this->client->zadd('q:jobs:' . $state, $score, $this->injectors['id']); $this->client->zadd('q:jobs:' . $this->injectors['type'] . ':' . $state, $score, $this->injectors['id']); // Set inactive job to waiting list if ($this->queue->originalMode() && 'inactive' == $state) { $this->client->lpush('q:' . $this->injectors['type'] . ':jobs', 1); } $this->set('updated_at', Util::now()); return $this; }
protected function sequence($mode) { $ret = $this->redis->multi($mode)->set('x', 42)->info()->type('x')->get('x')->exec(); $this->assertTrue(is_array($ret)); $i = 0; $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue(is_array($ret[$i++])); $this->assertTrue($ret[$i++] === Redis::REDIS_STRING); $this->assertTrue($ret[$i] === '42' || $ret[$i] === 42); $serializer = $this->redis->getOption(Redis::OPT_SERIALIZER); $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // testing incr, which doesn't work with the serializer $ret = $this->redis->multi($mode)->delete('key1')->set('key1', 'value1')->get('key1')->getSet('key1', 'value2')->get('key1')->set('key2', 4)->incr('key2')->get('key2')->decr('key2')->get('key2')->renameKey('key2', 'key3')->get('key3')->renameNx('key3', 'key1')->renameKey('key3', 'key2')->incr('key2', 5)->get('key2')->decr('key2', 5)->get('key2')->exec(); $this->assertTrue(is_array($ret)); $i = 0; $this->assertTrue(is_long($ret[$i++])); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 'value1'); $this->assertTrue($ret[$i++] == 'value1'); $this->assertTrue($ret[$i++] == 'value2'); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 5); $this->assertTrue($ret[$i++] == 5); $this->assertTrue($ret[$i++] == 4); $this->assertTrue($ret[$i++] == 4); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 4); $this->assertTrue($ret[$i++] == FALSE); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 9); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 4); $this->assertTrue(count($ret) == $i); $this->redis->setOption(Redis::OPT_SERIALIZER, $serializer); $ret = $this->redis->multi($mode)->delete('key1')->delete('key2')->set('key1', 'val1')->setnx('key1', 'valX')->setnx('key2', 'valX')->exists('key1')->exists('key3')->ping()->exec(); $this->assertTrue(is_array($ret)); $this->assertTrue($ret[0] == TRUE); $this->assertTrue($ret[1] == TRUE); $this->assertTrue($ret[2] == TRUE); $this->assertTrue($ret[3] == FALSE); $this->assertTrue($ret[4] == TRUE); $this->assertTrue($ret[5] == TRUE); $this->assertTrue($ret[6] == FALSE); $this->assertTrue($ret[7] == '+PONG'); $ret = $this->redis->multi($mode)->randomKey()->exec(); $ret = $this->redis->multi($mode)->exec(); $this->assertTrue($ret == array()); // ttl, mget, mset, msetnx, expire, expireAt $this->redis->delete('key'); $ret = $this->redis->multi($mode)->ttl('key')->mget(array('key1', 'key2', 'key3'))->mset(array('key3' => 'value3', 'key4' => 'value4'))->set('key', 'value')->expire('key', 5)->ttl('key')->expireAt('key', '0000')->exec(); $this->assertTrue(is_array($ret)); $i = 0; $ttl = $ret[$i++]; $this->assertTrue($ttl === -1 || $ttl === -2); $this->assertTrue($ret[$i++] === array('val1', 'valX', FALSE)); // mget $this->assertTrue($ret[$i++] === TRUE); // mset $this->assertTrue($ret[$i++] === TRUE); // set $this->assertTrue($ret[$i++] === TRUE); // expire $this->assertTrue($ret[$i++] === 5); // ttl $this->assertTrue($ret[$i++] === TRUE); // expireAt $this->assertTrue(count($ret) == $i); $ret = $this->redis->multi($mode)->set('lkey', 'x')->set('lDest', 'y')->delete('lkey', 'lDest')->rpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->rpoplpush('lkey', 'lDest')->lGetRange('lDest', 0, -1)->lpop('lkey')->llen('lkey')->lRemove('lkey', 'lvalue', 3)->llen('lkey')->lget('lkey', 0)->lGetRange('lkey', 0, -1)->lSet('lkey', 1, "newValue")->lGetRange('lkey', 0, -1)->llen('lkey')->exec(); $this->assertTrue(is_array($ret)); $i = 0; $this->assertTrue($ret[$i++] === TRUE); // SET $this->assertTrue($ret[$i++] === TRUE); // SET $this->assertTrue($ret[$i++] === 2); // deleting 2 keys $this->assertTrue($ret[$i++] === 1); // rpush, now 1 element $this->assertTrue($ret[$i++] === 2); // lpush, now 2 elements $this->assertTrue($ret[$i++] === 3); // lpush, now 3 elements $this->assertTrue($ret[$i++] === 4); // lpush, now 4 elements $this->assertTrue($ret[$i++] === 5); // lpush, now 5 elements $this->assertTrue($ret[$i++] === 6); // lpush, now 6 elements $this->assertTrue($ret[$i++] === 'lvalue'); // rpoplpush returns the element: "lvalue" $this->assertTrue($ret[$i++] === array('lvalue')); // lDest contains only that one element. $this->assertTrue($ret[$i++] === 'lvalue'); // removing a second element from lkey, now 4 elements left ↓ $this->assertTrue($ret[$i++] === 4); // 4 elements left, after 2 pops. $this->assertTrue($ret[$i++] === 3); // removing 3 elements, now 1 left. $this->assertTrue($ret[$i++] === 1); // 1 element left $this->assertTrue($ret[$i++] === "lvalue"); // this is the current head. $this->assertTrue($ret[$i++] === array("lvalue")); // this is the current list. $this->assertTrue($ret[$i++] === FALSE); // updating a non-existent element fails. $this->assertTrue($ret[$i++] === array("lvalue")); // this is the current list. $this->assertTrue($ret[$i++] === 1); // 1 element left $this->assertTrue(count($ret) == $i); $ret = $this->redis->multi(Redis::PIPELINE)->delete('lkey', 'lDest')->rpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->rpoplpush('lkey', 'lDest')->lGetRange('lDest', 0, -1)->lpop('lkey')->exec(); $this->assertTrue(is_array($ret)); $i = 0; $this->assertTrue($ret[$i++] <= 2); // deleted 0, 1, or 2 items $this->assertTrue($ret[$i++] === 1); // 1 element in the list $this->assertTrue($ret[$i++] === 2); // 2 elements in the list $this->assertTrue($ret[$i++] === 3); // 3 elements in the list $this->assertTrue($ret[$i++] === 'lvalue'); // rpoplpush returns the element: "lvalue" $this->assertTrue($ret[$i++] === array('lvalue')); // rpoplpush returns the element: "lvalue" $this->assertTrue($ret[$i++] === 'lvalue'); // pop returns the front element: "lvalue" $this->assertTrue(count($ret) == $i); // general command $ret = $this->redis->multi($mode)->select(3)->set('keyAAA', 'value')->set('keyAAB', 'value')->dbSize()->lastsave()->exec(); $this->redis->select(0); // back to normal $i = 0; $this->assertTrue(is_array($ret)); $this->assertTrue($ret[$i++] === TRUE); // select $this->assertTrue($ret[$i++] === TRUE); // set $this->assertTrue($ret[$i++] === TRUE); // set $this->assertTrue(is_long($ret[$i++])); // dbsize $this->assertTrue(is_long($ret[$i++])); // lastsave $this->assertTrue(count($ret) === $i); $serializer = $this->redis->getOption(Redis::OPT_SERIALIZER); $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // testing incr, which doesn't work with the serializer $ret = $this->redis->multi($mode)->delete('key1')->set('key1', 'value1')->get('key1')->getSet('key1', 'value2')->get('key1')->set('key2', 4)->incr('key2')->get('key2')->decr('key2')->get('key2')->renameKey('key2', 'key3')->get('key3')->renameNx('key3', 'key1')->renameKey('key3', 'key2')->incr('key2', 5)->get('key2')->decr('key2', 5)->get('key2')->exec(); $i = 0; $this->assertTrue(is_array($ret)); $this->assertTrue(is_long($ret[$i]) && $ret[$i] <= 1); $i++; $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 'value1'); $this->assertTrue($ret[$i++] == 'value1'); $this->assertTrue($ret[$i++] == 'value2'); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 5); $this->assertTrue($ret[$i++] == 5); $this->assertTrue($ret[$i++] == 4); $this->assertTrue($ret[$i++] == 4); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 4); $this->assertTrue($ret[$i++] == FALSE); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 9); $this->assertTrue($ret[$i++] == TRUE); $this->assertTrue($ret[$i++] == 4); $this->redis->setOption(Redis::OPT_SERIALIZER, $serializer); $ret = $this->redis->multi($mode)->delete('key1')->delete('key2')->set('key1', 'val1')->setnx('key1', 'valX')->setnx('key2', 'valX')->exists('key1')->exists('key3')->ping()->exec(); $this->assertTrue(is_array($ret)); $this->assertTrue($ret[0] == TRUE); $this->assertTrue($ret[1] == TRUE); $this->assertTrue($ret[2] == TRUE); $this->assertTrue($ret[3] == FALSE); $this->assertTrue($ret[4] == TRUE); $this->assertTrue($ret[5] == TRUE); $this->assertTrue($ret[6] == FALSE); $this->assertTrue($ret[7] == '+PONG'); $ret = $this->redis->multi($mode)->randomKey()->exec(); $this->assertTrue(is_array($ret) && count($ret) === 1); $this->assertTrue(is_string($ret[0])); // ttl, mget, mset, msetnx, expire, expireAt $ret = $this->redis->multi($mode)->ttl('key')->mget(array('key1', 'key2', 'key3'))->mset(array('key3' => 'value3', 'key4' => 'value4'))->set('key', 'value')->expire('key', 5)->ttl('key')->expireAt('key', '0000')->exec(); $i = 0; $this->assertTrue(is_array($ret)); $this->assertTrue(is_long($ret[$i++])); $this->assertTrue(is_array($ret[$i]) && count($ret[$i]) === 3); // mget $i++; $this->assertTrue($ret[$i++] === TRUE); // mset always returns TRUE $this->assertTrue($ret[$i++] === TRUE); // set always returns TRUE $this->assertTrue($ret[$i++] === TRUE); // expire always returns TRUE $this->assertTrue($ret[$i++] === 5); // TTL was just set. $this->assertTrue($ret[$i++] === TRUE); // expireAt returns TRUE for an existing key $this->assertTrue(count($ret) === $i); // lists $ret = $this->redis->multi($mode)->delete('lkey', 'lDest')->rpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->lpush('lkey', 'lvalue')->rpoplpush('lkey', 'lDest')->lGetRange('lDest', 0, -1)->lpop('lkey')->llen('lkey')->lRemove('lkey', 'lvalue', 3)->llen('lkey')->lget('lkey', 0)->lGetRange('lkey', 0, -1)->lSet('lkey', 1, "newValue")->lGetRange('lkey', 0, -1)->llen('lkey')->exec(); $this->assertTrue(is_array($ret)); $i = 0; $this->assertTrue($ret[$i] >= 0 && $ret[$i] <= 2); // delete $i++; $this->assertTrue($ret[$i++] === 1); // 1 value $this->assertTrue($ret[$i++] === 2); // 2 values $this->assertTrue($ret[$i++] === 3); // 3 values $this->assertTrue($ret[$i++] === 4); // 4 values $this->assertTrue($ret[$i++] === 5); // 5 values $this->assertTrue($ret[$i++] === 6); // 6 values $this->assertTrue($ret[$i++] === 'lvalue'); $this->assertTrue($ret[$i++] === array('lvalue')); // 1 value only in lDest $this->assertTrue($ret[$i++] === 'lvalue'); // now 4 values left $this->assertTrue($ret[$i++] === 4); $this->assertTrue($ret[$i++] === 3); // removing 3 elements. $this->assertTrue($ret[$i++] === 1); // length is now 1 $this->assertTrue($ret[$i++] === 'lvalue'); // this is the head $this->assertTrue($ret[$i++] === array('lvalue')); // 1 value only in lkey $this->assertTrue($ret[$i++] === FALSE); // can't set list[1] if we only have a single value in it. $this->assertTrue($ret[$i++] === array('lvalue')); // the previous error didn't touch anything. $this->assertTrue($ret[$i++] === 1); // the previous error didn't change the length $this->assertTrue(count($ret) === $i); // sets $ret = $this->redis->multi($mode)->delete('skey1', 'skey2', 'skeydest', 'skeyUnion', 'sDiffDest')->sadd('skey1', 'sValue1')->sadd('skey1', 'sValue2')->sadd('skey1', 'sValue3')->sadd('skey1', 'sValue4')->sadd('skey2', 'sValue1')->sadd('skey2', 'sValue2')->sSize('skey1')->sRemove('skey1', 'sValue2')->sSize('skey1')->sMove('skey1', 'skey2', 'sValue4')->sSize('skey2')->sContains('skey2', 'sValue4')->sMembers('skey1')->sMembers('skey2')->sInter('skey1', 'skey2')->sInterStore('skeydest', 'skey1', 'skey2')->sMembers('skeydest')->sUnion('skey2', 'skeydest')->sUnionStore('skeyUnion', 'skey2', 'skeydest')->sMembers('skeyUnion')->sDiff('skey1', 'skey2')->sDiffStore('sDiffDest', 'skey1', 'skey2')->sMembers('sDiffDest')->sPop('skey2')->sSize('skey2')->exec(); $i = 0; $this->assertTrue(is_array($ret)); $this->assertTrue(is_long($ret[$i]) && $ret[$i] >= 0 && $ret[$i] <= 5); $i++; // deleted at most 5 values. $this->assertTrue($ret[$i++] === 1); // skey1 now has 1 element. $this->assertTrue($ret[$i++] === 1); // skey1 now has 2 elements. $this->assertTrue($ret[$i++] === 1); // skey1 now has 3 elements. $this->assertTrue($ret[$i++] === 1); // skey1 now has 4 elements. $this->assertTrue($ret[$i++] === 1); // skey2 now has 1 element. $this->assertTrue($ret[$i++] === 1); // skey2 now has 2 elements. $this->assertTrue($ret[$i++] === 4); $this->assertTrue($ret[$i++] === 1); // we did remove that value. $this->assertTrue($ret[$i++] === 3); // now 3 values only. $this->assertTrue($ret[$i++] === TRUE); // the move did succeed. $this->assertTrue($ret[$i++] === 3); // sKey2 now has 3 values. $this->assertTrue($ret[$i++] === TRUE); // sKey2 does contain sValue4. foreach (array('sValue1', 'sValue3') as $k) { // sKey1 contains sValue1 and sValue3. $this->assertTrue(in_array($k, $ret[$i])); } $this->assertTrue(count($ret[$i++]) === 2); foreach (array('sValue1', 'sValue2', 'sValue4') as $k) { // sKey2 contains sValue1, sValue2, and sValue4. $this->assertTrue(in_array($k, $ret[$i])); } $this->assertTrue(count($ret[$i++]) === 3); $this->assertTrue($ret[$i++] === array('sValue1')); // intersection $this->assertTrue($ret[$i++] === 1); // intersection + store → 1 value in the destination set. $this->assertTrue($ret[$i++] === array('sValue1')); // sinterstore destination contents foreach (array('sValue1', 'sValue2', 'sValue4') as $k) { // (skeydest U sKey2) contains sValue1, sValue2, and sValue4. $this->assertTrue(in_array($k, $ret[$i])); } $this->assertTrue(count($ret[$i++]) === 3); // union size $this->assertTrue($ret[$i++] === 3); // unionstore size foreach (array('sValue1', 'sValue2', 'sValue4') as $k) { // (skeyUnion) contains sValue1, sValue2, and sValue4. $this->assertTrue(in_array($k, $ret[$i])); } $this->assertTrue(count($ret[$i++]) === 3); // skeyUnion size $this->assertTrue($ret[$i++] === array('sValue3')); // diff skey1, skey2 : only sValue3 is not shared. $this->assertTrue($ret[$i++] === 1); // sdiffstore size == 1 $this->assertTrue($ret[$i++] === array('sValue3')); // contents of sDiffDest $this->assertTrue(in_array($ret[$i++], array('sValue1', 'sValue2', 'sValue4'))); // we removed an element from sKey2 $this->assertTrue($ret[$i++] === 2); // sKey2 now has 2 elements only. $this->assertTrue(count($ret) === $i); // sorted sets $ret = $this->redis->multi($mode)->delete('zkey1', 'zkey2', 'zkey5', 'zInter', 'zUnion')->zadd('zkey1', 1, 'zValue1')->zadd('zkey1', 5, 'zValue5')->zadd('zkey1', 2, 'zValue2')->zRange('zkey1', 0, -1)->zDelete('zkey1', 'zValue2')->zRange('zkey1', 0, -1)->zadd('zkey1', 11, 'zValue11')->zadd('zkey1', 12, 'zValue12')->zadd('zkey1', 13, 'zValue13')->zadd('zkey1', 14, 'zValue14')->zadd('zkey1', 15, 'zValue15')->zDeleteRangeByScore('zkey1', 11, 13)->zrange('zkey1', 0, -1)->zReverseRange('zkey1', 0, -1)->zRangeByScore('zkey1', 1, 6)->zCard('zkey1')->zScore('zkey1', 'zValue15')->zadd('zkey2', 5, 'zValue5')->zadd('zkey2', 2, 'zValue2')->zInter('zInter', array('zkey1', 'zkey2'))->zRange('zkey1', 0, -1)->zRange('zkey2', 0, -1)->zRange('zInter', 0, -1)->zUnion('zUnion', array('zkey1', 'zkey2'))->zRange('zUnion', 0, -1)->zadd('zkey5', 5, 'zValue5')->zIncrBy('zkey5', 3, 'zValue5')->zScore('zkey5', 'zValue5')->zScore('zkey5', 'unknown')->exec(); $i = 0; $this->assertTrue(is_array($ret)); $this->assertTrue(is_long($ret[$i]) && $ret[$i] >= 0 && $ret[$i] <= 5); $i++; // deleting at most 5 keys $this->assertTrue($ret[$i++] === 1); $this->assertTrue($ret[$i++] === 1); $this->assertTrue($ret[$i++] === 1); $this->assertTrue($ret[$i++] === array('zValue1', 'zValue2', 'zValue5')); $this->assertTrue($ret[$i++] === 1); $this->assertTrue($ret[$i++] === array('zValue1', 'zValue5')); $this->assertTrue($ret[$i++] === 1); // adding zValue11 $this->assertTrue($ret[$i++] === 1); // adding zValue12 $this->assertTrue($ret[$i++] === 1); // adding zValue13 $this->assertTrue($ret[$i++] === 1); // adding zValue14 $this->assertTrue($ret[$i++] === 1); // adding zValue15 $this->assertTrue($ret[$i++] === 3); // deleted zValue11, zValue12, zValue13 $this->assertTrue($ret[$i++] === array('zValue1', 'zValue5', 'zValue14', 'zValue15')); $this->assertTrue($ret[$i++] === array('zValue15', 'zValue14', 'zValue5', 'zValue1')); $this->assertTrue($ret[$i++] === array('zValue1', 'zValue5')); $this->assertTrue($ret[$i++] === 4); // 4 elements $this->assertTrue($ret[$i++] === 15.0); $this->assertTrue($ret[$i++] === 1); // added value $this->assertTrue($ret[$i++] === 1); // added value $this->assertTrue($ret[$i++] === 1); // zinter only has 1 value $this->assertTrue($ret[$i++] === array('zValue1', 'zValue5', 'zValue14', 'zValue15')); // zkey1 contents $this->assertTrue($ret[$i++] === array('zValue2', 'zValue5')); // zkey2 contents $this->assertTrue($ret[$i++] === array('zValue5')); // zinter contents $this->assertTrue($ret[$i++] === 5); // zUnion has 5 values (1,2,5,14,15) $this->assertTrue($ret[$i++] === array('zValue1', 'zValue2', 'zValue5', 'zValue14', 'zValue15')); // zunion contents $this->assertTrue($ret[$i++] === 1); // added value to zkey5, with score 5 $this->assertTrue($ret[$i++] === 8.0); // incremented score by 3 → it is now 8. $this->assertTrue($ret[$i++] === 8.0); // current score is 8. $this->assertTrue($ret[$i++] === FALSE); // score for unknown element. $this->assertTrue(count($ret) === $i); // hash $ret = $this->redis->multi($mode)->delete('hkey1')->hset('hkey1', 'key1', 'value1')->hset('hkey1', 'key2', 'value2')->hset('hkey1', 'key3', 'value3')->hmget('hkey1', array('key1', 'key2', 'key3'))->hget('hkey1', 'key1')->hlen('hkey1')->hdel('hkey1', 'key2')->hdel('hkey1', 'key2')->hexists('hkey1', 'key2')->hkeys('hkey1')->hvals('hkey1')->hgetall('hkey1')->hset('hkey1', 'valn', 1)->hset('hkey1', 'val-fail', 'non-string')->hget('hkey1', 'val-fail')->exec(); $i = 0; $this->assertTrue(is_array($ret)); $this->assertTrue($ret[$i++] <= 1); // delete $this->assertTrue($ret[$i++] === 1); // added 1 element $this->assertTrue($ret[$i++] === 1); // added 1 element $this->assertTrue($ret[$i++] === 1); // added 1 element $this->assertTrue($ret[$i++] === array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3')); // hmget, 3 elements $this->assertTrue($ret[$i++] === 'value1'); // hget $this->assertTrue($ret[$i++] === 3); // hlen $this->assertTrue($ret[$i++] === 1); // hdel succeeded $this->assertTrue($ret[$i++] === 0); // hdel failed $this->assertTrue($ret[$i++] === FALSE); // hexists didn't find the deleted key $this->assertTrue($ret[$i] === array('key1', 'key3') || $ret[$i] === array('key3', 'key1')); $i++; // hkeys $this->assertTrue($ret[$i] === array('value1', 'value3') || $ret[$i] === array('value3', 'value1')); $i++; // hvals $this->assertTrue($ret[$i] === array('key1' => 'value1', 'key3' => 'value3') || $ret[$i] === array('key3' => 'value3', 'key1' => 'value1')); $i++; // hgetall $this->assertTrue($ret[$i++] === 1); // added 1 element $this->assertTrue($ret[$i++] === 1); // added the element, so 1. $this->assertTrue($ret[$i++] === 'non-string'); // hset succeeded $this->assertTrue(count($ret) === $i); $ret = $this->redis->multi($mode)->delete('test')->set('test', 'xyz')->get('test')->exec(); $i = 0; $this->assertTrue(is_array($ret)); $this->assertTrue($ret[$i++] <= 1); // delete $this->assertTrue($ret[$i++] === TRUE); // added 1 element $this->assertTrue($ret[$i++] === 'xyz'); $this->assertTrue(count($ret) === $i); // GitHub issue 78 $this->redis->del('test'); for ($i = 1; $i <= 5; $i++) { $this->redis->zadd('test', $i, (string) $i); } $result = $this->redis->multi($mode)->zscore('test', "1")->zscore('test', "6")->zscore('test', "8")->zscore('test', "2")->exec(); $this->assertTrue($result === array(1.0, FALSE, FALSE, 2.0)); }
public function testZScan() { if (version_compare($this->version, "2.8.0", "lt")) { $this->markTestSkipped(); return; } $this->redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); $this->redis->del('zset'); $i_tot_score = 0; $i_p_score = 0; $i_p_count = 0; for ($i = 0; $i < 2000; $i++) { if ($i < 10) { $this->redis->zadd('zset', $i, "pmem:{$i}"); $i_p_score += $i; $i_p_count += 1; } else { $this->redis->zadd('zset', $i, "mem:{$i}"); } $i_tot_score += $i; } // Scan them all $it = NULL; while ($arr_keys = $this->redis->zscan('zset', $it)) { foreach ($arr_keys as $str_mem => $f_score) { $i_tot_score -= $f_score; $i--; } } $this->assertEquals(0, $i); $this->assertEquals(0, $i_tot_score); // Just scan "pmem" members $it = NULL; $i_p_score_old = $i_p_score; $i_p_count_old = $i_p_count; while ($arr_keys = $this->redis->zscan('zset', $it, "*pmem*")) { foreach ($arr_keys as $str_mem => $f_score) { $i_p_score -= $f_score; $i_p_count -= 1; } } $this->assertEquals(0, $i_p_score); $this->assertEquals(0, $i_p_count); // Turn off retrying and we should get some empty results $this->redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY); $i_skips = 0; $i_p_score = $i_p_score_old; $i_p_count = $i_p_count_old; $it = NULL; while (($arr_keys = $this->redis->zscan('zset', $it, "*pmem*")) !== FALSE) { if (count($arr_keys) == 0) { $i_skips++; } foreach ($arr_keys as $str_mem => $f_score) { $i_p_score -= $f_score; $i_p_count -= 1; } } // We should still get all the keys, just with several empty results $this->assertTrue($i_skips > 0); $this->assertEquals(0, $i_p_score); $this->assertEquals(0, $i_p_count); }
public function save(){ Redis::hmset("Repository::{$this->id}", 'name', json_encode($this->name), 'url', json_encode($this->url)); // Can't recommend an uploaded file to a user, so don't bother inserting // it into the ranking sets, and go ahead and remove the reference to it // at some point in the near future if($this->isUpload()){ Redis::expire("Repository::{$this->id}", UPLOAD_EXPIRE_TIME); return; } // If it is a repository, we want to insert it into the ranking sets foreach($this->features() as $type => $attributes){ foreach($attributes as $attribute => $value){ Redis::zadd("Match::{$type}::{$attribute}", $value, $this->id); } } }
public function testZX() { $this->redis->delete('key'); $this->assertTrue(array() === $this->redis->zRange('key', 0, -1)); $this->assertTrue(array() === $this->redis->zRange('key', 0, -1, true)); $this->assertTrue(1 === $this->redis->zAdd('key', 0, 'val0')); $this->assertTrue(1 === $this->redis->zAdd('key', 2, 'val2')); $this->assertTrue(1 === $this->redis->zAdd('key', 1, 'val1')); $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'val3')); $this->assertTrue(array('val0', 'val1', 'val2', 'val3') === $this->redis->zRange('key', 0, -1)); // withscores $ret = $this->redis->zRange('key', 0, -1, true); $this->assertTrue(count($ret) == 4); $this->assertTrue($ret['val0'] == 0); $this->assertTrue($ret['val1'] == 1); $this->assertTrue($ret['val2'] == 2); $this->assertTrue($ret['val3'] == 3); $this->assertTrue(0 === $this->redis->zDelete('key', 'valX')); $this->assertTrue(1 === $this->redis->zDelete('key', 'val3')); $this->assertTrue(array('val0', 'val1', 'val2') === $this->redis->zRange('key', 0, -1)); // zGetReverseRange $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'val3')); $this->assertTrue(1 === $this->redis->zAdd('key', 3, 'aal3')); $zero_to_three = $this->redis->zRangeByScore('key', 0, 3); $this->assertTrue(array('val0', 'val1', 'val2', 'aal3', 'val3') === $zero_to_three || array('val0', 'val1', 'val2', 'val3', 'aal3') === $zero_to_three); $this->assertTrue(5 === $this->redis->zCount('key', 0, 3)); // withscores $this->redis->zRemove('key', 'aal3'); $zero_to_three = $this->redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE)); $this->assertTrue(array('val0' => 0, 'val1' => 1, 'val2' => 2, 'val3' => 3) == $zero_to_three); $this->assertTrue(4 === $this->redis->zCount('key', 0, 3)); // limit $this->assertTrue(array('val0') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(0, 1)))); $this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(0, 2)))); $this->assertTrue(array('val1', 'val2') === $this->redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 2)))); $this->assertTrue(array('val0', 'val1') === $this->redis->zRangeByScore('key', 0, 1, array('limit' => array(0, 100)))); $this->assertTrue(4 === $this->redis->zSize('key')); $this->assertTrue(1.0 === $this->redis->zScore('key', 'val1')); $this->assertFalse($this->redis->zScore('key', 'val')); $this->assertFalse($this->redis->zScore(3, 2)); // zincrby $this->redis->delete('key'); $this->assertTrue(1.0 === $this->redis->zIncrBy('key', 1, 'val1')); $this->assertTrue(1.0 === $this->redis->zScore('key', 'val1')); $this->assertTrue(2.5 === $this->redis->zIncrBy('key', 1.5, 'val1')); $this->assertTrue(2.5 === $this->redis->zScore('key', 'val1')); //zUnion $this->redis->delete('key1'); $this->redis->delete('key2'); $this->redis->delete('key3'); $this->redis->delete('keyU'); $this->redis->zAdd('key1', 0, 'val0'); $this->redis->zAdd('key1', 1, 'val1'); $this->redis->zAdd('key2', 2, 'val2'); $this->redis->zAdd('key2', 3, 'val3'); $this->redis->zAdd('key3', 4, 'val4'); $this->redis->zAdd('key3', 5, 'val5'); $this->assertTrue(4 === $this->redis->zUnion('keyU', array('key1', 'key3'))); $this->assertTrue(array('val0', 'val1', 'val4', 'val5') === $this->redis->zRange('keyU', 0, -1)); // Union on non existing keys $this->redis->delete('keyU'); $this->assertTrue(0 === $this->redis->zUnion('keyU', array('X', 'Y'))); $this->assertTrue(array() === $this->redis->zRange('keyU', 0, -1)); // !Exist U Exist $this->redis->delete('keyU'); $this->assertTrue(2 === $this->redis->zUnion('keyU', array('key1', 'X'))); $this->assertTrue($this->redis->zRange('key1', 0, -1) === $this->redis->zRange('keyU', 0, -1)); // test weighted zUnion $this->redis->delete('keyZ'); $this->assertTrue(4 === $this->redis->zUnion('keyZ', array('key1', 'key2'), array(1, 1))); $this->assertTrue(array('val0', 'val1', 'val2', 'val3') === $this->redis->zRange('keyZ', 0, -1)); $this->redis->zDeleteRangeByScore('keyZ', 0, 10); $this->assertTrue(4 === $this->redis->zUnion('keyZ', array('key1', 'key2'), array(5, 1))); $this->assertTrue(array('val0', 'val2', 'val3', 'val1') === $this->redis->zRange('keyZ', 0, -1)); $this->redis->delete('key1'); $this->redis->delete('key2'); $this->redis->delete('key3'); // zInter $this->redis->zAdd('key1', 0, 'val0'); $this->redis->zAdd('key1', 1, 'val1'); $this->redis->zAdd('key1', 3, 'val3'); $this->redis->zAdd('key2', 2, 'val1'); $this->redis->zAdd('key2', 3, 'val3'); $this->redis->zAdd('key3', 4, 'val3'); $this->redis->zAdd('key3', 5, 'val5'); $this->redis->delete('keyI'); $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2'))); $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1)); // Union on non existing keys $this->assertTrue(0 === $this->redis->zInter('keyX', array('X', 'Y'))); $this->assertTrue(array() === $this->redis->zRange('keyX', 0, -1)); // !Exist U Exist $this->assertTrue(0 === $this->redis->zInter('keyY', array('key1', 'X'))); $this->assertTrue(array() === $this->redis->zRange('keyY', 0, -1)); // test weighted zInter $this->redis->delete('key1'); $this->redis->delete('key2'); $this->redis->delete('key3'); $this->redis->zAdd('key1', 0, 'val0'); $this->redis->zAdd('key1', 1, 'val1'); $this->redis->zAdd('key1', 3, 'val3'); $this->redis->zAdd('key2', 2, 'val1'); $this->redis->zAdd('key2', 1, 'val3'); $this->redis->zAdd('key3', 7, 'val1'); $this->redis->zAdd('key3', 3, 'val3'); $this->redis->delete('keyI'); $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2'), array(1, 1))); $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1)); $this->redis->delete('keyI'); $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2', 'key3'), array(1, 5, 1), 'min')); $this->assertTrue(array('val1', 'val3') === $this->redis->zRange('keyI', 0, -1)); $this->redis->delete('keyI'); $this->assertTrue(2 === $this->redis->zInter('keyI', array('key1', 'key2', 'key3'), array(1, 5, 1), 'max')); $this->assertTrue(array('val3', 'val1') === $this->redis->zRange('keyI', 0, -1)); // zrevrange $this->redis->delete('z'); $this->redis->zadd('z', 1, 'one'); $this->redis->zadd('z', 2, 'two'); $this->redis->zadd('z', 5, 'five'); $this->redis->zadd('z', 10, 'ten'); $this->assertTrue(array('ten') === $this->redis->zReverseRange('z', 0, 0)); $this->assertTrue(array('ten', 'five', 'two') === $this->redis->zReverseRange('z', 0, 2)); $this->assertTrue(array('five', 'two') === $this->redis->zReverseRange('z', 1, 2)); $this->assertTrue(array('five') === $this->redis->zReverseRange('z', 1, 1)); $this->assertTrue(array() === $this->redis->zReverseRange('z', 1, 0)); $this->assertTrue(array() === $this->redis->zReverseRange('z', -1, 0)); // zrank, zrevrank $this->redis->delete('z'); $this->redis->zadd('z', 1, 'one'); $this->redis->zadd('z', 2, 'two'); $this->redis->zadd('z', 5, 'five'); $this->assertTrue(0 === $this->redis->zRank('z', 'one')); $this->assertTrue(1 === $this->redis->zRank('z', 'two')); $this->assertTrue(2 === $this->redis->zRank('z', 'five')); $this->assertTrue(2 === $this->redis->zRevRank('z', 'one')); $this->assertTrue(1 === $this->redis->zRevRank('z', 'two')); $this->assertTrue(0 === $this->redis->zRevRank('z', 'five')); }
echo 'DB size: ' . $redis->dbSize() . PHP_EOL; echo '___________________________' . PHP_EOL; $p = array(); $keys = $redis->keys('*'); $d = ':'; $z = 'readmin:patterns'; foreach ($keys as $key) { $parts = explode($d, $key); $pk = ''; foreach ($parts as $key => $part) { if ($key == count($parts) - 1) { // Добавляем строки только первого уровня if (count($parts) == 1) { $pk .= $part; } } else { $pk .= $part . $d . '*'; } if (array_key_exists($pk, $p)) { $p[$pk] = $p[$pk] + 1; } else { $p[$pk] = 1; } } } // Добавляем шаблоны в редис $redis->del($z); foreach ($p as $key => $value) { $redis->zadd($z, $value, $key); } echo 'Time ago: ' . (microtime(true) - $start) . PHP_EOL;
echo (int) $bool; $bool = $redis->sMove("myset", "myset1", 35); echo $bool; $data = $redis->smembers("myset"); $bool = $redis->sismember("myset", 555); echo (int) $bool; echo $redis->scard("myset"); $redis->sinterstore("output", "myset", "myset1"); $data = $redis->smembers("output"); echo "<pre>"; // sort $data = $redis->sort("myset", array("sort" => "desc")); echo "<pre>"; print_r($data); for ($i = 0; $i < 10; $i++) { $redis->zadd("zset", $i + rand(10, 99), $i + rand(100, 999)); } $data = $redis->zrange("zset", 0, 3, "withscores"); echo "<pre>"; print_r($data); $redis->zrem("zset", 456); echo $redis->zcount("zset", 10, 50); $redis->zRemRangeByScore("key", star, end); echo $redis->zScore("zset", 503); echo $redis->zrank("zset", 723); for ($i = 0; $i < 10; $i++) { $redis->hset("myhash", $i, rand(10, 99) + $i); } echo $redis->hget("myhash", "0"); echo $redis->hlen("myhash"); echo $redis->hdel("myhash", "0");
<?php include 'Redis.php'; $redis = new Redis(); $redis->debug = true; $redis->nonempty = 'abv'; $redis->empty = ''; var_dump($redis->empty); var_dump($redis->unset); var_dump($redis->nonempty); // Test binary safety using UTF8 keys and data (in bulgarian) $redis->{'КИРИЛИЦА'} = "ДА"; var_dump($redis->{'КИРИЛИЦА'}); // Test __call var_dump($redis->zadd("zkey", 1, "one")); var_dump($redis->zadd("zkey", 2, "two")); var_dump($redis->zadd("zkey", 3, "three")); var_dump($redis->zcard("zkey")); var_dump($redis->zrevrank("zkey", "two")); // Pipeline test echo "---Pipeline test\n"; $s = microtime(true); $redis->pipeline_begin(); var_dump($redis->set("pipeline1", "val1")); var_dump($redis->set("pipeline2", "val2")); var_dump($redis->set("pipeline3", "val3")); var_dump($redis->set("pipeline4", "val4")); var_dump($redis->set("pipeline5", "val5")); var_dump($redis->get("pipeline2")); var_dump($redis->pipeline_responses()); echo "Time: " . (microtime(true) - $s) . "\n";