From db02d84a40fd282c9d74b5f02315515c15033e9c Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Tue, 31 Jul 2012 11:08:25 +0800 Subject: [PATCH] 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 --- lib/cronlib.php | 4 -- lib/filelib.php | 167 ------------------------------------------------ 2 files changed, 171 deletions(-) diff --git a/lib/cronlib.php b/lib/cronlib.php index 457336af0c9..c0b86afb8c1 100644 --- a/lib/cronlib.php +++ b/lib/cronlib.php @@ -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()); diff --git a/lib/filelib.php b/lib/filelib.php index 14217a72c9a..cef949a58de 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -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); - } - } -}