Added a simple cache implementation

This commit is contained in:
2010-03-21 23:14:22 +00:00
parent 50283e9a9b
commit a5b57b3f44
2 changed files with 83 additions and 2 deletions

View File

@@ -0,0 +1,60 @@
<?php
class HandBrakeCluster_Cache {
protected $config;
protected $cache_dir;
public function __construct(HandBrakeCluster_Config $config) {
$this->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);
}
};
?>

View File

@@ -11,6 +11,7 @@ class HandBrakeCluster_Main {
private $database; private $database;
private $log; private $log;
private $request; private $request;
private $cache;
private function __construct() { private function __construct() {
$request_string = isset($_GET['l']) ? $_GET['l'] : ''; $request_string = isset($_GET['l']) ? $_GET['l'] : '';
@@ -20,7 +21,8 @@ class HandBrakeCluster_Main {
$this->config->setDatabase($this->database); $this->config->setDatabase($this->database);
$this->log = new HandBrakeCluster_Log($this->database, $this->config); $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 = new Smarty();
$this->smarty->template_dir = './templates'; $this->smarty->template_dir = './templates';
@@ -61,6 +63,10 @@ class HandBrakeCluster_Main {
return $this->request; return $this->request;
} }
public function cache() {
return $this->cache;
}
public static function initialise() { public static function initialise() {
spl_autoload_register(array('HandBrakeCluster_Main','autoload')); spl_autoload_register(array('HandBrakeCluster_Main','autoload'));
} }
@@ -93,6 +99,21 @@ class HandBrakeCluster_Main {
require_once $filename; 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(); HandBrakeCluster_Main::initialise();