Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TDbCache class file
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TDbCache.php 2496 2008-08-14 10:16:47Z knut $
10
 * @package System.Caching
11
 */
12
 
13
Prado::using('System.Data.TDbConnection');
14
 
15
/**
16
 * TDbCache class
17
 *
18
 * TDbCache implements a cache application module by storing cached data in a database.
19
 *
20
 * TDbCache relies on {@link http://www.php.net/manual/en/ref.pdo.php PDO} to retrieve
21
 * data from databases. In order to use TDbCache, you need to enable the PDO extension
22
 * as well as the corresponding PDO DB driver. For example, to use SQLite database
23
 * to store cached data, you need both php_pdo and php_pdo_sqlite extensions.
24
 *
25
 * By default, TDbCache creates and uses an SQLite database under the application
26
 * runtime directory. You may change this default setting by specifying the following
27
 * properties:
28
 * - {@link setConnectionID ConnectionID} or
29
 * - {@link setConnectionString ConnectionString}, {@link setUsername Username} and {@link setPassword Pasword}.
30
 *
31
 * The cached data is stored in a table in the specified database.
32
 * By default, the name of the table is called 'pradocache'. If the table does not
33
 * exist in the database, it will be automatically created with the following structure:
34
 * (itemkey CHAR(128) PRIMARY KEY, value BLOB, expire INT)
35
 *
36
 * Note, some DBMS might not support BLOB type. In this case, replace 'BLOB' with a suitable
37
 * binary data type (e.g. LONGBLOB in MySQL, BYTEA in PostgreSQL.)
38
 *
39
 * If you want to change the cache table name, or if you want to create the table by yourself,
40
 * you may set {@link setCacheTableName CacheTableName} and {@link setAutoCreateCacheTable AutoCreateCacheTableName} properties.
41
 *
42
 * The following basic cache operations are implemented:
43
 * - {@link get} : retrieve the value with a key (if any) from cache
44
 * - {@link set} : store the value with a key into cache
45
 * - {@link add} : store the value only if cache does not have this key
46
 * - {@link delete} : delete the value with the specified key from cache
47
 * - {@link flush} : delete all values from cache
48
 *
49
 * Each value is associated with an expiration time. The {@link get} operation
50
 * ensures that any expired value will not be returned. The expiration time by
51
 * the number of seconds. A expiration time 0 represents never expire.
52
 *
53
 * By definition, cache does not ensure the existence of a value
54
 * even if it never expires. Cache is not meant to be an persistent storage.
55
 *
56
 * Do not use the same database file for multiple applications using TDbCache.
57
 * Also note, cache is shared by all user sessions of an application.
58
 *
59
 * Some usage examples of TDbCache are as follows,
60
 * <code>
61
 * $cache=new TDbCache;  // TDbCache may also be loaded as a Prado application module
62
 * $cache->init(null);
63
 * $cache->add('object',$object);
64
 * $object2=$cache->get('object');
65
 * </code>
66
 *
67
 * If loaded, TDbCache will register itself with {@link TApplication} as the
68
 * cache module. It can be accessed via {@link TApplication::getCache()}.
69
 *
70
 * TDbCache may be configured in application configuration file as follows
71
 * <code>
72
 * <module id="cache" class="System.Caching.TDbCache" />
73
 * </code>
74
 *
75
 * @author Qiang Xue <qiang.xue@gmail.com>
76
 * @version $Id: TDbCache.php 2496 2008-08-14 10:16:47Z knut $
77
 * @package System.Caching
78
 * @since 3.1.0
79
 */
