* @copyright Curverider Ltd 2008 * @link http://elgg.org/ */ /** * @class ElggCache The elgg cache superclass. * This defines the interface for a cache (wherever that cache is stored). * @author Marcus Povey */ abstract class ElggCache { /** * Variables for the cache object. * * @var array */ private $variables; /** * Set the constructor. */ function __construct() { $this->variables = array(); } /** * Set a cache variable. * * @param string $variable * @param string $value */ public function set_variable($variable, $value) { $this->variables[$variable] = $value; } /** * Get variables for this cache. * * @param string $variable * @return mixed The variable or null; */ public function get_variable($variable) { if (isset($this->variables[$variable])) return $this->variables[$variable]; return null; } /** * Save data in a cache. * * @param string $key * @param string $data * @return bool */ abstract public function save($key, $data); /** * 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, $offset = 0, $limit = null); } /** * @class ElggFileCache * Store cached data in a file store. * @author Marcus Povey */ 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 } } ?>