123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- class getID3_cached_dbm extends getID3
- {
-
- private $dba;
-
- private $lock;
-
- private $cache_type;
-
- private $dbm_filename;
-
- public function __construct($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, $filesize=null, $original_filename='', $fp=null) {
- 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, $filesize, $original_filename, $fp);
-
- if (isset($key) && file_exists($filename)) {
- dba_insert($key, serialize($result), $this->dba);
- }
- return $result;
- }
- }
|