/** * @inheritdoc * * @see http://blog.apiaxle.com/post/storing-near-realtime-stats-in-redis/ */ public function increment($bucket, $step = 1) { $time = $this->syncedTime(); $granularities = $this->getGranularities(); foreach ($granularities as $granularity => $settings) { $key = $this->getKey($bucket, 'counts', $granularity, $settings, $time); $field = $this->getField($settings, $time); $this->redis->hIncrBy($key, $field, $step); $this->redis->expireAt($key, $time + $settings['ttl']); } $this->redis->sAdd('buckets', $bucket); $this->redis->sAdd(sprintf('types:%s', $bucket), 'counts'); }
public function testDifferentTypeSortedSet() { $key = '{hash}sortedset'; $dkey = '{hash}' . __FUNCTION__; $this->redis->del($key); $this->assertEquals(1, $this->redis->zAdd($key, 0, 'value')); // string I/F $this->assertEquals(FALSE, $this->redis->get($key)); $this->assertEquals(FALSE, $this->redis->getset($key, 'value2')); $this->assertEquals(FALSE, $this->redis->append($key, 'append')); $this->assertEquals(FALSE, $this->redis->getRange($key, 0, 8)); $this->assertEquals(array(FALSE), $this->redis->mget(array($key))); $this->assertEquals(FALSE, $this->redis->incr($key)); $this->assertEquals(FALSE, $this->redis->incrBy($key, 1)); $this->assertEquals(FALSE, $this->redis->decr($key)); $this->assertEquals(FALSE, $this->redis->decrBy($key, 1)); // lists I/F $this->assertEquals(FALSE, $this->redis->rPush($key, 'lvalue')); $this->assertEquals(FALSE, $this->redis->lPush($key, 'lvalue')); $this->assertEquals(FALSE, $this->redis->lLen($key)); $this->assertEquals(FALSE, $this->redis->lPop($key)); $this->assertEquals(FALSE, $this->redis->lrange($key, 0, -1)); $this->assertEquals(FALSE, $this->redis->lTrim($key, 0, 1)); $this->assertEquals(FALSE, $this->redis->lGet($key, 0)); $this->assertEquals(FALSE, $this->redis->lSet($key, 0, "newValue")); $this->assertEquals(FALSE, $this->redis->lrem($key, 'lvalue', 1)); $this->assertEquals(FALSE, $this->redis->lPop($key)); $this->assertEquals(FALSE, $this->redis->rPop($key)); $this->assertEquals(FALSE, $this->redis->rPoplPush($key, $dkey . 'lkey1')); // sets I/F $this->assertEquals(FALSE, $this->redis->sAdd($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->srem($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->sPop($key)); $this->assertEquals(FALSE, $this->redis->sMove($key, $dkey . 'skey1', 'sValue1')); $this->assertEquals(FALSE, $this->redis->scard($key)); $this->assertEquals(FALSE, $this->redis->sismember($key, 'sValue1')); $this->assertEquals(FALSE, $this->redis->sInter($key, $dkey . 'skey2')); $this->assertEquals(FALSE, $this->redis->sUnion($key, $dkey . 'skey4')); $this->assertEquals(FALSE, $this->redis->sDiff($key, $dkey . 'skey7')); $this->assertEquals(FALSE, $this->redis->sMembers($key)); $this->assertEquals(FALSE, $this->redis->sRandMember($key)); // hash I/F $this->assertEquals(FALSE, $this->redis->hSet($key, 'key1', 'value1')); $this->assertEquals(FALSE, $this->redis->hGet($key, 'key1')); $this->assertEquals(FALSE, $this->redis->hMGet($key, array('key1'))); $this->assertEquals(FALSE, $this->redis->hMSet($key, array('key1' => 'value1'))); $this->assertEquals(FALSE, $this->redis->hIncrBy($key, 'key2', 1)); $this->assertEquals(FALSE, $this->redis->hExists($key, 'key2')); $this->assertEquals(FALSE, $this->redis->hDel($key, 'key2')); $this->assertEquals(FALSE, $this->redis->hLen($key)); $this->assertEquals(FALSE, $this->redis->hKeys($key)); $this->assertEquals(FALSE, $this->redis->hVals($key)); $this->assertEquals(FALSE, $this->redis->hGetAll($key)); }
/** * 获取一个任务 * * @return bool|Caster */ private function getReadyJob() { $keyChip = $this->queue->name . ':' . Job::JOBS_TAB . ':' . $this->tube . ':'; $timePort = Util::now(); $this->expireActivate($keyChip, $timePort); if (!($id = $this->pop($keyChip . Job::STATE_READY, $timePort))) { return false; } if (!($job = Caster::reload($id))) { return false; } $this->client->hIncrBy($this->queue->name . ':' . Job::JOB_TAB . ':' . $id, 'retry_times', 1); $this->jobId = $id; $this->periodic = $this->client->hGet($this->queue->name . ':' . Job::JOB_TAB . ':' . $id, 'periodic'); return $job; }
/** * @param string $key * @param array $member * @param int $value * @return int */ public function hIncrBy($key, $member, $value) { return parent::hIncrBy($key, $member, $value); }
public function testHashes() { $this->redis->delete('h', 'key'); $this->assertTrue(0 === $this->redis->hLen('h')); $this->assertTrue(1 === $this->redis->hSet('h', 'a', 'a-value')); $this->assertTrue(1 === $this->redis->hLen('h')); $this->assertTrue(1 === $this->redis->hSet('h', 'b', 'b-value')); $this->assertTrue(2 === $this->redis->hLen('h')); $this->assertTrue('a-value' === $this->redis->hGet('h', 'a')); // simple get $this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get $this->assertTrue(0 === $this->redis->hSet('h', 'a', 'another-value')); // replacement $this->assertTrue('another-value' === $this->redis->hGet('h', 'a')); // get the new value $this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get $this->assertTrue(FALSE === $this->redis->hGet('h', 'c')); // unknown hash member $this->assertTrue(FALSE === $this->redis->hGet('key', 'c')); // unknownkey // hDel $this->assertTrue(1 === $this->redis->hDel('h', 'a')); // 1 on success $this->assertTrue(0 === $this->redis->hDel('h', 'a')); // 0 on failure $this->redis->delete('h'); $this->redis->hSet('h', 'x', 'a'); $this->redis->hSet('h', 'y', 'b'); $this->assertTrue(2 === $this->redis->hDel('h', 'x', 'y')); // variadic // hsetnx $this->redis->delete('h'); $this->assertTrue(TRUE === $this->redis->hSetNx('h', 'x', 'a')); $this->assertTrue(TRUE === $this->redis->hSetNx('h', 'y', 'b')); $this->assertTrue(FALSE === $this->redis->hSetNx('h', 'x', '?')); $this->assertTrue(FALSE === $this->redis->hSetNx('h', 'y', '?')); $this->assertTrue('a' === $this->redis->hGet('h', 'x')); $this->assertTrue('b' === $this->redis->hGet('h', 'y')); // keys $keys = $this->redis->hKeys('h'); $this->assertTrue($keys === array('x', 'y') || $keys === array('y', 'x')); // values $values = $this->redis->hVals('h'); $this->assertTrue($values === array('a', 'b') || $values === array('b', 'a')); // keys + values $all = $this->redis->hGetAll('h'); $this->assertTrue($all === array('x' => 'a', 'y' => 'b') || $all === array('y' => 'b', 'x' => 'a')); // hExists $this->assertTrue(TRUE === $this->redis->hExists('h', 'x')); $this->assertTrue(TRUE === $this->redis->hExists('h', 'y')); $this->assertTrue(FALSE === $this->redis->hExists('h', 'w')); $this->redis->delete('h'); $this->assertTrue(FALSE === $this->redis->hExists('h', 'x')); // hIncrBy $this->redis->delete('h'); $this->assertTrue(2 === $this->redis->hIncrBy('h', 'x', 2)); $this->assertTrue(3 === $this->redis->hIncrBy('h', 'x', 1)); $this->assertTrue(2 === $this->redis->hIncrBy('h', 'x', -1)); $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'x', "not-a-number")); $this->assertTrue("2" === $this->redis->hGet('h', 'x')); $this->redis->hSet('h', 'y', 'not-a-number'); $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'y', 1)); // hIncrByFloat $this->redis->delete('h'); $this->assertTrue(1.5 === $this->redis->hIncrByFloat('h', 'x', 1.5)); $this->assertTrue(3.0 === $this->redis->hincrByFloat('h', 'x', 1.5)); $this->assertTrue(1.5 === $this->redis->hincrByFloat('h', 'x', -1.5)); $this->redis->hset('h', 'y', 'not-a-number'); $this->assertTrue(FALSE === $this->redis->hIncrByFloat('h', 'y', 1.5)); // hmset $this->redis->delete('h'); $this->assertTrue(TRUE === $this->redis->hMset('h', array('x' => 123, 'y' => 456, 'z' => 'abc'))); $this->assertTrue('123' === $this->redis->hGet('h', 'x')); $this->assertTrue('456' === $this->redis->hGet('h', 'y')); $this->assertTrue('abc' === $this->redis->hGet('h', 'z')); $this->assertTrue(FALSE === $this->redis->hGet('h', 't')); // hmget $this->assertTrue(array('x' => '123', 'y' => '456') === $this->redis->hMget('h', array('x', 'y'))); $this->assertTrue(array('z' => 'abc') === $this->redis->hMget('h', array('z'))); $this->assertTrue(array('x' => '123', 't' => FALSE, 'y' => '456') === $this->redis->hMget('h', array('x', 't', 'y'))); $this->assertFalse(array(123 => 'x') === $this->redis->hMget('h', array(123))); $this->assertTrue(array(123 => FALSE) === $this->redis->hMget('h', array(123))); // hmget/hmset with numeric fields $this->redis->del('h'); $this->assertTrue(TRUE === $this->redis->hMset('h', array(123 => 'x', 'y' => 456))); $this->assertTrue('x' === $this->redis->hGet('h', 123)); $this->assertTrue('x' === $this->redis->hGet('h', '123')); $this->assertTrue('456' === $this->redis->hGet('h', 'y')); $this->assertTrue(array(123 => 'x', 'y' => '456') === $this->redis->hMget('h', array('123', 'y'))); // check non-string types. $this->redis->delete('h1'); $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('x' => 0, 'y' => array(), 'z' => new stdclass(), 't' => NULL))); $h1 = $this->redis->hGetAll('h1'); $this->assertTrue('0' === $h1['x']); $this->assertTrue('Array' === $h1['y']); $this->assertTrue('Object' === $h1['z']); $this->assertTrue('' === $h1['t']); }
/** * Decrement a raw value * * @param string $id Cache ID * @param int $offset Step/value to reduce by * @return mixed New value on success or FALSE on failure */ public function decrement($id, $offset = 1) { return $this->_redis->hIncrBy($id, 'data', -$offset); }
/** * hIncrBy a raw value * * @param string $id Cache ID * @param string $field Cache ID * @param int $offset Step/value to add * @return mixed New value on success or FALSE on failure */ public function hincrby($key, $field, $value = 1) { return $this->_redis->hIncrBy($key, $field, $value); }
public function testHashes() { $this->redis->delete('h', 'key'); $this->assertTrue(0 === $this->redis->hLen('h')); $this->assertTrue(TRUE === $this->redis->hSet('h', 'a', 'a-value')); $this->assertTrue(1 === $this->redis->hLen('h')); $this->assertTrue(TRUE === $this->redis->hSet('h', 'b', 'b-value')); $this->assertTrue(2 === $this->redis->hLen('h')); $this->assertTrue('a-value' === $this->redis->hGet('h', 'a')); // simple get $this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get $this->assertTrue(FALSE === $this->redis->hSet('h', 'a', 'another-value')); // replacement $this->assertTrue('another-value' === $this->redis->hGet('h', 'a')); // get the new value $this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get $this->assertTrue(FALSE === $this->redis->hGet('h', 'c')); // unknown hash member $this->assertTrue(FALSE === $this->redis->hGet('key', 'c')); // unknownkey // hDel $this->assertTrue(TRUE === $this->redis->hDel('h', 'a')); // TRUE on success $this->assertTrue(FALSE === $this->redis->hDel('h', 'a')); // FALSE on failure $this->redis->delete('h'); $this->redis->hSet('h', 'x', 'a'); $this->redis->hSet('h', 'y', 'b'); // keys $keys = $this->redis->hKeys('h'); $this->assertTrue($keys === array('x', 'y') || $keys === array('y', 'x')); // values $values = $this->redis->hVals('h'); $this->assertTrue($values === array('a', 'b') || $values === array('b', 'a')); // keys + values $all = $this->redis->hGetAll('h'); $this->assertTrue($all === array('x' => 'a', 'y' => 'b') || $all === array('y' => 'b', 'x' => 'a')); // hExists $this->assertTrue(TRUE === $this->redis->hExists('h', 'x')); $this->assertTrue(TRUE === $this->redis->hExists('h', 'y')); $this->assertTrue(FALSE === $this->redis->hExists('h', 'w')); $this->redis->delete('h'); $this->assertTrue(FALSE === $this->redis->hExists('h', 'x')); // hIncrBy $this->redis->delete('h'); $this->assertTrue(25 === $this->redis->hIncrBy('h', 'x', 25)); $this->assertTrue(35 === $this->redis->hIncrBy('h', 'x', 10)); $this->redis->hSet('h', 'y', 'not-a-number'); $this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'y', 1)); $this->redis->delete('h1'); //hMGet, hMSet $this->redis->hset("h1", "field1", "value1"); $this->redis->hset("h1", "field2", "value2"); $this->assertTrue(array('field1' => 'value1', 'field2' => 'value2') === $this->redis->hGetAll('h1')); $this->assertTrue(array('field1' => 'value1', 'field2' => 'value2') === $this->redis->hMGet('h1', array('field1', 'field2'))); $this->assertTrue(array('field1' => 'value1') === $this->redis->hMGet('h1', array('field1'))); $this->assertTrue(FALSE === $this->redis->hMGet('h1', array())); $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('field3' => 'value3'))); $this->assertTrue(array('field1' => 'value1', 'field2' => 'value2', 'field3' => 'value3') === $this->redis->hGetAll('h1')); $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('field3' => 'value4'))); $this->assertTrue(array('field1' => 'value1', 'field2' => 'value2', 'field3' => 'value4') === $this->redis->hGetAll('h1')); $this->assertTrue(TRUE === $this->redis->hMSet('h1', array('x' => 0, 'y' => array(), 'z' => new stdclass()))); $h1 = $this->redis->hGetAll('h1'); $this->assertTrue('0' === $h1['x']); $this->assertTrue('Array' === $h1['y']); $this->assertTrue('Object' === $h1['z']); }
/** * 自增hash表中某个key的值 * @param $hash string 哈希表名 * @param $key mixed 表中存储的key名 * @param $inc int 要增加的值 */ public static function hashInc($hash, $key, $inc) { $redis = new \Redis(); $redis->connect(self::_HOST, self::_PORT); $return = null; $return = $redis->hIncrBy($hash, $key, $inc); $redis->close(); $redis = null; return $return; }