public function testScalars() { // Basic get/set $this->credis->set('foo', 'FOO'); $this->assertEquals('FOO', $this->credis->get('foo')); $this->assertFalse($this->credis->get('nil')); // Empty string $this->credis->set('empty', ''); $this->assertEquals('', $this->credis->get('empty')); // UTF-8 characters $utf8str = str_repeat("quarter: ¼, micro: µ, thorn: Þ, ", 500); $this->credis->set('utf8', $utf8str); $this->assertEquals($utf8str, $this->credis->get('utf8')); // Array $this->assertTrue($this->credis->mSet(array('bar' => 'BAR', 'apple' => 'red'))); $mGet = $this->credis->mGet(array('foo', 'bar', 'empty')); $this->assertTrue(in_array('FOO', $mGet)); $this->assertTrue(in_array('BAR', $mGet)); $this->assertTrue(in_array('', $mGet)); // Non-array $mGet = $this->credis->mGet('foo', 'bar'); $this->assertTrue(in_array('FOO', $mGet)); $this->assertTrue(in_array('BAR', $mGet)); // Delete strings, null response $this->assertEquals(2, $this->credis->del('foo', 'bar')); $this->assertFalse($this->credis->get('foo')); $this->assertFalse($this->credis->get('bar')); // Long string $longString = str_repeat(md5('asd'), 4096); // 128k (redis.h REDIS_INLINE_MAX_SIZE = 64k) $this->assertTrue($this->credis->set('long', $longString)); $this->assertEquals($longString, $this->credis->get('long')); }
/** * 写入缓存 * @param string $id * @param string $value * @param string $expire * @return mixed */ public function set($id, $value, $expire = '3600') { $key = $this->_getKeyName($id); var_dump($this->_client); $result = $this->_client->set($key, serialize($value)); if ($expire) { $this->expire($id, $expire); } return $result; }
public function testscan() { $this->credis->set('name', 'Jack'); $this->credis->set('age', '33'); $iterator = null; $result = $this->credis->scan($iterator, 'n*'); $this->assertEquals($iterator, 0); $this->assertEquals($result, ['name']); }
public function testScripts() { $this->assertNull($this->credis->evalSha('1111111111111111111111111111111111111111')); $this->assertEquals(3, $this->credis->eval('return 3')); $this->assertEquals('09d3822de862f46d784e6a36848b4f0736dda47a', $this->credis->script('load', 'return 3')); $this->assertEquals(3, $this->credis->evalSha('09d3822de862f46d784e6a36848b4f0736dda47a')); $this->credis->set('foo', 'FOO'); $this->assertEquals('FOOBAR', $this->credis->eval("return redis.call('get', KEYS[1])..ARGV[1]", 'foo', 'BAR')); $this->assertEquals(array(1, 2, 'three'), $this->credis->eval("return {1,2,'three'}")); try { $this->credis->eval('this-is-not-lua'); $this->fail('Expected exception on invalid script.'); } catch (CredisException $e) { } }
public function testConnectionStrings() { $this->credis->close(); $this->credis = new Credis_Client('tcp://' . $this->config[0]->host . ':' . $this->config[0]->port); $this->assertEquals($this->credis->getHost(), $this->config[0]->host); $this->assertEquals($this->credis->getPort(), $this->config[0]->port); $this->credis = new Credis_Client('tcp://' . $this->config[0]->host); $this->assertEquals($this->credis->getPort(), $this->config[0]->port); $this->credis = new Credis_Client('tcp://' . $this->config[0]->host . ':' . $this->config[0]->port . '/abc123'); $this->assertEquals('abc123', $this->credis->getPersistence()); $this->credis = new Credis_Client(realpath(__DIR__) . '/redis.sock', 0, null, 'persistent'); $this->credis->connect(); $this->credis->set('key', 'value'); $this->assertEquals('value', $this->credis->get('key')); }
#!/usr/bin/php <?php $config = ['git_urls' => ['https://github.com/colinmollenhour/credis.git' => 'credis/'], 'require_once' => ['credis/Client.php', 'credis/Cluster.php', 'credis/Sentinel.php'], 'example' => function () { $client = new Credis_Client(getenv('REDIS_HOST') ?: '127.0.0.1', getenv('REDIS_PORT') ?: 6379); $client->set('testme_key', 'testme_val'); $value = $client->get('testme_key'); print ($value == 'testme_val' ? 'OK' : 'ERROR') . PHP_EOL; }]; if ($return_config) { return $config; } require_once __DIR__ . '/_yf_autoloader.php'; new yf_autoloader($config);
*/ $resource = Mage::getSingleton('core/resource'); /** * Retrieve the read connection */ $readConnection = $resource->getConnection('core_read'); // 1. query to order by session_expires, limit N, save last expire time, and session_id // 2. modify query with where session_expires >= last expire time, and session_id != session_id $exptime = 0; $lastid = 'NONE'; $batchlimit = 100; do { //$query = 'SELECT session_id, session_expires, session_data FROM ' . $resource->getTableName('core/session'). // ' WHERE session_expires >= ? AND session_id != ? ORDER BY session_expires ASC LIMIT '.$batchlimit; //$results = $readConnection->fetchAll($query, array($exptime, $lastid)); $query = $readConnection->select()->from(array('cs' => $resource->getTableName('core/session')), array('session_id', 'session_expires', 'session_data'))->having("session_expires > ?", $exptime)->having("session_id != ?", $lastid)->limit($batchlimit)->order('session_expires'); $results = $readConnection->fetchAll($query); //var_dump($results); foreach ($results as $row) { $lastid = $row['session_id']; $exptime = $row['session_expires']; $sesskey = PREFIX . $lastid; $_redis->set($sesskey, $row['session_data']); $_redis->expireat($sesskey, $exptime); echo $lastid . " " . $exptime . "\n"; } echo "----------------------------------\n"; } while (!empty($results)); /* Zend_Debug::dump($_SESSION); */
/** * {@inheritdoc} */ public function measure($variable, $value) { $this->credis_client->set($variable, $value); }
if (empty($user)) { return 'Invalid user.'; } return 'Welcome ' . json_encode($user); }); $app->get('/auth/callback', function (Request $request) use($app) { $payload = array('client_id' => clientId(), 'client_secret' => clientSecret(), 'redirect_uri' => callbackUrl(), 'grant_type' => 'authorization_code', 'code' => $request->get('code'), 'scope' => $request->get('scope'), 'context' => $request->get('context')); $client = new Client(bcAuthService()); $req = $client->post('/oauth2/token', array(), $payload, array('exceptions' => false)); $resp = $req->send(); if ($resp->getStatusCode() == 200) { $data = $resp->json(); list($context, $storeHash) = explode('/', $data['context'], 2); $key = getUserKey($storeHash, $data['user']['email']); $redis = new Credis_Client('localhost'); $redis->set($key, json_encode($data['user'], true)); return 'Hello ' . json_encode($data); } else { return 'Something went wrong... [' . $resp->getStatusCode() . '] ' . $resp->getBody(); } }); function verifySignedRequest($signedRequest) { list($encodedData, $encodedSignature) = explode('.', $signedRequest, 2); // decode the data $signature = base64_decode($encodedSignature); $jsonStr = base64_decode($encodedData); $data = json_decode($jsonStr, true); // confirm the signature $expectedSignature = hash_hmac('sha256', $jsonStr, clientSecret(), $raw = false); if (!hash_equals($expectedSignature, $signature)) {