MDL-34290 remove class cache_file as not used

in fact we have moodle filepool that can work perfectly for caching files, no need to create new class and storage
This commit is contained in:
Marina Glancy 2012-07-31 11:08:25 +08:00
parent 75dd40b265
commit db02d84a40
2 changed files with 0 additions and 171 deletions

View File

@ -466,10 +466,6 @@ function cron_run() {
$fs = get_file_storage();
$fs->cron();
mtrace("Clean up cached external files");
// 1 week
cache_file::cleanup(array(), 60 * 60 * 24 * 7);
mtrace("Cron script completed correctly");
$difftime = microtime_diff($starttime, microtime());

View File

@ -4271,170 +4271,3 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null) {
}
}
/**
* Universe file cacheing class
*
* @package core_files
* @category files
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cache_file {
/** @var string */
public $cachedir = '';
/**
* static method to create cache_file class instance
*
* @param array $options caching ooptions
*/
public static function get_instance($options = array()) {
return new cache_file($options);
}
/**
* Constructor
*
* @param array $options
*/
private function __construct($options = array()) {
global $CFG;
// Path to file caches.
if (isset($options['cachedir'])) {
$this->cachedir = $options['cachedir'];
} else {
$this->cachedir = $CFG->cachedir . '/filedir';
}
// Create cache directory.
if (!file_exists($this->cachedir)) {
mkdir($this->cachedir, $CFG->directorypermissions, true);
}
// When use cache_file::get, it will check ttl.
if (isset($options['ttl']) && is_numeric($options['ttl'])) {
$this->ttl = $options['ttl'];
} else {
// One day.
$this->ttl = 60 * 60 * 24;
}
}
/**
* Get cached file, false if file expires
*
* @param mixed $param
* @param array $options caching options
* @return bool|string
*/
public static function get($param, $options = array()) {
$instance = self::get_instance($options);
$filepath = $instance->generate_filepath($param);
if (file_exists($filepath)) {
$lasttime = filemtime($filepath);
if (time() - $lasttime > $instance->ttl) {
// Remove cache file.
unlink($filepath);
return false;
} else {
return $filepath;
}
} else {
return false;
}
}
/**
* Static method to create cache from a file
*
* @param mixed $ref
* @param string $srcfile
* @param array $options
* @return string cached file path
*/
public static function create_from_file($ref, $srcfile, $options = array()) {
$instance = self::get_instance($options);
$cachedfilepath = $instance->generate_filepath($ref);
copy($srcfile, $cachedfilepath);
return $cachedfilepath;
}
/**
* Static method to create cache from url
*
* @param mixed $ref file reference
* @param string $url file url
* @param array $options options
* @return string cached file path
*/
public static function create_from_url($ref, $url, $options = array()) {
$instance = self::get_instance($options);
$cachedfilepath = $instance->generate_filepath($ref);
$fp = fopen($cachedfilepath, 'w');
$curl = new curl;
$curl->download(array(array('url'=>$url, 'file'=>$fp)));
// Must close file handler.
fclose($fp);
return $cachedfilepath;
}
/**
* Static method to create cache from string
*
* @param mixed $ref file reference
* @param string $url file url
* @param array $options options
* @return string cached file path
*/
public static function create_from_string($ref, $string, $options = array()) {
$instance = self::get_instance($options);
$cachedfilepath = $instance->generate_filepath($ref);
$fp = fopen($cachedfilepath, 'w');
fwrite($fp, $string);
// Must close file handler.
fclose($fp);
return $cachedfilepath;
}
/**
* Build path to cache file
*
* @param mixed $ref
* @return string
*/
private function generate_filepath($ref) {
global $CFG;
$hash = sha1(serialize($ref));
$l1 = $hash[0].$hash[1];
$l2 = $hash[2].$hash[3];
$dir = $this->cachedir . "/$l1/$l2";
if (!file_exists($dir)) {
mkdir($dir, $CFG->directorypermissions, true);
}
return "$dir/$hash";
}
/**
* Remove cache files
*
* @param array $options options
* @param int $expire The number of seconds before expiry
*/
public static function cleanup($options = array(), $expire) {
global $CFG;
$instance = self::get_instance($options);
if ($dir = opendir($instance->cachedir)) {
while (($file = readdir($dir)) !== false) {
if (!is_dir($file) && $file != '.' && $file != '..') {
$lasttime = @filemtime($instance->cachedir . $file);
if(time() - $lasttime > $expire){
@unlink($instance->cachedir . $file);
}
}
}
closedir($dir);
}
}
}