/** * Store a value for a given key in the cache. * * This method does not check if there is a value available for the given key, may cause unexpected behavior if not. * Use has() to prevent this issue. * * @param string $key * @param array $tags * @param mixed $data */ public function set($key, array $tags, $data) { $key = $this->redis->prefix($key); $this->redis->set($key, $this->encoder->encode($data)); if (is_int($this->expirationTime) && $this->expirationTime > 0) { $this->redis->expire($key, $this->expirationTime); } foreach ($tags as $tag) { $this->redis->sadd($this->redis->prefix($tag), $key); } }
public function testSScan() { if (version_compare($this->version, "2.8.0", "lt")) { $this->markTestSkipped(); return; } $this->redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); $this->redis->del('set'); for ($i = 0; $i < 100; $i++) { $this->redis->sadd('set', "member:{$i}"); } // Scan all of them $it = NULL; while ($arr_keys = $this->redis->sscan('set', $it)) { $i -= count($arr_keys); foreach ($arr_keys as $str_mem) { $this->assertTrue(strpos($str_mem, 'member') !== FALSE); } } $this->assertEquals(0, $i); // Scan just ones with zero in them (0, 10, 20, 30, 40, 50, 60, 70, 80, 90) $it = NULL; $i_w_zero = 0; while ($arr_keys = $this->redis->sscan('set', $it, '*0*')) { $i_w_zero += count($arr_keys); } $this->assertEquals(10, $i_w_zero); }
public function testObject() { $this->redis->del('key'); $this->assertTrue($this->redis->object('encoding', 'key') === FALSE); $this->assertTrue($this->redis->object('refcount', 'key') === FALSE); $this->assertTrue($this->redis->object('idletime', 'key') === FALSE); $this->redis->set('key', 'value'); $this->assertTrue($this->redis->object('encoding', 'key') === "raw"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->lpush('key', 'value'); $this->assertTrue($this->redis->object('encoding', 'key') === "ziplist"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->sadd('key', 'value'); $this->assertTrue($this->redis->object('encoding', 'key') === "hashtable"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->sadd('key', 42); $this->redis->sadd('key', 1729); $this->assertTrue($this->redis->object('encoding', 'key') === "intset"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); $this->redis->del('key'); $this->redis->lpush('key', str_repeat('A', pow(10, 6))); // 1M elements, too big for a ziplist. $this->assertTrue($this->redis->object('encoding', 'key') === "linkedlist"); $this->assertTrue($this->redis->object('refcount', 'key') === 1); $this->assertTrue($this->redis->object('idletime', 'key') === 0); }
/** * Save the job * * @return $this */ public function save() { $this->emit('save'); $this->update(); $this->client->sadd('q:job:types', $this->injectors['type']); return $this; }
/** * Mark a worker as paused/active * * @since 0.0.1 * @param string $workerName Name of the paused worker * @param bool $paused Whether to mark the worker as paused or active */ public function setPausedWorker($workerName, $paused = true) { if ($paused) { $this->redis->sadd(self::$pausedWorkerKey, $workerName); } else { $this->redis->srem(self::$pausedWorkerKey, $workerName); } }
/** * Adds project and user to a sets that can be easily accessed */ private function _add_redis_set_members() { $this->redis->sadd($this->_project_namespace() . ":projects", $this->project); $this->redis->sadd($this->_user_namespace() . ":users", $this->user); }
<?php //$file = 'lock.lock'; //$fp = fopen($file,'r+'); //$flag1 = flock($fp,LOCK_EX); // // //$fp2 = fopen($file,'r+'); ////$flag2 = flock($fp2,LOCK_EX); ////var_dump($flag1,$flag2); /** * redis锁 */ $redis = new Redis(); $redis->conncec('127.0.0.1', 6379); $redis = new Redis(); $redis->conncec('127.0.0.1', 6379); for ($i = 0; $i < 3; ++$i) { if ($redis->isMember(1)) { sleep(1); } else { $redis->sadd('lock_stock', 1); break; } } //正常的扣库存流程了 $redis->srem('stock_lock', 1);
/** * {@inheritdoc} */ public function send(MessageInterface $message) { $this->redis->sadd($this->key, $this->serializer->serialize($message)); }
public function testEval() { if (version_compare($this->version, "2.5.0", "lt")) { $this->markTestSkipped(); } // Basic single line response tests $this->assertTrue(1 == $this->redis->eval('return 1')); $this->assertTrue(1.55 == $this->redis->eval("return '1.55'")); $this->assertTrue("hello, world" == $this->redis->eval("return 'hello, world'")); /* * Keys to be incorporated into lua results */ // Make a list $this->redis->del('mylist'); $this->redis->rpush('mylist', 'a'); $this->redis->rpush('mylist', 'b'); $this->redis->rpush('mylist', 'c'); // Make a set $this->redis->del('myset'); $this->redis->sadd('myset', 'd'); $this->redis->sadd('myset', 'e'); $this->redis->sadd('myset', 'f'); // Basic keys $this->redis->set('key1', 'hello, world'); $this->redis->set('key2', 'hello again!'); // Use a script to return our list, and verify its response $list = $this->redis->eval("return redis.call('lrange', 'mylist', 0, -1)"); $this->assertTrue($list === array('a', 'b', 'c')); // Use a script to return our set $set = $this->redis->eval("return redis.call('smembers', 'myset')"); $this->assertTrue($set == array('d', 'e', 'f')); // Test an empty MULTI BULK response $this->redis->del('not-any-kind-of-list'); $empty_resp = $this->redis->eval("return redis.call('lrange', 'not-any-kind-of-list', 0, -1)"); $this->assertTrue(is_array($empty_resp) && empty($empty_resp)); // Now test a nested reply $nested_script = "\n\t\t\treturn {\n\t\t\t\t1,2,3, {\n\t\t\t\t\tredis.call('get', 'key1'),\n\t\t\t\t\tredis.call('get', 'key2'),\n\t\t\t\t\tredis.call('lrange', 'not-any-kind-of-list', 0, -1),\n\t\t\t\t\t{\n\t\t\t\t\t\tredis.call('smembers','myset'),\n\t\t\t\t\t\tredis.call('lrange', 'mylist', 0, -1)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t"; $expected = array(1, 2, 3, array('hello, world', 'hello again!', array(), array(array('d', 'e', 'f'), array('a', 'b', 'c')))); // Now run our script, and check our values against each other $eval_result = $this->redis->eval($nested_script); $this->assertTrue(is_array($eval_result) && count($this->array_diff_recursive($eval_result, $expected)) == 0); /* * Nested reply wihin a multi/pipeline block */ $num_scripts = 10; foreach (array(Redis::PIPELINE, Redis::MULTI) as $mode) { $this->redis->multi($mode); for ($i = 0; $i < $num_scripts; $i++) { $this->redis->eval($nested_script); } $replies = $this->redis->exec(); foreach ($replies as $reply) { $this->assertTrue(is_array($reply) && count($this->array_diff_recursive($reply, $expected)) == 0); } } /* * KEYS/ARGV */ $args_script = "return {KEYS[1],KEYS[2],KEYS[3],ARGV[1],ARGV[2],ARGV[3]}"; $args_args = array('k1', 'k2', 'k3', 'v1', 'v2', 'v3'); $args_result = $this->redis->eval($args_script, $args_args, 3); $this->assertTrue($args_result === $args_args); // turn on key prefixing $this->redis->setOption(Redis::OPT_PREFIX, 'prefix:'); $args_result = $this->redis->eval($args_script, $args_args, 3); // Make sure our first three are prefixed for ($i = 0; $i < count($args_result); $i++) { if ($i < 3) { // Should be prefixed $this->assertTrue($args_result[$i] == 'prefix:' . $args_args[$i]); } else { // Should not be prefixed $this->assertTrue($args_result[$i] == $args_args[$i]); } } }
public function add($id){ if(Source::exists($id)){ $this->files[] = $id; Redis::sadd("Repository.Source::{$this->id}", $id); } }
public function answer($questionID, $answerID, $userID) { Redis::sadd("Survey:{$this->id}:{$questionID}:{$answerID}", $userID); }
$redis->set('key', ""); // this is 0111 1111 $redis->getBit('key', 0); /* 0 */ $redis->getBit('key', 1); /* 1 */ $redis->set('key', "*"); // ord("*") = 42 = 0x2f = "0010 1010" $redis->setBit('key', 5, 1); /* returns 0 */ $redis->setBit('key', 7, 1); /* returns 0 */ $redis->get('key'); /* chr(0x2f) = "/" = b("0010 1111") */ $redis->delete('s'); $redis->sadd('s', 5); $redis->sadd('s', 4); $redis->sadd('s', 2); $redis->sadd('s', 1); $redis->sadd('s', 3); var_dump($redis->sort('s')); // 1,2,3,4,5 var_dump($redis->sort('s', array('sort' => 'desc'))); // 5,4,3,2,1 var_dump($redis->sort('s', array('sort' => 'desc', 'store' => 'out'))); // (int)5 $redis->persist('key'); function psubscribe($redis, $pattern, $chan, $msg) { echo "Pattern: {$pattern}\n"; echo "Channel: {$chan}\n";
$redis->lpush("list", $i); } $redis->lpop("list"); $redis->rpop("list"); echo $redis->lsize("list"); echo $redis->lget("list", 0); echo $redis->lset("list", 1, "new_value"); $data = $redis->lRange("list", 0, -1); echo "<pre>"; print_r($data); $bool = $redis->ltrim("list", 0, 5); echo $redis->lrem("list", "5"); $bool = $redis->rpoplpush("srcKey", "dstKey"); // SET for ($i = 0; $i < 10; $i++) { $redis->sadd("myset", $i + rand(10, 99)); } $bool = $redis->srem("myset", 16); 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>";
$redis->lpush('test', 'c'); $redis->rpush('test', 'a'); print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b [2] => a [3] => a ) var_dump($redis->lremove('test', 'a', 2)); //结果:int(2) print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b ) /** sadd 描述:为一个Key添加一个值。如果这个值已经在这个Key中,则返回FALSE。 参数:key value 返回值:成功返回true,失败false */ $redis->delete('test'); var_dump($redis->sadd('test', '111')); //结果:bool(true) var_dump($redis->sadd('test', '333')); //结果:bool(true) print_r($redis->sort('test')); //结果:Array ( [0] => 111 [1] => 333 ) /** sremove 描述:删除Key中指定的value值 参数:key member 返回值:true or false */ $redis->delete('test'); $redis->sadd('test', '111');
public function addSurvey($surveyID) { if (!$this->exists()) { return; } Redis::sadd("User:{$this->id}:surveys", $surveyID); }