| | Author: Aleksander Machniak | +-----------------------------------------------------------------------+ */ /** * Interface class for accessing Redis cache * * @package Framework * @subpackage Cache * @author Thomas Bruederli * @author Aleksander Machniak */ class rcube_cache_redis extends rcube_cache { /** * Instance of Redis object * * @var Redis */ protected static $redis; /** * Object constructor. * * @param int $userid User identifier * @param string $prefix Key name prefix * @param string $ttl Expiration time of memcache/apc items * @param bool $packed Enables/disabled data serialization. * It's possible to disable data serialization if you're sure * stored data will be always a safe string */ public function __construct($userid, $prefix = '', $ttl = 0, $packed = true) { parent::__construct($userid, $prefix, $ttl, $packed); $rcube = rcube::get_instance(); $this->type = 'redis'; $this->debug = $rcube->config->get('redis_debug'); self::engine(); } /** * Get global handle for redis access * * @return object Redis */ public static function engine() { if (self::$redis !== null) { return self::$redis; } if (!class_exists('Redis')) { self::$redis = false; rcube::raise_error(array( 'code' => 604, 'type' => 'redis', 'line' => __LINE__, 'file' => __FILE__, 'message' => "Failed to find Redis. Make sure php-redis is included" ), true, true); } $rcube = rcube::get_instance(); $hosts = $rcube->config->get('redis_hosts'); // host config is wrong if (!is_array($hosts) || empty($hosts)) { rcube::raise_error(array( 'code' => 604, 'type' => 'redis', 'line' => __LINE__, 'file' => __FILE__, 'message' => "Redis host not configured" ), true, true); } // only allow 1 host for now until we support clustering if (count($hosts) > 1) { rcube::raise_error(array( 'code' => 604, 'type' => 'redis', 'line' => __LINE__, 'file' => __FILE__, 'message' => "Redis cluster not yet supported" ), true, true); } self::$redis = new Redis; foreach ($hosts as $redis_host) { // explode individual fields list($host, $port, $database, $password) = array_pad(explode(':', $redis_host, 4), 4, null); if (substr($redis_host, 0, 7) === 'unix://') { $host = substr($port, 2); $port = null; } else { // set default values if not set $host = $host ?: '127.0.0.1'; $port = $port ?: 6379; } try { if (self::$redis->connect($host, $port) === false) { rcube::raise_error(array( 'code' => 604, 'type' => 'redis', 'line' => __LINE__, 'file' => __FILE__, 'message' => "Could not connect to Redis server. Please check host and port" ), true, true); } if ($password !== null && self::$redis->auth($password) === false) { rcube::raise_error(array( 'code' => 604, 'type' => 'redis', 'line' => __LINE__, 'file' => __FILE__, 'message' => "Could not authenticate with Redis server. Please check password." ), true, true); } if ($database !== null && self::$redis->select($database) === false) { rcube::raise_error(array( 'code' => 604, 'type' => 'redis', 'line' => __LINE__, 'file' => __FILE__, 'message' => "Could not select Redis database. Please check database setting." ), true, true); } } catch (Exception $e) { rcube::raise_error($e, true, true); } } if (self::$redis->ping() !== "+PONG") { self::$redis = false; } return self::$redis; } /** * Remove cache records older than ttl */ public function expunge() { // No need for GC, entries are expunged automatically } /** * Remove expired records */ public static function gc() { // No need for GC, entries are expunged automatically } /** * Reads cache entry. * * @param string $key Cache internal key name * * @return mixed Cached value */ protected function get_item($key) { if (!self::$redis) { return false; } $data = self::$redis->get($key); if ($this->debug) { $this->debug('get', $key, $data); } return $data; } /** * Adds entry into Redis. * * @param string $key Cache internal key name * @param mixed $data Serialized cache data * * @param boolean True on success, False on failure */ protected function add_item($key, $data) { if (!self::$redis) { return false; } $result = self::$redis->setEx($key, $this->ttl, $data); if ($this->debug) { $this->debug('set', $key, $data, $result); } return $result; } /** * Deletes entry from Redis. * * @param string $key Cache internal key name * * @param boolean True on success, False on failure */ protected function delete_item($key) { if (!self::$redis) { return false; } $result = self::$redis->delete($key); if ($this->debug) { $this->debug('delete', $key, null, $result); } return $result; } }