diff options
author | icewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-03-11 18:09:58 +0000 |
---|---|---|
committer | icewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-03-11 18:09:58 +0000 |
commit | 4142d87fad92f7d54a51798fad8d892af2f8a246 (patch) | |
tree | 68795b06270ebd0e552bdf45763cffff06bff6db | |
parent | 90e40c96f7cb5e77837ea1d924fc6a7e23b31725 (diff) | |
download | elgg-4142d87fad92f7d54a51798fad8d892af2f8a246.tar.gz elgg-4142d87fad92f7d54a51798fad8d892af2f8a246.tar.bz2 |
Marcus Povey <marcus@dushka.co.uk>
* File cache outline
git-svn-id: https://code.elgg.org/elgg/trunk@173 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r-- | engine/lib/cache.php | 95 |
1 files changed, 94 insertions, 1 deletions
diff --git a/engine/lib/cache.php b/engine/lib/cache.php index 5a426cdee..66e467c86 100644 --- a/engine/lib/cache.php +++ b/engine/lib/cache.php @@ -65,8 +65,101 @@ * Load data from the cache using a given key. * * @param string $key + * @param int $offset + * @param int $limit * @return mixed The stored data or false. */ - abstract public function load($key); + abstract public function load($key, $offset = 0, $limit = null); + } + + /** + * @class ElggFileCache + * Store cached data in a file store. + * @author Marcus Povey <marcus@dushka.co.uk> + */ + class ElggFileCache extends ElggCache + { + /** + * Set the Elgg cache. + * + * @param string $cache_path The cache path. + * @param int $max_age Maximum age in seconds, 0 if no limit. + * @param int $max_size Maximum size of cache in seconds, 0 if no limit. + */ + function __construct($cache_path, $max_age = 0, $max_size = 0) + { + set_variable("cache_path", $cache_path); + set_variable("max_age", $max_age); + set_variable("max_size", $max_size); + + if ($cache_path=="") throw new ConfigurationException("Cache path set to nothing!"); + } + + /** + * Create and return a handle to a file. + * + * @param string $filename + * @param string $rw + */ + protected function create_file($filename, $rw = "rb") + { + // Create a filename matrix + $matrix = ""; + for ($n = 0; $n < strlen($filename); $n++) + $matrix .= $filename[$n] . "/"; + + // Create full path + $path = $this->set_variable("cache_path") . $matrix; + if (!mkdir($path, 0700, true)) throw new IOException("Could not make $path"); + + // Open the file + return fopen($path . $filename, $rw); + } + + /** + * Create a sanitised filename for the file. + * + * @param string $filename + */ + protected function sanitise_filename($filename) + { + // TODO : Writeme + + return $filename; + } + + public function save($key, $data) + { + $f = $this->create_file($this->sanitise_filename($key), "wb"); + if ($f) + { + $result = fwrite($f, $data); + fclose($f); + + return $result; + } + + return false; + } + + public function load($key, $offset = 0, $limit = null) + { + $f = $this->create_file($this->sanitise_filename($key)); + if ($f) + { + fseek($f, $offset); + $data = stream_get_contents($f, $limit, $offset); + fclose($f); + + return $data; + } + + return false; + } + + public function __destruct() + { + // TODO: Check size and age, clean up accordingly + } } ?>
\ No newline at end of file |