2012-06-18 13:12:53 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2012-06-18 13:12:53 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-17 19:46:33 +04:00
|
|
|
namespace OC\Cache;
|
2012-06-18 13:12:53 +04:00
|
|
|
|
2013-09-17 19:46:33 +04:00
|
|
|
class FileGlobal {
|
2012-08-29 00:39:05 +04:00
|
|
|
static protected function getCacheDir() {
|
2013-09-18 17:02:25 +04:00
|
|
|
$cache_dir = get_temp_dir().'/owncloud-' . \OC_Util::getInstanceId().'/';
|
2012-06-18 13:12:53 +04:00
|
|
|
if (!is_dir($cache_dir)) {
|
|
|
|
mkdir($cache_dir);
|
|
|
|
}
|
|
|
|
return $cache_dir;
|
|
|
|
}
|
|
|
|
|
2012-06-18 13:33:24 +04:00
|
|
|
protected function fixKey($key) {
|
|
|
|
return str_replace('/', '_', $key);
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
*/
|
2012-06-18 13:12:53 +04:00
|
|
|
public function get($key) {
|
2012-06-18 13:33:24 +04:00
|
|
|
$key = $this->fixKey($key);
|
2012-06-18 13:12:53 +04:00
|
|
|
if ($this->hasKey($key)) {
|
2012-08-29 00:39:05 +04:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-06-18 13:12:53 +04:00
|
|
|
return file_get_contents($cache_dir.$key);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*/
|
2012-06-18 13:12:53 +04:00
|
|
|
public function set($key, $value, $ttl=0) {
|
2012-06-18 13:33:24 +04:00
|
|
|
$key = $this->fixKey($key);
|
2012-08-29 00:39:05 +04:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-06-18 13:12:53 +04: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 13:33:24 +04:00
|
|
|
$key = $this->fixKey($key);
|
2012-08-29 00:39:05 +04:00
|
|
|
$cache_dir = self::getCacheDir();
|
2015-02-03 12:13:25 +03:00
|
|
|
if ($cache_dir && is_file($cache_dir.$key) && is_readable($cache_dir.$key)) {
|
2012-06-18 13:12:53 +04:00
|
|
|
$mtime = filemtime($cache_dir.$key);
|
|
|
|
if ($mtime < time()) {
|
|
|
|
unlink($cache_dir.$key);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove($key) {
|
2012-08-29 00:39:05 +04:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!$cache_dir) {
|
2012-06-18 13:12:53 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-18 13:33:24 +04:00
|
|
|
$key = $this->fixKey($key);
|
2012-06-18 13:12:53 +04:00
|
|
|
return unlink($cache_dir.$key);
|
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function clear($prefix='') {
|
2012-08-29 00:39:05 +04:00
|
|
|
$cache_dir = self::getCacheDir();
|
2012-09-07 15:16:50 +04:00
|
|
|
$prefix = $this->fixKey($prefix);
|
2012-09-07 17:22:01 +04:00
|
|
|
if($cache_dir and is_dir($cache_dir)) {
|
2012-06-18 13:12:53 +04:00
|
|
|
$dh=opendir($cache_dir);
|
2013-09-04 15:06:04 +04: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 13:12:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|