1: <?php
2: /**
3: * CXCache class file
4: *
5: * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CXCache implements a cache application module based on {@link http://xcache.lighttpd.net/ xcache}.
13: *
14: * To use this application component, the XCache PHP extension must be loaded.
15: * Flush functionality will only work correctly if "xcache.admin.enable_auth" is set to "Off" in php.ini.
16: *
17: * See {@link CCache} manual for common cache operations that are supported by CXCache.
18: *
19: * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
20: * @package system.caching
21: */
22: class CXCache extends CCache
23: {
24: /**
25: * Initializes this application component.
26: * This method is required by the {@link IApplicationComponent} interface.
27: * It checks the availability of memcache.
28: * @throws CException if memcache extension is not loaded or is disabled.
29: */
30: public function init()
31: {
32: parent::init();
33: if(!function_exists('xcache_isset'))
34: throw new CException(Yii::t('yii','CXCache requires PHP XCache extension to be loaded.'));
35: }
36:
37: /**
38: * Retrieves a value from cache with a specified key.
39: * This is the implementation of the method declared in the parent class.
40: * @param string $key a unique key identifying the cached value
41: * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
42: */
43: protected function getValue($key)
44: {
45: return xcache_isset($key) ? xcache_get($key) : false;
46: }
47:
48: /**
49: * Stores a value identified by a key in cache.
50: * This is the implementation of the method declared in the parent class.
51: *
52: * @param string $key the key identifying the value to be cached
53: * @param string $value the value to be cached
54: * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
55: * @return boolean true if the value is successfully stored into cache, false otherwise
56: */
57: protected function setValue($key,$value,$expire)
58: {
59: return xcache_set($key,$value,$expire);
60: }
61:
62: /**
63: * Stores a value identified by a key into cache if the cache does not contain this key.
64: * This is the implementation of the method declared in the parent class.
65: *
66: * @param string $key the key identifying the value to be cached
67: * @param string $value the value to be cached
68: * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
69: * @return boolean true if the value is successfully stored into cache, false otherwise
70: */
71: protected function addValue($key,$value,$expire)
72: {
73: return !xcache_isset($key) ? $this->setValue($key,$value,$expire) : false;
74: }
75:
76: /**
77: * Deletes a value with the specified key from cache
78: * This is the implementation of the method declared in the parent class.
79: * @param string $key the key of the value to be deleted
80: * @return boolean if no error happens during deletion
81: */
82: protected function deleteValue($key)
83: {
84: return xcache_unset($key);
85: }
86:
87: /**
88: * Deletes all values from cache.
89: * This is the implementation of the method declared in the parent class.
90: * @return boolean whether the flush operation was successful.
91: * @since 1.1.5
92: */
93: protected function flushValues()
94: {
95: for($i=0, $max=xcache_count(XC_TYPE_VAR); $i<$max; $i++)
96: {
97: if(xcache_clear_cache(XC_TYPE_VAR, $i)===false)
98: return false;
99: }
100: return true;
101: }
102: }
103:
104: