/** * Try to get an item, and if missed call the fallback method to produce the value and store it. * * @param string $key * @param callable $fallback * @param int|\DateInterval|\DateTime|callback $validity Number of seconds this is valid for (if int) * @return mixed */ function getWithFallback($key, callable $fallback, $validity = null) { $value = $this->connection->get($key); if (!$value && !$this->connection->exists($key)) { $value = $fallback(); if (is_callable($validity)) { $validity = call_user_func($validity, $value); } $this->set($key, $value, $validity); return $value; } return $value; }
/** * @inheritDoc */ public function read($id) { if ($data = $this->_redis->get($this->getPrefixKey($id))) { return unserialize($data); } return null; }
/** * @inheritdoc */ public function retrieve($key) { if (!$this->exists($key)) { return null; } return unserialize($this->redis->get($this->prefix . $key)); }
/** * Returns cached value by key * * @param string $key * @return bool|mixed */ public function get($key) { if (false === ($value = $this->cache->get($this->prepareKey($key)))) { return false; } return $this->unserializeCompound($value); }
/** * Attempt to retrieve a value from the cache server, if not set it. * * @param string $key Key we can use to retrieve the data. * * @return bool|string False on failure or String, data belonging to the key. * @access public */ public function get($key) { if ($this->connected === true && $this->ping() === true) { $data = $this->server->get($key); return $this->isRedis ? $this->IgBinarySupport ? igbinary_unserialize($data) : unserialize($data) : $data; } return false; }
/** * get var * @param $key * @param null $default * @return bool|mixed */ public function get($key, $default = null) { $result = $this->redis->get($key); if ($result) { return $result; } return $default; }
/** * Loads item by cache key. * * @param string $key * @return mixed * * @throws StorageException if storage error occurs, handler can not be used */ protected function load($key) { try { return $this->redis->get($key); } catch (\Exception $e) { throw new StorageException("Failed to load redis key: {$key}", 1, $e); } }
/** * Get a cache variable * * Retrieve a variable from the cache and return it's * value to the user. * * @param string $name The name of the cache variable to retrieve. * * @return mixed The value of the retrieved variable or false if * variable isn't found. */ public function get($name) { $key = $this->redis->get($name); if ($key !== false) { return unserialize($key); } return false; }
public static function increment($host) { $count = self::$redis->incrBy(self::$prefix . $host, 1); if (self::$redis->get(self::$prefix . $host) == 1) { self::$redis->setTimeout(self::$prefix . $host, self::$ttl); } return $count; }
/** * Get cache * * @param string Cache ID * @return mixed */ public function get($key) { $value = $this->_redis->get($key); if ($value !== FALSE && isset($this->_serialized[$key])) { return unserialize($value); } return $value; }
public function get($key) { $result = self::$cache->get($this->getNamespace() . $key); if ($result === false && !self::$cache->exists($this->getNamespace() . $key)) { return null; } else { return json_decode($result, true); } }
/** * @param $key * @return Option */ public static function getMy($key) { $hash = self::hash($key); if (self::$redis->exists(self::myPrefix() . ":" . $hash)) { return Option::Some(unserialize(self::$redis->get(self::myPrefix() . ":" . $hash))); } else { return Option::None(); } }
/** * @param int $provider * @param int $providerUserId * * @return bool|User * */ public function loadUserByOauth($provider, $providerUserId) { if (!($userName = $this->redis->get('credentials:' . implode(':', array($provider, $providerUserId))))) { return false; } if (!($user = $this->loadUserByUsername($userName))) { return false; } return new User($user); }
public function testStdClass() { $data = new \stdClass(); $data->a = 'a'; $data->b = 'b'; $key = new CacheKey('test', rand(1, 1000)); $this->cache->set($key, $data); $val = $this->cache->get($key); $this->assertEquals($data, $val); }
public function get($id, $default = NULL) { // Get the value from Redis $value = $this->_redis->get($id); if ($value == false) { $value = $default; } // Return the value return $value; }
public function __construct() { $redis = new Redis(); try { $redis->connect('127.0.0.1', 6379); $this->data = array('count' => $redis->get('cobbler:count'), 'speed' => $redis->get('cobbler:speed')); $redis->close(); } catch (Exception $e) { $this->error(500, $e->getMessage()); } }
/** * 获取缓存数据 * @param string $cache_id * @return mixed */ public function get($cache_id) { try { $result = $this->_redis->get($cache_id); if (!$result) { return false; } return @unserialize($result); } catch (Exception $e) { $this->_error = $e->getMessage(); return false; } }
/** * Get cache * * @param string Cache ID * @return mixed */ public function get($key) { // //Tim // if(!$this->is_supported()) // { // return false; // } $value = $this->_redis->get($key); if ($value !== FALSE && isset($this->_serialized[$key])) { return unserialize($value); } return $value; }
/** * Check if the Scheduler Worker is already running * * @return boolean True if the scheduler worker is already running */ public function isRunningSchedulerWorker() { $pids = $this->redis->hKeys(self::$workerKey); $schedulerPid = $this->redis->get(self::$schedulerWorkerKey); if ($schedulerPid !== false && is_array($pids)) { if (in_array($schedulerPid, $pids)) { return true; } // Pid is outdated, remove it $this->unregisterSchedulerWorker(); return false; } return false; }
public function test($setIterationCount, $getIterationCount) { $setTime = microtime(true); for ($i = 0; $i < $setIterationCount; $i++) { $this->redis->set('test' . $i, $this->stringForTest); } $setTime = microtime(true) - $setTime; $getTime = microtime(true); for ($i = 0; $i < $getIterationCount; $i++) { $this->redis->get('test' . $i % $setIterationCount); } $getTime = microtime(true) - $getTime; return ['set' => $setTime, 'get' => $getTime]; }
/** * {@inheritdoc} */ public function update($key, callable $modifier, $ttl = 0) { $this->redis->watch($key); $data = $this->redis->get($key); if ($data === false) { $this->redis->unwatch(); return false; } else { $modifier($data); if ($this->setInTransaction($key, $data, (int) $ttl)) { return $data; } } throw new AtomicViolationException("Atomic violation occurred when updating key \"{$key}\"."); }
/** * @return int */ public function getCurrentNumber() { Assertion::notNull(self::$redis, "Redis connection hasn't been set"); $initialNumber = $this->initialNumber; $registeredKeys = self::$redis->sMembers($this->identifier); foreach ($registeredKeys as $key) { $value = self::$redis->get($key); if ($value !== false) { $initialNumber += $value; } else { self::$redis->sRemove($this->identifier, $key); } } return $initialNumber; }
/** * 取得数据,支持批量取 * * @param string/array $key * @return mixed */ public function get($key) { $this->_connect(); $time = microtime(1); if (is_array($key)) { # redis多取 if ($this->prefix) { foreach ($key as &$k) { $k = $this->prefix . $k; } } $return = $this->_redis->mget($key); foreach ($return as &$item) { $this->_de_format_data($item); } } else { $return = $this->_redis->get($key); $this->_de_format_data($return); } $time = microtime(1) - $time; if (false === $return) { Core::debug()->warn($key, 'cache redis mis key'); Core::debug()->info($time, 'use time'); return false; } else { Core::debug()->info($key, 'cache redis hit key'); Core::debug()->info($time, 'use time'); } return $return; }
/** * @site http://www.chenliujin.com * @author chenliujin <*****@*****.**> * @since 2016-01-19 * @param type $key */ public static function Read($key) { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $val = $redis->get($key); return $val ? $val : ''; }
public function getCacheAction() { $redis = new Redis(); $redis->connect('127.0.0.1'); $page = $redis->get('page'); print_r($page); }
/** * {@inheritDoc} */ public function removeWord($language, $word) { $word = mb_strtolower($word, mb_detect_encoding($word)); if ($this->redis->exists($word)) { $languages = unserialize($this->redis->get($word)); if (false !== ($index = array_search($language, $languages))) { unset($languages[$index]); if (!count($languages)) { $this->redis->del($word); } else { $this->redis->set($word, serialize($languages)); } } } return $this; }
/** * Get cached data by ID and group * * @param string $id The cache data ID * @param string $group The cache data group * @param boolean $checkTime True to verify cache time expiration threshold * * @return mixed Boolean false on failure or a cached data object * * @since 3.4 */ public function get($id, $group, $checkTime = true) { if (static::isConnected() == false) { return false; } return static::$_redis->get($this->_getCacheId($id, $group)); }
function initModules($application) { $redis = new Redis(); $redis->connect(CACHE_HOSTNAME, CACHE_PORT); $redis->select(CACHE_DB); $modulesKey = $application . '-modules'; $modules = $redis->get($modulesKey); if (empty($modules)) { $modules = []; $dir = __DIR__ . '/../../' . $application . '/modules'; $dirpath = realpath($dir); $filenames = scandir($dir); foreach ($filenames as $filename) { if ($filename == '.' || $filename == '..') { continue; } if (is_file($dirpath . DIRECTORY_SEPARATOR . $filename . '/Module.php')) { $modules[strtolower($filename)] = ['class' => $application . '\\modules\\' . $filename . '\\Module']; } } $redis->set($modulesKey, serialize([$modules, null])); } else { $modules = unserialize($modules); if (!empty($modules[0])) { $modules = $modules[0]; } } $redis->close(); return $modules; }
public function testRedisShouldWorkProperly() { $redis = new \Redis(); $redis->connect('localhost'); $this->assertTrue($redis->set('test', 'test'), 'The set method return false'); $this->assertEquals('test', $redis->get('test'), 'The get method it\'s broken'); }
public function _display_cache(&$CFG, &$URI) { $cache_path = $CFG->item('cache_path') === '' ? APPPATH . 'cache/' : $CFG->item('cache_path'); $uri = $CFG->item('base_url') . $CFG->item('index_page') . $URI->uri_string; if ($CFG->item('cache_query_string') && !empty($_SERVER['QUERY_STRING'])) { $uri .= '?' . $_SERVER['QUERY_STRING']; } $filepath = $cache_path . md5($uri); $redis = new Redis(); $host = $CFG->item("redis_host"); $port = $CFG->item("redis_port"); $redis->connect($host, $port); if ($redis->exists($filepath)) { $cache = $redis->get($filepath); if (!preg_match('/^(.*)ENDCI--->/', $cache, $match)) { return false; } } else { return false; } $cache_info = unserialize($match[1]); $expire = $cache_info['expire']; $last_modified = $cache_info["last_modified"]; $this->set_cache_header($last_modified, $expire); foreach ($cache_info['headers'] as $header) { $this->set_header($header[0], $header[1]); } $this->_display(substr($cache, strlen($match[0]))); log_message('debug', 'Cache is current. Sending it to browser.'); return TRUE; }