1: <?php
2: /**
3: * CEAcceleratorCache class file
4: *
5: * @author Steffen Dietz <steffo.dietz[at]googlemail[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: * CEAcceleratorCache implements a cache application module based on {@link http://eaccelerator.net/ eaccelerator}.
13: *
14: * To use this application component, the eAccelerator PHP extension must be loaded.
15: *
16: * See {@link CCache} manual for common cache operations that are supported by CEAccelerator.
17: *
18: * Please note that as of v0.9.6, eAccelerator no longer supports data caching.
19: * This means if you still want to use this component, your eAccelerator should be of 0.9.5.x or lower version.
20: *
21: * @author Steffen Dietz <steffo.dietz[at]googlemail[dot]com>
22: * @package system.caching
23: */
24: class CEAcceleratorCache extends CCache
25: {
26: /**
27: * Initializes this application component.
28: * This method is required by the {@link IApplicationComponent} interface.
29: * It checks the availability of eAccelerator.
30: * @throws CException if eAccelerator extension is not loaded, is disabled or the cache functions are not compiled in.
31: */
32: public function init()
33: {
34: parent::init();
35: if(!function_exists('eaccelerator_get'))
36: throw new CException(Yii::t('yii','CEAcceleratorCache requires PHP eAccelerator extension to be loaded, enabled or compiled with the "--with-eaccelerator-shared-memory" option.'));
37: }
38:
39: /**
40: * Retrieves a value from cache with a specified key.
41: * This is the implementation of the method declared in the parent class.
42: * @param string $key a unique key identifying the cached value
43: * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
44: */
45: protected function getValue($key)
46: {
47: $result = eaccelerator_get($key);
48: return $result !== NULL ? $result : false;
49: }
50:
51: /**
52: * Stores a value identified by a key in cache.
53: * This is the implementation of the method declared in the parent class.
54: *
55: * @param string $key the key identifying the value to be cached
56: * @param string $value the value to be cached
57: * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
58: * @return boolean true if the value is successfully stored into cache, false otherwise
59: */
60: protected function setValue($key,$value,$expire)
61: {
62: return eaccelerator_put($key,$value,$expire);
63: }
64:
65: /**
66: * Stores a value identified by a key into cache if the cache does not contain this key.
67: * This is the implementation of the method declared in the parent class.
68: *
69: * @param string $key the key identifying the value to be cached
70: * @param string $value the value to be cached
71: * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
72: * @return boolean true if the value is successfully stored into cache, false otherwise
73: */
74: protected function addValue($key,$value,$expire)
75: {
76: return (NULL === eaccelerator_get($key)) ? $this->setValue($key,$value,$expire) : false;
77: }
78:
79: /**
80: * Deletes a value with the specified key from cache
81: * This is the implementation of the method declared in the parent class.
82: * @param string $key the key of the value to be deleted
83: * @return boolean if no error happens during deletion
84: */
85: protected function deleteValue($key)
86: {
87: return eaccelerator_rm($key);
88: }
89:
90: /**
91: * Deletes all values from cache.
92: * This is the implementation of the method declared in the parent class.
93: * @return boolean whether the flush operation was successful.
94: * @since 1.1.5
95: */
96: protected function flushValues()
97: {
98: // first, remove expired content from cache
99: eaccelerator_gc();
100: // now, remove leftover cache-keys
101: $keys = eaccelerator_list_keys();
102: foreach($keys as $key)
103: $this->deleteValue(substr($key['name'], 1));
104: return true;
105: }
106: }
107: