2012-06-18 09:12:53 +00:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2013-09-17 15:46:33 +00:00
|
|
|
namespace OC\Cache;
|
2012-06-18 09:12:53 +00:00
|
|
|
|
2013-09-17 15:46:33 +00:00
|
|
|
class FileGlobal {
|
2012-08-28 20:39:05 +00:00
|
|
|
static protected function getCacheDir() {
|
2013-09-18 13:02:25 +00:00
|
|
|
$cache_dir = get_temp_dir().'/owncloud-' . \OC_Util::getInstanceId().'/';
|
2012-06-18 09:12:53 +00:00
|
|
|
if (!is_dir($cache_dir)) {
|
|
|
|
mkdir($cache_dir);
|
|
|
|
}
|
|
|
|
return $cache_dir;
|
|
|
|
}
|
|
|
|
|
2012-06-18 09:33:24 +00:00
|
|
|
protected function fixKey($key) {
|
|
|
|
return str_replace('/', '_', $key);
|
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
*/
|
2012-06-18 09:12:53 +00:00
|
|
|
public function get($key) {
|
2012-06-18 09:33:24 +00:00
|
|
|
$key = $this->fixKey($key);
|
2012-06-18 09:12:53 +00:00
|
|
|
if ($this->hasKey($key)) {
|
2012-08-28 20:39:05 +00:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-06-18 09:12:53 +00:00
|
|
|
return file_get_contents($cache_dir.$key);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*/
|
2012-06-18 09:12:53 +00:00
|
|
|
public function set($key, $value, $ttl=0) {
|
2012-06-18 09:33:24 +00:00
|
|
|
$key = $this->fixKey($key);
|
2012-08-28 20:39:05 +00:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-06-18 09:12:53 +00:00
|
|
|
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) {
|
2012-06-18 09:33:24 +00:00
|
|
|
$key = $this->fixKey($key);
|
2012-08-28 20:39:05 +00:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-06-18 09:12:53 +00:00
|
|
|
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) {
|
2012-08-28 20:39:05 +00:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!$cache_dir) {
|
2012-06-18 09:12:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-18 09:33:24 +00:00
|
|
|
$key = $this->fixKey($key);
|
2012-06-18 09:12:53 +00:00
|
|
|
return unlink($cache_dir.$key);
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
public function clear($prefix='') {
|
2012-08-28 20:39:05 +00:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-09-07 11:16:50 +00:00
|
|
|
$prefix = $this->fixKey($prefix);
|
2012-09-07 13:22:01 +00:00
|
|
|
if($cache_dir and is_dir($cache_dir)) {
|
2012-06-18 09:12:53 +00:00
|
|
|
$dh=opendir($cache_dir);
|
2013-09-04 11:06:04 +00:00
|
|
|
if(is_resource($dh)) {
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
|
|
|
if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
|
|
|
|
unlink($cache_dir.$file);
|
|
|
|
}
|
2012-06-18 09:12:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 20:39:05 +00:00
|
|
|
|
|
|
|
static public function gc() {
|
2014-02-13 15:28:49 +00:00
|
|
|
$appConfig = \OC::$server->getAppConfig();
|
|
|
|
$last_run = $appConfig->getValue('core', 'global_cache_gc_lastrun', 0);
|
2012-08-28 20:39:05 +00:00
|
|
|
$now = time();
|
|
|
|
if (($now - $last_run) < 300) {
|
|
|
|
// only do cleanup every 5 minutes
|
|
|
|
return;
|
|
|
|
}
|
2014-02-13 15:28:49 +00:00
|
|
|
$appConfig->setValue('core', 'global_cache_gc_lastrun', $now);
|
2012-08-28 20:39:05 +00:00
|
|
|
$cache_dir = self::getCacheDir();
|
|
|
|
if($cache_dir and is_dir($cache_dir)) {
|
|
|
|
$dh=opendir($cache_dir);
|
2013-09-04 11:06:04 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-08-28 20:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-18 09:12:53 +00:00
|
|
|
}
|