]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Caching/TSqliteCache.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Caching / TSqliteCache.php
1 <?php
2 /**
3  * TSqliteCache class file
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Caching
10  */
11
12 /**
13  * TSqliteCache class
14  *
15  * TSqliteCache implements a cache application module based on SQLite database.
16  *
17  * THIS CLASS IS DEPRECATED since it relies on the sqlite PHP extension, that is
18  * no longer loaded by default since PHP 5.1. You are discouraged from using it:
19  * use {@link TDbCache} instead.
20  *
21  * Since PRADO v3.1.0, a new DB-based cache module called {@link TDbCache}
22  * is provided. If you have PDO extension installed, you may consider using
23  * the new cache module instead as it allows you to use different database
24  * to store the cached data.
25  *
26  * The database file is specified by the {@link setDbFile DbFile} property.
27  * If not set, the database file will be created under the system state path.
28  * If the specified database file does not exist, it will be created automatically.
29  * Make sure the directory containing the specified DB file and the file itself is
30  * writable by the Web server process.
31  *
32  * The following basic cache operations are implemented:
33  * - {@link get} : retrieve the value with a key (if any) from cache
34  * - {@link set} : store the value with a key into cache
35  * - {@link add} : store the value only if cache does not have this key
36  * - {@link delete} : delete the value with the specified key from cache
37  * - {@link flush} : delete all values from cache
38  *
39  * Each value is associated with an expiration time. The {@link get} operation
40  * ensures that any expired value will not be returned. The expiration time by
41  * the number of seconds. A expiration time 0 represents never expire.
42  *
43  * By definition, cache does not ensure the existence of a value
44  * even if it never expires. Cache is not meant to be an persistent storage.
45  *
46  * Do not use the same database file for multiple applications using TSqliteCache.
47  * Also note, cache is shared by all user sessions of an application.
48  *
49  * Some usage examples of TSqliteCache are as follows,
50  * <code>
51  * $cache=new TSqliteCache;  // TSqliteCache may also be loaded as a Prado application module
52  * $cache->setDbFile($dbFilePath);
53  * $cache->init(null);
54  * $cache->add('object',$object);
55  * $object2=$cache->get('object');
56  * </code>
57  *
58  * If loaded, TSqliteCache will register itself with {@link TApplication} as the
59  * cache module. It can be accessed via {@link TApplication::getCache()}.
60  *
61  * TSqliteCache may be configured in application configuration file as follows
62  * <code>
63  * <module id="cache" class="System.Caching.TSqliteCache" DbFile="Application.Data.site" />
64  * </code>
65  * where {@link getDbFile DbFile} is a property specifying the location of the
66  * SQLite DB file (in the namespace format).
67  *
68  * @author Qiang Xue <qiang.xue@gmail.com>
69  * @package System.Caching
70  * @since 3.0
71  */
72 class TSqliteCache extends TCache
73 {
74         /**
75          * name of the table storing cache data
76          */
77         const CACHE_TABLE='cache';
78         /**
79          * extension of the db file name
80          */
81         const DB_FILE_EXT='.db';
82
83         /**
84          * @var boolean if the module has been initialized
85          */
86         private $_initialized=false;
87         /**
88          * @var SQLiteDatabase the sqlite database instance
89          */
90         private $_db=null;
91         /**
92          * @var string the database file name
93          */
94         private $_file=null;
95
96         /**
97          * Destructor.
98          * Disconnect the db connection.
99          */
100         public function __destruct()
101         {
102                 $this->_db=null;
103         }
104
105         /**
106          * Initializes this module.
107          * This method is required by the IModule interface. It checks if the DbFile
108          * property is set, and creates a SQLiteDatabase instance for it.
109          * The database or the cache table does not exist, they will be created.
110          * Expired values are also deleted.
111          * @param TXmlElement configuration for this module, can be null
112          * @throws TConfigurationException if sqlite extension is not installed,
113          *         DbFile is set invalid, or any error happens during creating database or cache table.
114          */
115         public function init($config)
116         {
117                 if(!function_exists('sqlite_open'))
118                         throw new TConfigurationException('sqlitecache_extension_required');
119                 if($this->_file===null)
120                         $this->_file=$this->getApplication()->getRuntimePath().'/sqlite.cache';
121                 $error='';
122                 if(($this->_db=new SQLiteDatabase($this->_file,0666,$error))===false)
123                         throw new TConfigurationException('sqlitecache_connection_failed',$error);
124                 if(@$this->_db->query('DELETE FROM '.self::CACHE_TABLE.' WHERE expire<>0 AND expire<'.time())===false)
125                 {
126                         if($this->_db->query('CREATE TABLE '.self::CACHE_TABLE.' (key CHAR(128) PRIMARY KEY, value BLOB, expire INT)')===false)
127                                 throw new TConfigurationException('sqlitecache_table_creation_failed',sqlite_error_string(sqlite_last_error()));
128                 }
129                 $this->_initialized=true;
130                 parent::init($config);
131         }
132
133         /**
134          * @return string database file path (in namespace form)
135          */
136         public function getDbFile()
137         {
138                 return $this->_file;
139         }
140
141         /**
142          * @param string database file path (in namespace form)
143          * @throws TInvalidOperationException if the module is already initialized
144          * @throws TConfigurationException if the file is not in proper namespace format
145          */
146         public function setDbFile($value)
147         {
148                 if($this->_initialized)
149                         throw new TInvalidOperationException('sqlitecache_dbfile_unchangeable');
150                 else if(($this->_file=Prado::getPathOfNamespace($value,self::DB_FILE_EXT))===null)
151                         throw new TConfigurationException('sqlitecache_dbfile_invalid',$value);
152         }
153
154         /**
155          * Retrieves a value from cache with a specified key.
156          * This is the implementation of the method declared in the parent class.
157          * @param string a unique key identifying the cached value
158          * @return string the value stored in cache, false if the value is not in the cache or expired.
159          */
160         protected function getValue($key)
161         {
162                 $sql='SELECT value FROM '.self::CACHE_TABLE.' WHERE key=\''.$key.'\' AND (expire=0 OR expire>'.time().') LIMIT 1';
163                 if(($ret=$this->_db->query($sql))!=false && ($row=$ret->fetch(SQLITE_ASSOC))!==false)
164                         return unserialize($row['value']);
165                 else
166                         return false;
167         }
168
169         /**
170          * Stores a value identified by a key in cache.
171          * This is the implementation of the method declared in the parent class.
172          *
173          * @param string the key identifying the value to be cached
174          * @param string the value to be cached
175          * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
176          * @return boolean true if the value is successfully stored into cache, false otherwise
177          */
178         protected function setValue($key,$value,$expire)
179         {
180                 $expire=($expire<=0)?0:time()+$expire;
181                 $sql='REPLACE INTO '.self::CACHE_TABLE.' VALUES(\''.$key.'\',\''.sqlite_escape_string(serialize($value)).'\','.$expire.')';
182                 return $this->_db->query($sql)!==false;
183         }
184
185         /**
186          * Stores a value identified by a key into cache if the cache does not contain this key.
187          * This is the implementation of the method declared in the parent class.
188          *
189          * @param string the key identifying the value to be cached
190          * @param string the value to be cached
191          * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
192          * @return boolean true if the value is successfully stored into cache, false otherwise
193          */
194         protected function addValue($key,$value,$expire)
195         {
196                 $expire=($expire<=0)?0:time()+$expire;
197                 $sql='INSERT INTO '.self::CACHE_TABLE.' VALUES(\''.$key.'\',\''.sqlite_escape_string(serialize($value)).'\','.$expire.')';
198                 return @$this->_db->query($sql)!==false;
199         }
200
201         /**
202          * Deletes a value with the specified key from cache
203          * This is the implementation of the method declared in the parent class.
204          * @param string the key of the value to be deleted
205          * @return boolean if no error happens during deletion
206          */
207         protected function deleteValue($key)
208         {
209                 $sql='DELETE FROM '.self::CACHE_TABLE.' WHERE key=\''.$key.'\'';
210                 return $this->_db->query($sql)!==false;
211         }
212
213         /**
214          * Deletes all values from cache.
215          * Be careful of performing this operation if the cache is shared by multiple applications.
216          */
217         public function flush()
218         {
219                 return $this->_db->query('DELETE FROM '.self::CACHE_TABLE)!==false;
220         }
221 }
222