Skip to content

Commit

Permalink
[Patch] Unhardcoding access to redis memcache server
Browse files Browse the repository at this point in the history
Unhardcoding access to memcache in order to use it outside EOS cache
system.
  • Loading branch information
nadir committed Apr 11, 2016
1 parent 6f9d689 commit b481f77
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 79 deletions.
78 changes: 13 additions & 65 deletions lib/private/files/objectstore/eosmemcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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]));
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
Expand Down
15 changes: 1 addition & 14 deletions lib/private/files/objectstore/eosreqcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
109 changes: 109 additions & 0 deletions lib/private/files/objectstore/redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace OC\Files\ObjectStore;

class Redis
{
private static $redisInstance;

private static function init()
{
if(self::$redisInstance == null)
{
self::$redisInstance = new \Redis();

if(!self::$redisInstance->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);
}
}
}

0 comments on commit b481f77

Please sign in to comment.