From b481f77ce77c87bf5a1d3d877131e9fdb82122e1 Mon Sep 17 00:00:00 2001 From: nadir Date: Mon, 11 Apr 2016 14:43:16 +0200 Subject: [PATCH] [Patch] Unhardcoding access to redis memcache server Unhardcoding access to memcache in order to use it outside EOS cache system. --- lib/private/files/objectstore/eosmemcache.php | 78 +++---------- lib/private/files/objectstore/eosreqcache.php | 15 +-- lib/private/files/objectstore/redis.php | 109 ++++++++++++++++++ 3 files changed, 123 insertions(+), 79 deletions(-) create mode 100644 lib/private/files/objectstore/redis.php diff --git a/lib/private/files/objectstore/eosmemcache.php b/lib/private/files/objectstore/eosmemcache.php index 81d8232b264c..0c7f7e9b1566 100644 --- a/lib/private/files/objectstore/eosmemcache.php +++ b/lib/private/files/objectstore/eosmemcache.php @@ -22,37 +22,8 @@ class EosMemCache implements IEosCache /** @var string Cache key for files identified by eospath and a given depth */ const KEY_FILEINFO_BY_PATH = 'getFileInfoByEosPath'; - /** @var Redis redis client object */ - private $redisClient; - /** @var bool stores whether cache system is available or not */ - private $unableToConnect; - public function __construct() { - $this->init(); - } - - /** - * Stablish (if not done already) and test the connection to the cache - * server on every call - * - * @return bool True if server is available, false otherwise - */ - private function init() - { - if($this->redisClient == NULL) - { - $this->unableToConnect = false; - $this->redisClient = new \Redis(); - - if(!$this->redisClient->connect('127.0.0.1', 6379)) - { - $this->unableToConnect = true; - \OCP\Util::writeLog('EOS MEMCACHE', 'Unable to connect to redis server on 127.0.0.1:6379', \OCP\Util::ERROR); - } - } - - return !$this->unableToConnect; } /** @@ -210,14 +181,7 @@ public function getFileInfoByEosPath($depth, $eosPath) */ private function writeToCache($outerKey, $key, $value) { - if($this->init()) - { - $this->redisClient->hSet($outerKey, $key, json_encode([time(), $value])); - } - else - { - \OCP\Util::writeLog('EOS MEMCACHE', 'Unable to access redis server', \OCP\Util::ERROR); - } + Redis::writeToCacheMap($outerKey, $key, json_encode([time(), $value])); } /** @@ -228,14 +192,7 @@ private function writeToCache($outerKey, $key, $value) */ private function deleteFromCache($outerKey, $key) { - if($this->init()) - { - $this->redisClient->hDel($outerKey, $key); - } - else - { - \OCP\Util::writeLog('EOS MEMCACHE', 'Unable to access memcache', \OCP\Util::ERROR); - } + Redis::deleteFromCacheMap($outerKey, $key); } /** @@ -247,29 +204,20 @@ private function deleteFromCache($outerKey, $key) */ private function readFromCache($outerKey, $key) { - $value = NULL; - if($this->init()) + $value = Redis::readFromCacheMap($outerKey, $key); + if($value !== FALSE) { - $value = $this->redisClient->hGet($outerKey, $key); - // Key found - if($value !== FALSE) + // Check for expire date + $value = json_decode($value, TRUE); + $elapsed = time() - (int)$value[0]; + if($elapsed > self::EXPIRE_TIME_SECONDS) { - // Check for expire date - $value = json_decode($value, TRUE); - $elapsed = time() - (int)$value[0]; - if($elapsed > self::EXPIRE_TIME_SECONDS) - { - $value = FALSE; - } - else - { - $value = $value[1]; - } + $value = FALSE; + } + else + { + $value = $value[1]; } - } - else - { - \OCP\Util::writeLog('EOS MEMCACHE', 'Unable to access memcache', \OCP\Util::ERROR); } return $value; diff --git a/lib/private/files/objectstore/eosreqcache.php b/lib/private/files/objectstore/eosreqcache.php index b801523c6694..db2ef129124d 100644 --- a/lib/private/files/objectstore/eosreqcache.php +++ b/lib/private/files/objectstore/eosreqcache.php @@ -79,25 +79,12 @@ public function setFileById($id, $data) { $this->init(); $GLOBALS['cernbox']['getfilebyid'][$id] = $data; } - - private function dontUseCache() { - $avoid_paths = \OCP\Config::getSystemValue("avoid_req_cache_paths", array()); - foreach($avoid_paths as $path) { - if(strpos($_SERVER['REQUEST_URI'], $path) !== false) { - return true; - } - } - return FALSE; - } - + /** * {@inheritDoc} * @see IEosCache::getMeta() */ public function getMeta($ocPath) { - if ($this->dontUseCache ()) { - return FALSE; - } $this->init (); if (isset ( $GLOBALS ['cernbox'] ['getmeta'] [$ocPath] )) { // \OCP\Util::writeLog('EOSCACHE',"HIT op:" . __FUNCTION__ . "(ocpath:$ocPath)", \OCP\Util::ERROR); diff --git a/lib/private/files/objectstore/redis.php b/lib/private/files/objectstore/redis.php new file mode 100644 index 000000000000..ed11983d0ba4 --- /dev/null +++ b/lib/private/files/objectstore/redis.php @@ -0,0 +1,109 @@ +connect('127.0.0.1', 6379)) + { + \OCP\Util::writeLog('EOS MEMCACHE', 'Unable to connect to redis server on 127.0.0.1:6379', \OCP\Util::ERROR); + self::$redisInstance = null; + return false; + } + } + + return true; + } + + public static function writeToCache($key, $value) + { + if(self::init()) + { + self::$redisInstance->set($key, $value); + } + else + { + \OCP\Util::writeLog('REDIS MEMCACHE', 'Unable to access redis server', \OCP\Util::ERROR); + } + } + + public static function readFromCache($key) + { + if(self::init()) + { + self::$redisInstance->get($key); + } + else + { + \OCP\Util::writeLog('REDIS MEMCACHE', 'Unable to access redis server', \OCP\Util::ERROR); + } + } + + public static function deleteFromCache($key) + { + if(self::init()) + { + self::$redisInstance->del($key); + } + else + { + \OCP\Util::writeLog('REDIS MEMCACHE', 'Unable to access redis server', \OCP\Util::ERROR); + } + } + + public static function writeToCacheMap($hash, $key, $value) + { + if(self::init()) + { + self::$redisInstance->hSet($hash, $key, $value); + } + else + { + \OCP\Util::writeLog('REDIS MEMCACHE', 'Unable to access redis server', \OCP\Util::ERROR); + } + } + + public static function readHashFromCacheMap($hash) + { + if(self::init()) + { + return self::$redisInstance->hGetALl($hash); + } + else + { + \OCP\Util::writeLog('REDIS MEMCACHE', 'Unable to access memcache', \OCP\Util::ERROR); + } + } + + public static function readFromCacheMap($hash, $key) + { + if(self::init()) + { + return self::$redisInstance->hGet($hash, $key); + } + else + { + \OCP\Util::writeLog('REDIS MEMCACHE', 'Unable to access memcache', \OCP\Util::ERROR); + } + } + + public static function deleteFromCacheMap($hash, $key) + { + if(self::init()) + { + self::$redisInstance->hDel($hash, $key); + } + else + { + \OCP\Util::writeLog('REDIS MEMCACHE', 'Unable to access memcache', \OCP\Util::ERROR); + } + } +} \ No newline at end of file