80
class TDbCache extends TCache
81
{
82
	/**
83
	 * @var string the ID of TDataSourceConfig module
84
	 */
85
	private $_connID='';
86
	/**
87
	 * @var TDbConnection the DB connection instance
88
	 */
89
	private $_db;
90
	/**
91
	 * @var string name of the DB cache table
92
	 */
93
	private $_cacheTable='pradocache';
94
	/**
95
	 * @var boolean whether the cache DB table should be created automatically
96
	 */
97
	private $_autoCreate=true;
98
	private $_username='';
99
	private $_password='';
100
	private $_connectionString='';
101
 
102
	/**
103
	 * Destructor.
104
	 * Disconnect the db connection.
105
	 */
106
	public function __destruct()
107
	{
108
		if($this->_db!==null)
109
			$this->_db->setActive(false);
110
	}
111
 
112
	/**
113
	 * Initializes this module.
114
	 * This method is required by the IModule interface. It checks if the DbFile
115
	 * property is set, and creates a SQLiteDatabase instance for it.
116
	 * The database or the cache table does not exist, they will be created.
117
	 * Expired values are also deleted.
118
	 * @param TXmlElement configuration for this module, can be null
119
	 * @throws TConfigurationException if sqlite extension is not installed,
120
	 *         DbFile is set invalid, or any error happens during creating database or cache table.
121
	 */
122
	public function init($config)
123
	{
124
		$db=$this->getDbConnection();
125
		$db->setActive(true);
126
 
127
		$sql='DELETE FROM '.$this->_cacheTable.' WHERE expire<>0 AND expire<'.time();
128
		try
129
		{
130
			$db->createCommand($sql)->execute();
131
		}
132
		catch(Exception $e)
133
		{
134
			// DB table not exists
135
			if($this->_autoCreate)
136
			{
137
				$driver=$db->getDriverName();
138
				if($driver==='mysql')
139
					$blob='LONGBLOB';
140
				else if($driver==='pgsql')
141
					$blob='BYTEA';
142
				else
143
					$blob='BLOB';
144
 
145
				$sql='CREATE TABLE '.$this->_cacheTable." (itemkey CHAR(128) PRIMARY KEY, value $blob, expire INT)";
146
				$db->createCommand($sql)->execute();
147
			}
148
			else
149
				throw new TConfigurationException('db_cachetable_inexistent',$this->_cacheTable);
150
		}
151
 
152
		parent::init($config);
153
	}
154
 
155
	/**
156
	 * Creates the DB connection.
157
	 * @param string the module ID for TDataSourceConfig
158
	 * @return TDbConnection the created DB connection
159
	 * @throws TConfigurationException if module ID is invalid or empty
160
	 */
161
	protected function createDbConnection()
162
	{
163
		if($this->_connID!=='')
164
		{
165
			$config=$this->getApplication()->getModule($this->_connID);
166
			if($config instanceof TDataSourceConfig)
167
				return $config->getDbConnection();
168
			else
169
				throw new TConfigurationException('dbcache_connectionid_invalid',$this->_connID);
170
		}
171
		else
172
		{
173
			$db=new TDbConnection;
174
			if($this->_connectionString!=='')
175
			{
176
				$db->setConnectionString($this->_connectionString);
177
				if($this->_username!=='')
178
					$db->setUsername($this->_username);
179
				if($this->_password!=='')
180
					$db->setPassword($this->_password);
181
			}
182
			else
183
			{
184
				// default to SQLite3 database
185
				$dbFile=$this->getApplication()->getRuntimePath().'/sqlite3.cache';
186
				$db->setConnectionString('sqlite:'.$dbFile);
187
			}
188
			return $db;
189
		}
190
	}
191
 
192
	/**
193
	 * @return TDbConnection the DB connection instance
194
	 */
195
	public function getDbConnection()
196
	{
197
		if($this->_db===null)
198
			$this->_db=$this->createDbConnection();
199
		return $this->_db;
200
	}
201
 
202
	/**
203
	 * @return string the ID of a {@link TDataSourceConfig} module. Defaults to empty string, meaning not set.
204
	 * @since 3.1.1
205
	 */
206
	public function getConnectionID()
207
	{
208
		return $this->_connID;
209
	}
210
 
211
	/**
212
	 * Sets the ID of a TDataSourceConfig module.
213
	 * The datasource module will be used to establish the DB connection for this cache module.
214
	 * The database connection can also be specified via {@link setConnectionString ConnectionString}.
215
	 * When both ConnectionID and ConnectionString are specified, the former takes precedence.
216
	 * @param string ID of the {@link TDataSourceConfig} module
217
	 * @since 3.1.1
218
	 */
219
	public function setConnectionID($value)
220
	{
221
		$this->_connID=$value;
222
	}
223
 
224
	/**
225
	 * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
226
	 */
227
	public function getConnectionString()
228
	{
229
		return $this->_connectionString;
230
	}
231
 
232
	/**
233
	 * @param string The Data Source Name, or DSN, contains the information required to connect to the database.
234
	 * @see http://www.php.net/manual/en/function.pdo-construct.php
235
	 */
236
	public function setConnectionString($value)
237
	{
238
		$this->_connectionString=$value;
239
	}
240
 
241
	/**
242
	 * @return string the username for establishing DB connection. Defaults to empty string.
243
	 */
244
	public function getUsername()
245
	{
246
		return $this->_username;
247
	}
248
 
249
	/**
250
	 * @param string the username for establishing DB connection
251
	 */
252
	public function setUsername($value)
253
	{
254
		$this->_username=$value;
255
	}
256
 
257
	/**
258
	 * @return string the password for establishing DB connection. Defaults to empty string.
259
	 */
260
	public function getPassword()
261
	{
262
		return $this->_password;
263
	}
264
 
265
	/**
266
	 * @param string the password for establishing DB connection
267
	 */
268
	public function setPassword($value)
269
	{
270
		$this->_password=$value;
271
	}
272
 
273
	/**
274
	 * @return string the name of the DB table to store cache content. Defaults to 'pradocache'.
275
	 * @see setAutoCreateCacheTable
276
	 */
277
	public function getCacheTableName()
278
	{
279
		return $this->_cacheTable;
280
	}
281
 
282
	/**
283
	 * Sets the name of the DB table to store cache content.
284
	 * Note, if {@link setAutoCreateCacheTable AutoCreateCacheTable} is false
285
	 * and you want to create the DB table manually by yourself,
286
	 * you need to make sure the DB table is of the following structure:
287
	 * (itemkey CHAR(128) PRIMARY KEY, value BLOB, expire INT)
288
	 * Note, some DBMS might not support BLOB type. In this case, replace 'BLOB' with a suitable
289
	 * binary data type (e.g. LONGBLOB in MySQL, BYTEA in PostgreSQL.)
290
	 * @param string the name of the DB table to store cache content
291
	 * @see setAutoCreateCacheTable
292
	 */
293
	public function setCacheTableName($value)
294
	{
295
		$this->_cacheTable=$value;
296
	}
297
 
298
	/**
299
	 * @return boolean whether the cache DB table should be automatically created if not exists. Defaults to true.
300
	 * @see setAutoCreateCacheTable
301
	 */
302
	public function getAutoCreateCacheTable()
303
	{
304
		return $this->_autoCreate;
305
	}
306
 
307
	/**
308
	 * @param boolean whether the cache DB table should be automatically created if not exists.
309
	 * @see setCacheTableName
310
	 */
311
	public function setAutoCreateCacheTable($value)
312
	{
313
		$this->_autoCreate=TPropertyValue::ensureBoolean($value);
314
	}
315
 
316
	/**
317
	 * Retrieves a value from cache with a specified key.
318
	 * This is the implementation of the method declared in the parent class.
319
	 * @param string a unique key identifying the cached value
320
	 * @return string the value stored in cache, false if the value is not in the cache or expired.
321
	 */
322
	protected function getValue($key)
323
	{
324
		$sql='SELECT value FROM '.$this->_cacheTable.' WHERE itemkey=\''.$key.'\' AND (expire=0 OR expire>'.time().')';
325
		return $this->_db->createCommand($sql)->queryScalar();
326
	}
327
 
328
	/**
329
	 * Stores a value identified by a key in cache.
330
	 * This is the implementation of the method declared in the parent class.
331
	 *
332
	 * @param string the key identifying the value to be cached
333
	 * @param string the value to be cached
334
	 * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
335
	 * @return boolean true if the value is successfully stored into cache, false otherwise
336
	 */
337
	protected function setValue($key,$value,$expire)
338
	{
339
		$this->deleteValue($key);
340
		return $this->addValue($key,$value,$expire);
341
	}
342
 
343
	/**
344
	 * Stores a value identified by a key into cache if the cache does not contain this key.
345
	 * This is the implementation of the method declared in the parent class.
346
	 *
347
	 * @param string the key identifying the value to be cached
348
	 * @param string the value to be cached
349
	 * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
350
	 * @return boolean true if the value is successfully stored into cache, false otherwise
351
	 */
352
	protected function addValue($key,$value,$expire)
353
	{
354
		$expire=($expire<=0)?0:time()+$expire;
355
		$sql="INSERT INTO {$this->_cacheTable} (itemkey,value,expire) VALUES(:key,:value,$expire)";
356
		try
357
		{
358
			$command=$this->_db->createCommand($sql);
359
			$command->bindValue(':key',$key,PDO::PARAM_STR);
360
			$command->bindValue(':value',$value,PDO::PARAM_LOB);
361
			$command->execute();
362
			return true;
363
		}
364
		catch(Exception $e)
365
		{
366
			return false;
367
		}
368
	}
369
 
370
	/**
371
	 * Deletes a value with the specified key from cache
372
	 * This is the implementation of the method declared in the parent class.
373
	 * @param string the key of the value to be deleted
374
	 * @return boolean if no error happens during deletion
375
	 */
376
	protected function deleteValue($key)
377
	{
378
		$command=$this->_db->createCommand("DELETE FROM {$this->_cacheTable} WHERE itemkey=:key");
379
		$command->bindValue(':key',$key,PDO::PARAM_STR);
380
		$command->execute();
381
		return true;
382
	}
383
 
384
	/**
385
	 * Deletes all values from cache.
386
	 * Be careful of performing this operation if the cache is shared by multiple applications.
387
	 */
388
	public function flush()
389
	{
390
		$this->_db->createCommand("DELETE FROM {$this->_cacheTable}")->execute();
391
		return true;
392
	}
393
}
394
 
395
?>