Added a simple cache implementation
This commit is contained in:
60
HandBrakeCluster/Cache.class.php
Normal file
60
HandBrakeCluster/Cache.class.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
?>
|
||||||
@@ -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'] : '';
|
||||||
@@ -21,6 +22,7 @@ class HandBrakeCluster_Main {
|
|||||||
|
|
||||||
$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();
|
||||||
|
|||||||
Reference in New Issue
Block a user