1: <?php
2: /**
3: * CApcCache class file
4: *
5: * @author Qiang Xue <qiang.xue@gmail.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: * CApcCache provides APC caching in terms of an application component.
13: *
14: * The caching is based on {@link http://www.php.net/apc APC}.
15: * To use this application component, the APC PHP extension must be loaded.
16: *
17: * See {@link CCache} manual for common cache operations that are supported by CApcCache.
18: *
19: * @author Qiang Xue <qiang.xue@gmail.com>
20: * @package system.caching
21: * @since 1.0
22: */
23: class CApcCache extends CCache
24: {
25: /**
26: * Initializes this application component.
27: * This method is required by the {@link IApplicationComponent} interface.
28: * It checks the availability of APC.
29: * @throws CException if APC cache extension is not loaded or is disabled.
30: */
31: public function init()
32: {
33: parent::init();
34: if(!extension_loaded('apc'))
35: throw new CException(Yii::t('yii','CApcCache requires PHP apc extension to be loaded.'));
36: }
37:
38: /**
39: * Retrieves a value from cache with a specified key.
40: * This is the implementation of the method declared in the parent class.
41: * @param string $key a unique key identifying the cached value
42: * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
43: */
44: protected function getValue($key)
45: {
46: return apc_fetch($key);
47: }
48:
49: /**
50: * Retrieves multiple values from cache with the specified keys.
51: * @param array $keys a list of keys identifying the cached values
52: * @return array a list of cached values indexed by the keys
53: */
54: protected function getValues($keys)
55: {
56: return apc_fetch($keys);
57: }
58:
59: /**
60: * Stores a value identified by a key in cache.
61: * This is the implementation of the method declared in the parent class.
62: *
63: * @param string $key the key identifying the value to be cached
64: * @param string $value the value to be cached
65: * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
66: * @return boolean true if the value is successfully stored into cache, false otherwise
67: */
68: protected function setValue($key,$value,$expire)
69: {
70: return apc_store($key,$value,$expire);
71: }
72:
73: /**
74: * Stores a value identified by a key into cache if the cache does not contain this key.
75: * This is the implementation of the method declared in the parent class.
76: *
77: * @param string $key the key identifying the value to be cached
78: * @param string $value the value to be cached
79: * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
80: * @return boolean true if the value is successfully stored into cache, false otherwise
81: */
82: protected function addValue($key,$value,$expire)
83: {
84: return apc_add($key,$value,$expire);
85: }
86:
87: /**
88: * Deletes a value with the specified key from cache
89: * This is the implementation of the method declared in the parent class.
90: * @param string $key the key of the value to be deleted
91: * @return boolean if no error happens during deletion
92: */
93: protected function deleteValue($key)
94: {
95: return apc_delete($key);
96: }
97:
98: /**
99: * Deletes all values from cache.
100: * This is the implementation of the method declared in the parent class.
101: * @return boolean whether the flush operation was successful.
102: * @since 1.1.5
103: */
104: protected function flushValues()
105: {
106: if(extension_loaded('apcu'))
107: return apc_clear_cache();
108:
109: return apc_clear_cache('user');
110: }
111: }
112: