|
Size: | 2411 |
Storage flags: | strip |
<?php
class getID3_cached_dbm extends getID3
{
public function getID3_cached_dbm($cache_type, $dbm_filename, $lock_filename) {
if (!extension_loaded('dba')) {
throw new Exception('PHP is not compiled with dba support, required to use DBM style cache.');
}
if (!function_exists('dba_handlers') || !in_array($cache_type, dba_handlers())) {
throw new Exception('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
}
if (!file_exists($lock_filename)) {
if (!touch($lock_filename)) {
throw new Exception('failed to create lock file: '.$lock_filename);
}
}
if (!is_writeable($lock_filename)) {
throw new Exception('lock file: '.$lock_filename.' is not writable');
}
$this->lock = fopen($lock_filename, 'w');
flock($this->lock, LOCK_EX);
if (!file_exists($dbm_filename)) {
if (!touch($dbm_filename)) {
throw new Exception('failed to create dbm file: '.$dbm_filename);
}
}
$this->dba = dba_open($dbm_filename, 'w', $cache_type);
if (!$this->dba) {
$this->dba = dba_open($dbm_filename, 'n', $cache_type);
if (!$this->dba) {
throw new Exception('failed to create dbm file: '.$dbm_filename);
}
dba_insert(getID3::VERSION, getID3::VERSION, $this->dba);
}
$this->cache_type = $cache_type;
$this->dbm_filename = $dbm_filename;
register_shutdown_function(array($this, '__destruct'));
if (dba_fetch(getID3::VERSION, $this->dba) != getID3::VERSION) {
$this->clear_cache();
}
parent::__construct();
}
public function __destruct() {
dba_close($this->dba);
flock($this->lock, LOCK_UN);
fclose($this->lock);
}
public function clear_cache() {
dba_close($this->dba);
$this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type);
if (!$this->dba) {
throw new Exception('failed to clear cache/recreate dbm file: '.$this->dbm_filename);
}
dba_insert(getID3::VERSION, getID3::VERSION, $this->dba);
register_shutdown_function(array($this, '__destruct'));
}
public function analyze($filename) {
if (file_exists($filename)) {
$key = $filename.'::'.filemtime($filename).'::'.filesize($filename);
$result = dba_fetch($key, $this->dba);
if ($result !== false) {
return unserialize($result);
}
}
$result = parent::analyze($filename);
if (file_exists($filename)) {
dba_insert($key, serialize($result), $this->dba);
}
return $result;
}
}