diff --git a/HandBrakeCluster/Cache.class.php b/HandBrakeCluster/Cache.class.php new file mode 100644 index 0000000..79ca03c --- /dev/null +++ b/HandBrakeCluster/Cache.class.php @@ -0,0 +1,60 @@ +config = $config; + $this->cache_dir = $config->get('cache.base_dir'); + + if (is_dir($cache_dir)) { + if ( ! is_writeable($cache_dir)) { + throw new HandBrakeCluster_Exception_InvalidCacheDir(); + } + } else { + if ( ! HandBrakeCluster_Main::mkdir_recursive($this->cache_dir)) { + throw new HandBrakeCluster_Exception_InvalidCacheDir(); + } + } + } + + protected function cacheFilename($source_filename) { + return $this->cache_dir . sha1($source_filename); + } + + public function exists($source_filename, $ttl = 3600) { + $cache_filename = $this->cacheFilename($source_filename); + + // Check to see if the file is cached + if (!file_exists($cache_filename)) { + return false; + } + + // Check to see if the cache has expired + if (filemtime($cache_filename) + $ttl < time()) { + return false; + } + + return true; + } + + public function store($source_filename, $content) { + $cache_filename = $this->cacheFilename($source_filename); + return file_put_contents($cache_filename, $content); + } + + public function fetch($source_filename, $ttl = 3600) { + $cache_filename = $this->cacheFilename($source_filename); + + if (!$this->exists($source_filename)) { + throw new HandBrakeCluster_Exception_CacheObjectNotFound($source_filename); + } + + return file_get_contents($cache_filename); + } + +}; + +?> \ No newline at end of file diff --git a/HandBrakeCluster/Main.class.php b/HandBrakeCluster/Main.class.php index 469e4a3..1227cd6 100644 --- a/HandBrakeCluster/Main.class.php +++ b/HandBrakeCluster/Main.class.php @@ -11,6 +11,7 @@ class HandBrakeCluster_Main { private $database; private $log; private $request; + private $cache; private function __construct() { $request_string = isset($_GET['l']) ? $_GET['l'] : ''; @@ -20,8 +21,9 @@ class HandBrakeCluster_Main { $this->config->setDatabase($this->database); $this->log = new HandBrakeCluster_Log($this->database, $this->config); - $this->request = new HandBrakeCluster_RequestParser($request_string); - + $this->request = new HandBrakeCluster_RequestParser($request_string); + $this->cache = new HandBrakeCluster_Cache($this->config); + $this->smarty = new Smarty(); $this->smarty->template_dir = './templates'; $this->smarty->compile_dir = './tmp/templates'; @@ -60,6 +62,10 @@ class HandBrakeCluster_Main { public function request() { return $this->request; } + + public function cache() { + return $this->cache; + } public static function initialise() { spl_autoload_register(array('HandBrakeCluster_Main','autoload')); @@ -93,6 +99,21 @@ class HandBrakeCluster_Main { require_once $filename; } } + + public static function mkdir_recursive($directory, $permissions=0777) { + $parts = explode('/', $directory); + $path = ''; + for ($i=1,$l=count($parts); $i<=$l; $i++) { + $iPath = $parts; + $path = join('/', array_slice($iPath, 0, $i)); + if (empty($path)) continue; + if (!file_exists($path)) { + if (!mkdir($path)) return false; + if (!chmod($path, $permissions)) return false; + } + } + return true; + } } HandBrakeCluster_Main::initialise();