source: extensions/charlies_content/getid3/getid3/extension.cache.dbm.php @ 3318

Last change on this file since 3318 was 3318, checked in by vdigital, 15 years ago

+ Add Charlies' content to depository

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1<?php
2// +----------------------------------------------------------------------+
3// | PHP version 5                                                        |
4// +----------------------------------------------------------------------+
5// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
6// +----------------------------------------------------------------------+
7// | This source file is subject to version 2 of the GPL license,         |
8// | that is bundled with this package in the file license.txt and is     |
9// | available through the world-wide-web at the following url:           |
10// | http://www.gnu.org/copyleft/gpl.html                                 |
11// +----------------------------------------------------------------------+
12// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
13// +----------------------------------------------------------------------+
14// | Authors: James Heinrich <infoØgetid3*org>                            |
15// |          Allan Hansen <ahØartemis*dk>                                |
16// +----------------------------------------------------------------------+
17// | extension.cache.mysql.php                                            |
18// | MySQL Cache Extension.                                               |
19// | dependencies: getid3.                                                |
20// +----------------------------------------------------------------------+
21//
22// $Id: extension.cache.dbm.php 3318 2009-05-20 21:54:10Z vdigital $
23
24
25/**
26* This is a caching extension for getID3(). It works the exact same
27* way as the getID3 class, but return cached information very fast
28*
29* Example:  (see also demo.cache.dbm.php in /demo/)
30*
31*    Normal getID3 usage (example):
32*
33*       require_once 'getid3/getid3.php';
34*       $getid3 = new getid3;
35*       $getid3->encoding = 'UTF-8';
36*       try {
37*           $info1 = $getid3->Analyse('file1.flac');
38*           $info2 = $getid3->Analyse('file2.wv');
39*           ....
40*
41*    getID3_cached usage:
42*
43*       require_once 'getid3/getid3.php';
44*       require_once 'getid3/getid3/extension.cache.mysql.php';
45*       $getid3 = new getid3_cached_mysql('localhost', 'database', 'username', 'password');
46*       $getid3->encoding = 'UTF-8';
47*       try {
48*           $info1 = $getid3->analyse('file1.flac');
49*           $info2 = $getid3->analyse('file2.wv');
50*           ...
51*
52*
53* Supported Cache Types
54*
55*   SQL Databases:          (use extension.cache.mysql)
56*
57*   cache_type          cache_options
58*   -------------------------------------------------------------------
59*   mysql               host, database, username, password
60*
61*
62*   DBM-Style Databases:    (this extension)
63*
64*   cache_type          cache_options
65*   -------------------------------------------------------------------
66*   gdbm                dbm_filename, lock_filename
67*   ndbm                dbm_filename, lock_filename
68*   db2                 dbm_filename, lock_filename
69*   db3                 dbm_filename, lock_filename
70*   db4                 dbm_filename, lock_filename  (PHP5 required)
71*
72*   PHP must have write access to both dbm_filename and lock_filename.
73*
74*
75* Recommended Cache Types
76*
77*   Infrequent updates, many reads      any DBM
78*   Frequent updates                    mysql
79*/
80
81
82class getid3_cached_dbm extends getid3
83{
84
85    public function __construct($cache_type, $dbm_filename, $lock_filename) {
86
87        // Check for dba extension
88        if (!extension_loaded('dba')) {
89            throw new getid3_exception('PHP is not compiled with dba support, required to use DBM style cache.');
90        }
91
92        if (!in_array($cache_type, dba_handlers())) {
93            throw new getid3_exception('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
94        }
95
96        // Create lock file if needed
97        if (!file_exists($lock_filename)) {
98            if (!touch($lock_filename)) {
99                die('failed to create lock file: ' . $lock_filename);
100            }
101        }
102
103        // Open lock file for writing
104        if (!is_writeable($lock_filename)) {
105            die('lock file: ' . $lock_filename . ' is not writable');
106        }
107        $this->lock = fopen($lock_filename, 'w');
108
109        // Acquire exclusive write lock to lock file
110        flock($this->lock, LOCK_EX);
111
112        // Create dbm-file if needed
113        if (!file_exists($dbm_filename)) {
114            if (!touch($dbm_filename)) {
115                die('failed to create dbm file: ' . $dbm_filename);
116            }
117        }
118
119        // Try to open dbm file for writing
120        $this->dba = @dba_open($dbm_filename, 'w', $cache_type);
121        if (!$this->dba) {
122
123            // Failed - create new dbm file
124            $this->dba = dba_open($dbm_filename, 'n', $cache_type);
125
126            if (!$this->dba) {
127                die('failed to create dbm file: ' . $dbm_filename);
128            }
129
130            // Insert getID3 version number
131            dba_insert(getid3::VERSION, getid3::VERSION, $this->dba);
132        }
133
134        // Init misc values
135        $this->cache_type   = $cache_type;
136        $this->dbm_filename = $dbm_filename;
137
138        // Register destructor
139        register_shutdown_function(array($this, '__destruct'));
140
141        // Check version number and clear cache if changed
142        if (dba_fetch(getid3::VERSION, $this->dba) != getid3::VERSION) {
143            $this->clear_cache();
144        }
145
146        parent::__construct();
147    }
148
149
150
151    public function __destruct() {
152
153        // Close dbm file
154        @dba_close($this->dba);
155
156        // Release exclusive lock
157        @flock($this->lock, LOCK_UN);
158
159        // Close lock file
160        @fclose($this->lock);
161    }
162
163
164
165    public function clear_cache() {
166
167        // Close dbm file
168        dba_close($this->dba);
169
170        // Create new dbm file
171        $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type);
172
173        if (!$this->dba) {
174            die('failed to clear cache/recreate dbm file: ' . $this->dbm_filename);
175        }
176
177        // Insert getID3 version number
178        dba_insert(getid3::VERSION, getid3::VERSION, $this->dba);
179
180        // Reregister shutdown function
181        register_shutdown_function(array($this, '__destruct'));
182    }
183
184
185
186    // public: analyze file
187    public function Analyze($filename) {
188
189        if (file_exists($filename)) {
190
191            // Calc key     filename::mod_time::size    - should be unique
192            $key = $filename . '::' . filemtime($filename) . '::' . filesize($filename);
193
194            // Loopup key
195            $result = dba_fetch($key, $this->dba);
196
197            // Hit
198            if ($result !== false) {
199                return unserialize($result);
200            }
201        }
202
203        // Miss
204        $result = parent::Analyze($filename);
205
206        // Save result
207        if (file_exists($filename)) {
208            dba_insert($key, serialize($result), $this->dba);
209        }
210
211        return $result;
212    }
213
214}
215
216
217?>
Note: See TracBrowser for help on using the repository browser.