/** * Stores $key, $value and $ttl in redis db as well as in our cache. * * @param string $key * @param string $value * @param int $ttl * @param bool $writeToRedis * @return bool */ private function cachedSet($key, $value, $ttl = 0, $writeToRedis = true) { if (0 > $ttl) { $ttl = 0; } if ($writeToRedis) { if (!$this->redis->set($key, $value, $ttl)) { return false; } } if (0 === $ttl) { $ttl = $this->lifetime; } $this->cache[$key] = array('value' => $value, 'cached_at' => $this->time->current(), 'ttl' => $ttl); return true; }
/** * Set the string value in argument as value of the key. * * @param string $key * @param string $value * @param int $ttl Calling setex() is preferred if you want a Time To Live. * * @return bool: If the command is successful return TRUE * @link http://redis.io/commands/set * @example $redis->set('key', 'value'); */ public function set($key, $value, $ttl = 0) { try { return $this->client->set($key, $value, $ttl); } catch (Exception $e) { return $this->handleException($e, __FUNCTION__, func_get_args()); } }
/** * Test the scan family of commands */ public function testScan() { $this->redis->flushdb(); $keys = ['testScan', 'testScanHello', 'testScanHallo', 'testScanHaallo', 'testScanHoula', 'testScan2', 'testScanHello2', 'testScanHallo2', 'testScanHaallo2', 'testScanHoula2', 'testScan3', 'testScanHello3', 'testScanHallo3', 'testScanHaallo3', 'testScanHoula3']; foreach ($keys as $key) { $this->redis->set($key, 'someValue'); } // a bit tricky to test, since we can't determine the outcome of a single iteration (or can we?) // so we just iterate until the iterator is exhausted and then compare the final results $iterator = 0; // we unroll the iteration here, to have better control over the involved values $result = $this->redis->scan($iterator); $this->assertNotEmpty($result); //$this->assertSame(10, count($result)); $this->assertNotSame(0, $iterator); $result = array_merge($result, $this->redis->scan($iterator)); foreach ($keys as $key) { $this->assertContains($key, $result); } $this->assertSame(15, count($result)); $this->assertSame(0, $iterator); $result1 = $this->redis->scan($iterator, 'testScanH?llo', 20); $this->assertContains('testScanHello', $result1); $this->assertContains('testScanHallo', $result1); $this->assertEquals(2, count($result1)); $this->assertSame(0, $iterator); $result2 = $this->redis->scan($iterator, 'testScanH*llo', 20); $this->assertContains('testScanHello', $result2); $this->assertContains('testScanHallo', $result2); $this->assertContains('testScanHaallo', $result2); $this->assertEquals(3, count($result2)); $this->assertSame(0, $iterator); $result3 = $this->redis->scan($iterator, 'testScanH[ae]llo', 20); $this->assertContains('testScanHello', $result3); $this->assertContains('testScanHallo', $result3); $this->assertEquals(2, count($result3)); $this->assertSame(0, $iterator); $result4 = $this->redis->scan($iterator, '*', 20); $this->assertEquals(15, count($result4)); $this->assertSame(0, $iterator); }
/** * Set the string value in argument as value of the key. * * @param string $key * @param string $value * @param int $ttl Calling setex() is preferred if you want a Time To Live. * * @return bool: If the command is successful return TRUE * @link http://redis.io/commands/set * @example $redis->set('key', 'value'); */ public function set($key, $value, $ttl = 0) { $this->appendToLog('SET ' . $key . ' ' . $value . ' ' . $ttl); return $this->client->set($key, $value, $ttl); }