only use memcache, if available

This commit is contained in:
Arthur Schiwon 2015-05-08 13:27:27 +02:00
parent ebf3953908
commit 90611e6594
1 changed files with 17 additions and 2 deletions

View File

@ -37,12 +37,18 @@ abstract class Proxy {
static private $accesses = array(); static private $accesses = array();
private $ldap = null; private $ldap = null;
/** @var \OCP\ICache|null */
private $cache;
/** /**
* @param ILDAPWrapper $ldap * @param ILDAPWrapper $ldap
*/ */
public function __construct(ILDAPWrapper $ldap) { public function __construct(ILDAPWrapper $ldap) {
$this->ldap = $ldap; $this->ldap = $ldap;
$this->cache = \OC\Cache::getGlobalCache(); $memcache = \OC::$server->getMemCacheFactory();
if($memcache->isAvailable()) {
$this->cache = $memcache->create();
}
} }
/** /**
@ -151,7 +157,7 @@ abstract class Proxy {
* @return mixed|null * @return mixed|null
*/ */
public function getFromCache($key) { public function getFromCache($key) {
if(!$this->isCached($key)) { if(is_null($this->cache) || !$this->isCached($key)) {
return null; return null;
} }
$key = $this->getCacheKey($key); $key = $this->getCacheKey($key);
@ -164,6 +170,9 @@ abstract class Proxy {
* @return bool * @return bool
*/ */
public function isCached($key) { public function isCached($key) {
if(is_null($this->cache)) {
return false;
}
$key = $this->getCacheKey($key); $key = $this->getCacheKey($key);
return $this->cache->hasKey($key); return $this->cache->hasKey($key);
} }
@ -173,12 +182,18 @@ abstract class Proxy {
* @param mixed $value * @param mixed $value
*/ */
public function writeToCache($key, $value) { public function writeToCache($key, $value) {
if(is_null($this->cache)) {
return;
}
$key = $this->getCacheKey($key); $key = $this->getCacheKey($key);
$value = base64_encode(serialize($value)); $value = base64_encode(serialize($value));
$this->cache->set($key, $value, '2592000'); $this->cache->set($key, $value, '2592000');
} }
public function clearCache() { public function clearCache() {
if(is_null($this->cache)) {
return;
}
$this->cache->clear($this->getCacheKey(null)); $this->cache->clear($this->getCacheKey(null));
} }
} }