Import Ruty
This commit is contained in:
+138
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
+-----------------------------------------------------------------------+
|
||||
| This file is part of the Roundcube Webmail client |
|
||||
| |
|
||||
| Copyright (C) The Roundcube Dev Team |
|
||||
| Copyright (C) Kolab Systems AG |
|
||||
| |
|
||||
| Licensed under the GNU General Public License version 3 or |
|
||||
| any later version with exceptions for skins & plugins. |
|
||||
| See the README file for a full license statement. |
|
||||
| |
|
||||
| PURPOSE: |
|
||||
| Caching engine - APC |
|
||||
+-----------------------------------------------------------------------+
|
||||
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
||||
| Author: Aleksander Machniak <alec@alec.pl> |
|
||||
+-----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface implementation class for accessing APC cache
|
||||
*
|
||||
* @package Framework
|
||||
* @subpackage Cache
|
||||
*/
|
||||
class rcube_cache_apc extends rcube_cache
|
||||
{
|
||||
/**
|
||||
* Indicates if APC module is enabled and in a required version
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $enabled;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($userid, $prefix = '', $ttl = 0, $packed = true, $indexed = false)
|
||||
{
|
||||
parent::__construct($userid, $prefix, $ttl, $packed, $indexed);
|
||||
|
||||
$rcube = rcube::get_instance();
|
||||
|
||||
$this->type = 'apc';
|
||||
$this->enabled = function_exists('apc_exists'); // APC 3.1.4 required
|
||||
$this->debug = $rcube->config->get('apc_debug');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove cache records older than ttl
|
||||
*/
|
||||
public function expunge()
|
||||
{
|
||||
// No need for GC, entries are expunged automatically
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove expired records of all caches
|
||||
*/
|
||||
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 (!$this->enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = apc_fetch($key);
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('get', $key, $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds entry into memcache/apc/redis DB.
|
||||
*
|
||||
* @param string $key Cache internal key name
|
||||
* @param mixed $data Serialized cache data
|
||||
*
|
||||
* @param bool True on success, False on failure
|
||||
*/
|
||||
protected function add_item($key, $data)
|
||||
{
|
||||
if (!$this->enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (apc_exists($key)) {
|
||||
apc_delete($key);
|
||||
}
|
||||
|
||||
$result = apc_store($key, $data, $this->ttl);
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('set', $key, $data, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes entry from memcache/apc/redis DB.
|
||||
*
|
||||
* @param string $key Cache internal key name
|
||||
*
|
||||
* @param bool True on success, False on failure
|
||||
*/
|
||||
protected function delete_item($key)
|
||||
{
|
||||
if (!$this->enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = apc_delete($key);
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('delete', $key, null, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+237
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
+-----------------------------------------------------------------------+
|
||||
| This file is part of the Roundcube Webmail client |
|
||||
| |
|
||||
| Copyright (C) The Roundcube Dev Team |
|
||||
| Copyright (C) Kolab Systems AG |
|
||||
| |
|
||||
| Licensed under the GNU General Public License version 3 or |
|
||||
| any later version with exceptions for skins & plugins. |
|
||||
| See the README file for a full license statement. |
|
||||
| |
|
||||
| PURPOSE: |
|
||||
| Caching engine - SQL DB |
|
||||
+-----------------------------------------------------------------------+
|
||||
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
||||
| Author: Aleksander Machniak <alec@alec.pl> |
|
||||
+-----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface implementation class for accessing SQL Database cache
|
||||
*
|
||||
* @package Framework
|
||||
* @subpackage Cache
|
||||
*/
|
||||
class rcube_cache_db extends rcube_cache
|
||||
{
|
||||
/**
|
||||
* Instance of database handler
|
||||
*
|
||||
* @var rcube_db
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* (Escaped) Cache table name (cache or cache_shared)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($userid, $prefix = '', $ttl = 0, $packed = true, $indexed = false)
|
||||
{
|
||||
parent::__construct($userid, $prefix, $ttl, $packed, $indexed);
|
||||
|
||||
$rcube = rcube::get_instance();
|
||||
|
||||
$this->type = 'db';
|
||||
$this->db = $rcube->get_dbh();
|
||||
$this->table = $this->db->table_name($userid ? 'cache' : 'cache_shared', true);
|
||||
|
||||
$this->refresh_time *= 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove cache records older than ttl
|
||||
*/
|
||||
public function expunge()
|
||||
{
|
||||
if ($this->ttl) {
|
||||
$this->db->query(
|
||||
"DELETE FROM {$this->table} WHERE "
|
||||
. ($this->userid ? "`user_id` = {$this->userid} AND " : "")
|
||||
. "`cache_key` LIKE ?"
|
||||
. " AND `expires` < " . $this->db->now(),
|
||||
$this->prefix . '.%');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove expired records of all caches
|
||||
*/
|
||||
public static function gc()
|
||||
{
|
||||
$rcube = rcube::get_instance();
|
||||
$db = $rcube->get_dbh();
|
||||
|
||||
$db->query("DELETE FROM " . $db->table_name('cache', true) . " WHERE `expires` < " . $db->now());
|
||||
$db->query("DELETE FROM " . $db->table_name('cache_shared', true) . " WHERE `expires` < " . $db->now());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads cache entry.
|
||||
*
|
||||
* @param string $key Cache key name
|
||||
*
|
||||
* @return mixed Cached value
|
||||
*/
|
||||
protected function read_record($key)
|
||||
{
|
||||
$sql_result = $this->db->query(
|
||||
"SELECT `data`, `cache_key` FROM {$this->table} WHERE "
|
||||
. ($this->userid ? "`user_id` = {$this->userid} AND " : "")
|
||||
."`cache_key` = ?",
|
||||
$this->prefix . '.' . $key);
|
||||
|
||||
$data = null;
|
||||
|
||||
if ($sql_arr = $this->db->fetch_assoc($sql_result)) {
|
||||
if (strlen($sql_arr['data']) > 0) {
|
||||
$data = $this->unserialize($sql_arr['data']);
|
||||
}
|
||||
|
||||
$this->db->reset();
|
||||
}
|
||||
|
||||
if (!$this->indexed) {
|
||||
$this->cache[$key] = $data;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes single cache record into DB.
|
||||
*
|
||||
* @param string $key Cache key name
|
||||
* @param mixed $data Serialized cache data
|
||||
* @param DateTime $ts Timestamp
|
||||
*
|
||||
* @param bool True on success, False on failure
|
||||
*/
|
||||
protected function store_record($key, $data, $ts = null)
|
||||
{
|
||||
$value = $this->serialize($data);
|
||||
$size = strlen($value);
|
||||
|
||||
// don't attempt to write too big data sets
|
||||
if ($size > $this->max_packet_size()) {
|
||||
trigger_error("rcube_cache: max_packet_size ($this->max_packet) exceeded for key $key. Tried to write $size bytes", E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
|
||||
$db_key = $this->prefix . '.' . $key;
|
||||
|
||||
// Remove NULL rows (here we don't need to check if the record exist)
|
||||
if ($value == 'N;') {
|
||||
$result = $this->db->query(
|
||||
"DELETE FROM {$this->table} WHERE "
|
||||
. ($this->userid ? "`user_id` = {$this->userid} AND " : "")
|
||||
."`cache_key` = ?",
|
||||
$db_key);
|
||||
|
||||
return !$this->db->is_error($result);
|
||||
}
|
||||
|
||||
$expires = $this->db->param($this->ttl ? $this->db->now($this->ttl) : 'NULL', rcube_db::TYPE_SQL);
|
||||
$pkey = ['cache_key' => $db_key];
|
||||
|
||||
if ($this->userid) {
|
||||
$pkey['user_id'] = $this->userid;
|
||||
}
|
||||
|
||||
$result = $this->db->insert_or_update(
|
||||
$this->table, $pkey, ['expires', 'data'], [$expires, $value]
|
||||
);
|
||||
|
||||
$count = $this->db->affected_rows($result);
|
||||
|
||||
return $count > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the cache record(s).
|
||||
*
|
||||
* @param string $key Cache key name or pattern
|
||||
* @param bool $prefix_mode Enable it to clear all keys starting
|
||||
* with prefix specified in $key
|
||||
*/
|
||||
protected function remove_record($key = null, $prefix_mode = false)
|
||||
{
|
||||
// Remove all keys (in specified cache)
|
||||
if ($key === null) {
|
||||
$where = "`cache_key` LIKE " . $this->db->quote($this->prefix . '.%');
|
||||
$this->cache = [];
|
||||
}
|
||||
// Remove keys by name prefix
|
||||
else if ($prefix_mode) {
|
||||
$where = "`cache_key` LIKE " . $this->db->quote($this->prefix . '.' . $key . '%');
|
||||
foreach (array_keys($this->cache) as $k) {
|
||||
if (strpos($k, $key) === 0) {
|
||||
$this->cache[$k] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Remove one key by name
|
||||
else {
|
||||
$where = "`cache_key` = " . $this->db->quote($this->prefix . '.' . $key);
|
||||
$this->cache[$key] = null;
|
||||
}
|
||||
|
||||
$this->db->query(
|
||||
"DELETE FROM {$this->table} WHERE "
|
||||
. ($this->userid ? "`user_id` = {$this->userid} AND " : "") . $where
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes data for storing
|
||||
*/
|
||||
protected function serialize($data)
|
||||
{
|
||||
return $this->db->encode($data, $this->packed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unserializes serialized data
|
||||
*/
|
||||
protected function unserialize($data)
|
||||
{
|
||||
return $this->db->decode($data, $this->packed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the maximum size for cache data to be written
|
||||
*/
|
||||
protected function max_packet_size()
|
||||
{
|
||||
if ($this->max_packet < 0) {
|
||||
$this->max_packet = 2097152; // default/max is 2 MB
|
||||
|
||||
if ($value = $this->db->get_variable('max_allowed_packet', $this->max_packet)) {
|
||||
$this->max_packet = $value;
|
||||
}
|
||||
|
||||
$this->max_packet -= 2000;
|
||||
}
|
||||
|
||||
return $this->max_packet;
|
||||
}
|
||||
}
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
+-----------------------------------------------------------------------+
|
||||
| This file is part of the Roundcube Webmail client |
|
||||
| |
|
||||
| Copyright (C) The Roundcube Dev Team |
|
||||
| Copyright (C) Kolab Systems AG |
|
||||
| |
|
||||
| Licensed under the GNU General Public License version 3 or |
|
||||
| any later version with exceptions for skins & plugins. |
|
||||
| See the README file for a full license statement. |
|
||||
| |
|
||||
| PURPOSE: |
|
||||
| Caching engine - Memcache |
|
||||
+-----------------------------------------------------------------------+
|
||||
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
||||
| Author: Aleksander Machniak <alec@alec.pl> |
|
||||
+-----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface implementation class for accessing Memcache cache
|
||||
*
|
||||
* @package Framework
|
||||
* @subpackage Cache
|
||||
*/
|
||||
class rcube_cache_memcache extends rcube_cache
|
||||
{
|
||||
/**
|
||||
* Instance of memcache handler
|
||||
*
|
||||
* @var Memcache
|
||||
*/
|
||||
protected static $memcache;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($userid, $prefix = '', $ttl = 0, $packed = true, $indexed = false)
|
||||
{
|
||||
parent::__construct($userid, $prefix, $ttl, $packed, $indexed);
|
||||
|
||||
$this->type = 'memcache';
|
||||
$this->debug = rcube::get_instance()->config->get('memcache_debug');
|
||||
|
||||
self::engine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get global handle for memcache access
|
||||
*
|
||||
* @return object Memcache
|
||||
*/
|
||||
public static function engine()
|
||||
{
|
||||
if (self::$memcache !== null) {
|
||||
return self::$memcache;
|
||||
}
|
||||
|
||||
// no memcache support in PHP
|
||||
if (!class_exists('Memcache')) {
|
||||
self::$memcache = false;
|
||||
|
||||
rcube::raise_error([
|
||||
'code' => 604,
|
||||
'type' => 'memcache',
|
||||
'line' => __LINE__,
|
||||
'file' => __FILE__,
|
||||
'message' => "Failed to find Memcache. Make sure php-memcache is included"
|
||||
],
|
||||
true, true);
|
||||
}
|
||||
|
||||
// add all configured hosts to pool
|
||||
$rcube = rcube::get_instance();
|
||||
$pconnect = $rcube->config->get('memcache_pconnect', true);
|
||||
$timeout = $rcube->config->get('memcache_timeout', 1);
|
||||
$retry_interval = $rcube->config->get('memcache_retry_interval', 15);
|
||||
$seen = [];
|
||||
$available = 0;
|
||||
|
||||
// Callback for memcache failure
|
||||
$error_callback = function($host, $port) use ($seen, $available) {
|
||||
// only report once
|
||||
if (!$seen["$host:$port"]++) {
|
||||
$available--;
|
||||
rcube::raise_error([
|
||||
'code' => 604, 'type' => 'memcache',
|
||||
'line' => __LINE__, 'file' => __FILE__,
|
||||
'message' => "Memcache failure on host $host:$port"
|
||||
],
|
||||
true, false);
|
||||
}
|
||||
};
|
||||
|
||||
self::$memcache = new Memcache;
|
||||
|
||||
foreach ((array) $rcube->config->get('memcache_hosts') as $host) {
|
||||
if (substr($host, 0, 7) != 'unix://') {
|
||||
list($host, $port) = explode(':', $host);
|
||||
if (!$port) $port = 11211;
|
||||
}
|
||||
else {
|
||||
$port = 0;
|
||||
}
|
||||
|
||||
$available += intval(self::$memcache->addServer(
|
||||
$host, $port, $pconnect, 1, $timeout, $retry_interval, false, $error_callback));
|
||||
}
|
||||
|
||||
// test connection and failover (will result in $available == 0 on complete failure)
|
||||
self::$memcache->increment('__CONNECTIONTEST__', 1); // NOP if key doesn't exist
|
||||
|
||||
if (!$available) {
|
||||
self::$memcache = false;
|
||||
}
|
||||
|
||||
return self::$memcache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove cache records older than ttl
|
||||
*/
|
||||
public function expunge()
|
||||
{
|
||||
// No need for GC, entries are expunged automatically
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove expired records of all caches
|
||||
*/
|
||||
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::$memcache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = self::$memcache->get($key);
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('get', $key, $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds entry into the cache.
|
||||
*
|
||||
* @param string $key Cache internal key name
|
||||
* @param mixed $data Serialized cache data
|
||||
*
|
||||
* @param bool True on success, False on failure
|
||||
*/
|
||||
protected function add_item($key, $data)
|
||||
{
|
||||
if (!self::$memcache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = self::$memcache->replace($key, $data, MEMCACHE_COMPRESSED, $this->ttl);
|
||||
|
||||
if (!$result) {
|
||||
$result = self::$memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->ttl);
|
||||
}
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('set', $key, $data, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes entry from the cache
|
||||
*
|
||||
* @param string $key Cache internal key name
|
||||
*
|
||||
* @param bool True on success, False on failure
|
||||
*/
|
||||
protected function delete_item($key)
|
||||
{
|
||||
if (!self::$memcache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// #1488592: use 2nd argument
|
||||
$result = self::$memcache->delete($key, 0);
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('delete', $key, null, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
+-----------------------------------------------------------------------+
|
||||
| This file is part of the Roundcube Webmail client |
|
||||
| |
|
||||
| Copyright (C) The Roundcube Dev Team |
|
||||
| Copyright (C) Kolab Systems AG |
|
||||
| |
|
||||
| Licensed under the GNU General Public License version 3 or |
|
||||
| any later version with exceptions for skins & plugins. |
|
||||
| See the README file for a full license statement. |
|
||||
| |
|
||||
| PURPOSE: |
|
||||
| Caching engine - Memcache |
|
||||
+-----------------------------------------------------------------------+
|
||||
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
||||
| Author: Aleksander Machniak <alec@alec.pl> |
|
||||
+-----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface implementation class for accessing Memcached cache
|
||||
*
|
||||
* @package Framework
|
||||
* @subpackage Cache
|
||||
*/
|
||||
class rcube_cache_memcached extends rcube_cache
|
||||
{
|
||||
/**
|
||||
* Instance of memcached handler
|
||||
*
|
||||
* @var Memcached
|
||||
*/
|
||||
protected static $memcache;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($userid, $prefix = '', $ttl = 0, $packed = true, $indexed = false)
|
||||
{
|
||||
parent::__construct($userid, $prefix, $ttl, $packed, $indexed);
|
||||
|
||||
$this->type = 'memcache';
|
||||
$this->debug = rcube::get_instance()->config->get('memcache_debug');
|
||||
|
||||
// Maximum TTL is 30 days, bigger values are treated by Memcached
|
||||
// as unix timestamp which is not what we want
|
||||
if ($this->ttl > 60*60*24*30) {
|
||||
$this->ttl = 60*60*24*30;
|
||||
}
|
||||
|
||||
self::engine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get global handle for memcache access
|
||||
*
|
||||
* @return object Memcache
|
||||
*/
|
||||
public static function engine()
|
||||
{
|
||||
if (self::$memcache !== null) {
|
||||
return self::$memcache;
|
||||
}
|
||||
|
||||
// no memcache support in PHP
|
||||
if (!class_exists('Memcached')) {
|
||||
self::$memcache = false;
|
||||
|
||||
rcube::raise_error([
|
||||
'code' => 604, 'type' => 'memcache', 'line' => __LINE__, 'file' => __FILE__,
|
||||
'message' => "Failed to find Memcached. Make sure php-memcached is installed"
|
||||
],
|
||||
true, true);
|
||||
}
|
||||
|
||||
// add all configured hosts to pool
|
||||
$rcube = rcube::get_instance();
|
||||
$pconnect = $rcube->config->get('memcache_pconnect', true);
|
||||
$timeout = $rcube->config->get('memcache_timeout', 1);
|
||||
$retry_interval = $rcube->config->get('memcache_retry_interval', 15);
|
||||
$hosts = $rcube->config->get('memcache_hosts');
|
||||
$persistent_id = $pconnect ? ('rc' . md5(serialize($hosts))) : null;
|
||||
|
||||
self::$memcache = new Memcached($persistent_id);
|
||||
|
||||
self::$memcache->setOptions([
|
||||
Memcached::OPT_CONNECT_TIMEOUT => $timeout * 1000,
|
||||
Memcached::OPT_RETRY_TIMEOUT => $timeout,
|
||||
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
|
||||
Memcached::OPT_COMPRESSION => true,
|
||||
]);
|
||||
|
||||
if (!$pconnect || !count(self::$memcache->getServerList())) {
|
||||
foreach ((array) $hosts as $host) {
|
||||
if (substr($host, 0, 7) != 'unix://') {
|
||||
list($host, $port) = explode(':', $host);
|
||||
if (!$port) $port = 11211;
|
||||
}
|
||||
else {
|
||||
$host = substr($host, 7);
|
||||
$port = 0;
|
||||
}
|
||||
|
||||
self::$memcache->addServer($host, $port);
|
||||
}
|
||||
}
|
||||
|
||||
// test connection
|
||||
$result = self::$memcache->increment('__CONNECTIONTEST__');
|
||||
|
||||
if ($result === false && ($res_code = self::$memcache->getResultCode()) !== Memcached::RES_NOTFOUND) {
|
||||
self::$memcache = false;
|
||||
|
||||
rcube::raise_error([
|
||||
'code' => 604, 'type' => 'memcache', 'line' => __LINE__, 'file' => __FILE__,
|
||||
'message' => "Memcache connection failure (code: $res_code)."
|
||||
],
|
||||
true, false);
|
||||
}
|
||||
|
||||
return self::$memcache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove cache records older than ttl
|
||||
*/
|
||||
public function expunge()
|
||||
{
|
||||
// No need for GC, entries are expunged automatically
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove expired records of all caches
|
||||
*/
|
||||
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::$memcache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = self::$memcache->get($key);
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('get', $key, $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds entry into the cache.
|
||||
*
|
||||
* @param string $key Cache internal key name
|
||||
* @param mixed $data Serialized cache data
|
||||
*
|
||||
* @param bool True on success, False on failure
|
||||
*/
|
||||
protected function add_item($key, $data)
|
||||
{
|
||||
if (!self::$memcache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = self::$memcache->set($key, $data, $this->ttl);
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('set', $key, $data, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes entry from the cache
|
||||
*
|
||||
* @param string $key Cache internal key name
|
||||
*
|
||||
* @param bool True on success, False on failure
|
||||
*/
|
||||
protected function delete_item($key)
|
||||
{
|
||||
if (!self::$memcache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// #1488592: use 2nd argument
|
||||
$result = self::$memcache->delete($key, 0);
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('delete', $key, null, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+261
@@ -0,0 +1,261 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
+-----------------------------------------------------------------------+
|
||||
| This file is part of the Roundcube Webmail client |
|
||||
| |
|
||||
| Copyright (C) The Roundcube Dev Team |
|
||||
| Copyright (C) Kolab Systems AG |
|
||||
| |
|
||||
| Licensed under the GNU General Public License version 3 or |
|
||||
| any later version with exceptions for skins & plugins. |
|
||||
| See the README file for a full license statement. |
|
||||
| |
|
||||
| PURPOSE: |
|
||||
| Caching engine - Redis |
|
||||
+-----------------------------------------------------------------------+
|
||||
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
||||
| Author: Aleksander Machniak <alec@alec.pl> |
|
||||
+-----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface implementation class for accessing Redis cache
|
||||
*
|
||||
* @package Framework
|
||||
* @subpackage Cache
|
||||
*/
|
||||
class rcube_cache_redis extends rcube_cache
|
||||
{
|
||||
/**
|
||||
* Instance of Redis object
|
||||
*
|
||||
* @var Redis
|
||||
*/
|
||||
protected static $redis;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($userid, $prefix = '', $ttl = 0, $packed = true, $indexed = false)
|
||||
{
|
||||
parent::__construct($userid, $prefix, $ttl, $packed, $indexed);
|
||||
|
||||
$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([
|
||||
'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([
|
||||
'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([
|
||||
'code' => 604,
|
||||
'type' => 'redis',
|
||||
'line' => __LINE__,
|
||||
'file' => __FILE__,
|
||||
'message' => "Redis cluster not yet supported"
|
||||
],
|
||||
true, true);
|
||||
}
|
||||
|
||||
self::$redis = new Redis;
|
||||
$failures = 0;
|
||||
|
||||
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 = 0;
|
||||
}
|
||||
else {
|
||||
// set default values if not set
|
||||
$host = $host ?: '127.0.0.1';
|
||||
$port = $port ?: 6379;
|
||||
}
|
||||
|
||||
try {
|
||||
if (self::$redis->connect($host, $port) === false) {
|
||||
throw new Exception("Could not connect to Redis server. Please check host and port.");
|
||||
}
|
||||
|
||||
if ($password !== null && self::$redis->auth($password) === false) {
|
||||
throw new Exception("Could not authenticate with Redis server. Please check password.");
|
||||
}
|
||||
|
||||
if ($database !== null && self::$redis->select($database) === false) {
|
||||
throw new Exception("Could not select Redis database. Please check database setting.");
|
||||
}
|
||||
}
|
||||
catch (Exception $e) {
|
||||
rcube::raise_error($e, true, false);
|
||||
$failures++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($hosts) === $failures) {
|
||||
self::$redis = false;
|
||||
}
|
||||
|
||||
if (self::$redis) {
|
||||
try {
|
||||
$ping = self::$redis->ping();
|
||||
if ($ping !== true && $ping !== "+PONG") {
|
||||
throw new Exception("Redis connection failure. Ping failed.");
|
||||
}
|
||||
}
|
||||
catch (Exception $e) {
|
||||
self::$redis = false;
|
||||
rcube::raise_error($e, true, 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;
|
||||
}
|
||||
|
||||
try {
|
||||
$data = self::$redis->get($key);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
rcube::raise_error($e, true, false);
|
||||
return false;
|
||||
}
|
||||
|
||||
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 bool True on success, False on failure
|
||||
*/
|
||||
protected function add_item($key, $data)
|
||||
{
|
||||
if (!self::$redis) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$result = self::$redis->setEx($key, $this->ttl, $data);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
rcube::raise_error($e, true, false);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('set', $key, $data, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes entry from Redis.
|
||||
*
|
||||
* @param string $key Cache internal key name
|
||||
*
|
||||
* @param bool True on success, False on failure
|
||||
*/
|
||||
protected function delete_item($key)
|
||||
{
|
||||
if (!self::$redis) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$fname = method_exists(self::$redis, 'del') ? 'del' : 'delete';
|
||||
$result = self::$redis->$fname($key);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
rcube::raise_error($e, true, false);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->debug) {
|
||||
$this->debug('delete', $key, null, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user