| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TXCache class file
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TXCache.php 1994 2007-06-11 16:02:28Z knut $
|
|
|
10 |
* @package System.Caching
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TXCache class
|
|
|
15 |
*
|
|
|
16 |
* TXCache implements a cache application module based on {@link http://xcache.lighttpd.net/ xcache}.
|
|
|
17 |
*
|
|
|
18 |
* By definition, cache does not ensure the existence of a value
|
|
|
19 |
* even if it never expires. Cache is not meant to be an persistent storage.
|
|
|
20 |
*
|
|
|
21 |
* To use this module, the xcache PHP extension must be loaded and configured in the php.ini.
|
|
|
22 |
*
|
|
|
23 |
* Some usage examples of TXCache are as follows,
|
|
|
24 |
* <code>
|
|
|
25 |
* $cache=new TXCache; // TXCache may also be loaded as a Prado application module
|
|
|
26 |
* $cache->init(null);
|
|
|
27 |
* $cache->add('object',$object);
|
|
|
28 |
* $object2=$cache->get('object');
|
|
|
29 |
* </code>
|
|
|
30 |
*
|
|
|
31 |
* If loaded, TXCache will register itself with {@link TApplication} as the
|
|
|
32 |
* cache module. It can be accessed via {@link TApplication::getCache()}.
|
|
|
33 |
*
|
|
|
34 |
* TXCache may be configured in application configuration file as follows
|
|
|
35 |
* <code>
|
|
|
36 |
* <module id="cache" class="System.Caching.TXCache" />
|
|
|
37 |
* </code>
|
|
|
38 |
*
|
|
|
39 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
40 |
* @version $Id: TXCache.php 1994 2007-06-11 16:02:28Z knut $
|
|
|
41 |
* @package System.Caching
|
|
|
42 |
* @since 3.1.1
|
|
|
43 |
*/
|
|
|
44 |
class TXCache extends TCache
|
|
|
45 |
{
|
|
|
46 |
/**
|
|
|
47 |
* Initializes this module.
|
|
|
48 |
* This method is required by the IModule interface.
|
|
|
49 |
* @param TXmlElement configuration for this module, can be null
|
|
|
50 |
* @throws TConfigurationException if xcache extension is not installed or not started, check your php.ini
|
|
|
51 |
*/
|
|
|
52 |
public function init($config)
|
|
|
53 |
{
|
|
|
54 |
if(!function_exists('xcache_isset'))
|
|
|
55 |
throw new TConfigurationException('xcache_extension_required');
|
|
|
56 |
|
|
|
57 |
$enabled = (int)ini_get('xcache.cacher') !== 0;
|
|
|
58 |
$var_size = (int)ini_get('xcache.var_size');
|
|
|
59 |
|
|
|
60 |
if(!($enabled && $var_size > 0))
|
|
|
61 |
throw new TConfigurationException('xcache_extension_not_enabled');
|
|
|
62 |
|
|
|
63 |
parent::init($config);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Retrieves a value from cache with a specified key.
|
|
|
68 |
* This is the implementation of the method declared in the parent class.
|
|
|
69 |
* @param string a unique key identifying the cached value
|
|
|
70 |
* @return string the value stored in cache, false if the value is not in the cache or expired.
|
|
|
71 |
*/
|
|
|
72 |
protected function getValue($key)
|
|
|
73 |
{
|
|
|
74 |
return xcache_isset($key) ? xcache_get($key) : false;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Stores a value identified by a key in cache.
|
|
|
79 |
* This is the implementation of the method declared in the parent class.
|
|
|
80 |
*
|
|
|
81 |
* @param string the key identifying the value to be cached
|
|
|
82 |
* @param string the value to be cached
|
|
|
83 |
* @param integer the number of seconds in which the cached value will expire. 0 means never expire.
|
|
|
84 |
* @return boolean true if the value is successfully stored into cache, false otherwise
|
|
|
85 |
*/
|
|
|
86 |
protected function setValue($key,$value,$expire)
|
|
|
87 |
{
|
|
|
88 |
return xcache_set($key,$value,$expire);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Stores a value identified by a key into cache if the cache does not contain this key.
|
|
|
93 |
* This is the implementation of the method declared in the parent class.
|
|
|
94 |
*
|
|
|
95 |
* @param string the key identifying the value to be cached
|
|
|
96 |
* @param string the value to be cached
|
|
|
97 |
* @param integer the number of seconds in which the cached value will expire. 0 means never expire.
|
|
|
98 |
* @return boolean true if the value is successfully stored into cache, false otherwise
|
|
|
99 |
*/
|
|
|
100 |
protected function addValue($key,$value,$expire)
|
|
|
101 |
{
|
|
|
102 |
return !xcache_isset($key) ? $this->setValue($key,$value,$expire) : false;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Deletes a value with the specified key from cache
|
|
|
107 |
* This is the implementation of the method declared in the parent class.
|
|
|
108 |
* @param string the key of the value to be deleted
|
|
|
109 |
* @return boolean if no error happens during deletion
|
|
|
110 |
*/
|
|
|
111 |
protected function deleteValue($key)
|
|
|
112 |
{
|
|
|
113 |
return xcache_unset($key);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Deletes all values from cache.
|
|
|
118 |
* Be careful of performing this operation if the cache is shared by multiple applications.
|
|
|
119 |
*/
|
|
|
120 |
public function flush()
|
|
|
121 |
{
|
|
|
122 |
return xcache_clear_cache();
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
?>
|