3 * TSqliteCache class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Caching
15 * TSqliteCache implements a cache application module based on SQLite database.
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.
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.
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.
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
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.
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.
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.
49 * Some usage examples of TSqliteCache are as follows,
51 * $cache=new TSqliteCache; // TSqliteCache may also be loaded as a Prado application module
52 * $cache->setDbFile($dbFilePath);
54 * $cache->add('object',$object);
55 * $object2=$cache->get('object');
58 * If loaded, TSqliteCache will register itself with {@link TApplication} as the
59 * cache module. It can be accessed via {@link TApplication::getCache()}.
61 * TSqliteCache may be configured in application configuration file as follows
63 * <module id="cache" class="System.Caching.TSqliteCache" DbFile="Application.Data.site" />
65 * where {@link getDbFile DbFile} is a property specifying the location of the
66 * SQLite DB file (in the namespace format).
68 * @author Qiang Xue <qiang.xue@gmail.com>
69 * @package System.Caching
72 class TSqliteCache extends TCache
75 * name of the table storing cache data
77 const CACHE_TABLE='cache';
79 * extension of the db file name
81 const DB_FILE_EXT='.db';
84 * @var boolean if the module has been initialized
86 private $_initialized=false;
88 * @var SQLiteDatabase the sqlite database instance
92 * @var string the database file name
98 * Disconnect the db connection.
100 public function __destruct()
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.
115 public function init($config)
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';
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)
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()));
129 $this->_initialized=true;
130 parent::init($config);
134 * @return string database file path (in namespace form)
136 public function getDbFile()
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
146 public function setDbFile($value)
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);
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.
160 protected function getValue($key)
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']);
170 * Stores a value identified by a key in cache.
171 * This is the implementation of the method declared in the parent class.
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
178 protected function setValue($key,$value,$expire)
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;
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.
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
194 protected function addValue($key,$value,$expire)
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;
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
207 protected function deleteValue($key)
209 $sql='DELETE FROM '.self::CACHE_TABLE.' WHERE key=\''.$key.'\'';
210 return $this->_db->query($sql)!==false;
214 * Deletes all values from cache.
215 * Be careful of performing this operation if the cache is shared by multiple applications.
217 public function flush()
219 return $this->_db->query('DELETE FROM '.self::CACHE_TABLE)!==false;