diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2013-10-07 17:47:54 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2013-10-07 17:47:54 +0200 |
commit | 5e397d89c4152aee48692b1d302f1f11b16838b3 (patch) | |
tree | 1f9e20c9ad153b3d1dd2ad077c1f97a8da8a7815 /lib/private/cache | |
parent | a2301e8b6975a213f05468f171229f7a1986a377 (diff) | |
parent | a3d0e29aa5df3390929c6378bd4f9c6bbf25c473 (diff) | |
download | nextcloud-server-5e397d89c4152aee48692b1d302f1f11b16838b3.tar.gz nextcloud-server-5e397d89c4152aee48692b1d302f1f11b16838b3.zip |
Merge branch 'master' into fixing-4546-master
Conflicts:
lib/private/connector/sabre/directory.php
Diffstat (limited to 'lib/private/cache')
-rw-r--r-- | lib/private/cache/broker.php | 63 | ||||
-rw-r--r-- | lib/private/cache/file.php | 136 | ||||
-rw-r--r-- | lib/private/cache/fileglobal.php | 106 | ||||
-rw-r--r-- | lib/private/cache/fileglobalgc.php | 9 | ||||
-rw-r--r-- | lib/private/cache/usercache.php | 77 |
5 files changed, 391 insertions, 0 deletions
diff --git a/lib/private/cache/broker.php b/lib/private/cache/broker.php new file mode 100644 index 00000000000..9b7e837e1bc --- /dev/null +++ b/lib/private/cache/broker.php @@ -0,0 +1,63 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Cache; + +class Broker { + + /** + * @var \OC\Cache + */ + protected $fast_cache; + + /** + * @var \OC\Cache + */ + protected $slow_cache; + + public function __construct($fast_cache, $slow_cache) { + $this->fast_cache = $fast_cache; + $this->slow_cache = $slow_cache; + } + + public function get($key) { + if ($r = $this->fast_cache->get($key)) { + return $r; + } + return $this->slow_cache->get($key); + } + + public function set($key, $value, $ttl=0) { + if (!$this->fast_cache->set($key, $value, $ttl)) { + if ($this->fast_cache->hasKey($key)) { + $this->fast_cache->remove($key); + } + return $this->slow_cache->set($key, $value, $ttl); + } + return true; + } + + public function hasKey($key) { + if ($this->fast_cache->hasKey($key)) { + return true; + } + return $this->slow_cache->hasKey($key); + } + + public function remove($key) { + if ($this->fast_cache->remove($key)) { + return true; + } + return $this->slow_cache->remove($key); + } + + public function clear($prefix='') { + $this->fast_cache->clear($prefix); + $this->slow_cache->clear($prefix); + } +} diff --git a/lib/private/cache/file.php b/lib/private/cache/file.php new file mode 100644 index 00000000000..b0738d2a92b --- /dev/null +++ b/lib/private/cache/file.php @@ -0,0 +1,136 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Cache; + +class File { + protected $storage; + protected function getStorage() { + if (isset($this->storage)) { + return $this->storage; + } + if(\OC_User::isLoggedIn()) { + \OC\Files\Filesystem::initMountPoints(\OC_User::getUser()); + $subdir = 'cache'; + $view = new \OC\Files\View('/' . \OC_User::getUser()); + if(!$view->file_exists($subdir)) { + $view->mkdir($subdir); + } + $this->storage = new \OC\Files\View('/' . \OC_User::getUser().'/'.$subdir); + return $this->storage; + }else{ + \OC_Log::write('core', 'Can\'t get cache storage, user not logged in', \OC_Log::ERROR); + return false; + } + } + + public function get($key) { + $result = null; + $proxyStatus = \OC_FileProxy::$enabled; + \OC_FileProxy::$enabled = false; + if ($this->hasKey($key)) { + $storage = $this->getStorage(); + $result = $storage->file_get_contents($key); + } + \OC_FileProxy::$enabled = $proxyStatus; + return $result; + } + + /** + * Returns the size of the stored/cached data + * + * @param $key + * @return int + */ + public function size($key) { + $result = 0; + $proxyStatus = \OC_FileProxy::$enabled; + \OC_FileProxy::$enabled = false; + if ($this->hasKey($key)) { + $storage = $this->getStorage(); + $result = $storage->filesize($key); + } + \OC_FileProxy::$enabled = $proxyStatus; + return $result; + } + + public function set($key, $value, $ttl=0) { + $storage = $this->getStorage(); + $result = false; + $proxyStatus = \OC_FileProxy::$enabled; + \OC_FileProxy::$enabled = false; + if ($storage and $storage->file_put_contents($key, $value)) { + if ($ttl === 0) { + $ttl = 86400; // 60*60*24 + } + $result = $storage->touch($key, time() + $ttl); + } + \OC_FileProxy::$enabled = $proxyStatus; + return $result; + } + + public function hasKey($key) { + $storage = $this->getStorage(); + if ($storage && $storage->is_file($key)) { + $mtime = $storage->filemtime($key); + if ($mtime < time()) { + $storage->unlink($key); + return false; + } + return true; + } + return false; + } + + public function remove($key) { + $storage = $this->getStorage(); + if(!$storage) { + return false; + } + return $storage->unlink($key); + } + + public function clear($prefix='') { + $storage = $this->getStorage(); + if($storage and $storage->is_dir('/')) { + $dh=$storage->opendir('/'); + if(is_resource($dh)) { + while (($file = readdir($dh)) !== false) { + if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) { + $storage->unlink('/'.$file); + } + } + } + } + return true; + } + + public function gc() { + $storage = $this->getStorage(); + if($storage and $storage->is_dir('/')) { + $now = time(); + $dh=$storage->opendir('/'); + if(!is_resource($dh)) { + return null; + } + while (($file = readdir($dh)) !== false) { + if($file!='.' and $file!='..') { + $mtime = $storage->filemtime('/'.$file); + if ($mtime < $now) { + $storage->unlink('/'.$file); + } + } + } + } + } + + public static function loginListener() { + $c = new self(); + $c->gc(); + } +} diff --git a/lib/private/cache/fileglobal.php b/lib/private/cache/fileglobal.php new file mode 100644 index 00000000000..bd049bba4d0 --- /dev/null +++ b/lib/private/cache/fileglobal.php @@ -0,0 +1,106 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Cache; + +class FileGlobal { + static protected function getCacheDir() { + $cache_dir = get_temp_dir().'/owncloud-' . \OC_Util::getInstanceId().'/'; + if (!is_dir($cache_dir)) { + mkdir($cache_dir); + } + return $cache_dir; + } + + protected function fixKey($key) { + return str_replace('/', '_', $key); + } + + public function get($key) { + $key = $this->fixKey($key); + if ($this->hasKey($key)) { + $cache_dir = self::getCacheDir(); + return file_get_contents($cache_dir.$key); + } + return null; + } + + public function set($key, $value, $ttl=0) { + $key = $this->fixKey($key); + $cache_dir = self::getCacheDir(); + if ($cache_dir and file_put_contents($cache_dir.$key, $value)) { + if ($ttl === 0) { + $ttl = 86400; // 60*60*24 + } + return touch($cache_dir.$key, time() + $ttl); + } + return false; + } + + public function hasKey($key) { + $key = $this->fixKey($key); + $cache_dir = self::getCacheDir(); + if ($cache_dir && is_file($cache_dir.$key)) { + $mtime = filemtime($cache_dir.$key); + if ($mtime < time()) { + unlink($cache_dir.$key); + return false; + } + return true; + } + return false; + } + + public function remove($key) { + $cache_dir = self::getCacheDir(); + if(!$cache_dir) { + return false; + } + $key = $this->fixKey($key); + return unlink($cache_dir.$key); + } + + public function clear($prefix='') { + $cache_dir = self::getCacheDir(); + $prefix = $this->fixKey($prefix); + if($cache_dir and is_dir($cache_dir)) { + $dh=opendir($cache_dir); + if(is_resource($dh)) { + while (($file = readdir($dh)) !== false) { + if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) { + unlink($cache_dir.$file); + } + } + } + } + } + + static public function gc() { + $last_run = \OC_AppConfig::getValue('core', 'global_cache_gc_lastrun', 0); + $now = time(); + if (($now - $last_run) < 300) { + // only do cleanup every 5 minutes + return; + } + \OC_AppConfig::setValue('core', 'global_cache_gc_lastrun', $now); + $cache_dir = self::getCacheDir(); + if($cache_dir and is_dir($cache_dir)) { + $dh=opendir($cache_dir); + if(is_resource($dh)) { + while (($file = readdir($dh)) !== false) { + if($file!='.' and $file!='..') { + $mtime = filemtime($cache_dir.$file); + if ($mtime < $now) { + unlink($cache_dir.$file); + } + } + } + } + } + } +} diff --git a/lib/private/cache/fileglobalgc.php b/lib/private/cache/fileglobalgc.php new file mode 100644 index 00000000000..399dd5e6f94 --- /dev/null +++ b/lib/private/cache/fileglobalgc.php @@ -0,0 +1,9 @@ +<?php + +namespace OC\Cache; + +class FileGlobalGC extends \OC\BackgroundJob\Job{ + public function run($argument){ + FileGlobal::gc(); + } +} diff --git a/lib/private/cache/usercache.php b/lib/private/cache/usercache.php new file mode 100644 index 00000000000..baa8820700b --- /dev/null +++ b/lib/private/cache/usercache.php @@ -0,0 +1,77 @@ +<?php +/** + * Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net) + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +namespace OC\Cache; + +/** + * This interface defines method for accessing the file based user cache. + */ +class UserCache implements \OCP\ICache { + + /** + * @var \OC\Cache\File $userCache + */ + protected $userCache; + + public function __construct() { + $this->userCache = new File(); + } + + /** + * Get a value from the user cache + * + * @param string $key + * @return mixed + */ + public function get($key) { + return $this->userCache->get($key); + } + + /** + * Set a value in the user cache + * + * @param string $key + * @param mixed $value + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 + * @return bool + */ + public function set($key, $value, $ttl = 0) { + if (empty($key)) { + return false; + } + return $this->userCache->set($key, $value, $ttl); + } + + /** + * Check if a value is set in the user cache + * + * @param string $key + * @return bool + */ + public function hasKey($key) { + return $this->userCache->hasKey($key); + } + + /** + * Remove an item from the user cache + * + * @param string $key + * @return bool + */ + public function remove($key) { + return $this->userCache->remove($key); + } + + /** + * clear the user cache of all entries starting with a prefix + * @param string $prefix (optional) + * @return bool + */ + public function clear($prefix = '') { + return $this->userCache->clear($prefix); + } +} |