pradosoft /
prado
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * TAPCCache class file |
||
| 5 | * |
||
| 6 | * @author Alban Hanry <[email protected]> |
||
| 7 | * @link https://github.com/pradosoft/prado |
||
| 8 | * @license https://github.com/pradosoft/prado/blob/master/LICENSE |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Prado\Caching; |
||
| 12 | |||
| 13 | use Prado\Exceptions\TConfigurationException; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * TAPCCache class |
||
| 17 | * |
||
| 18 | * TAPCCache implements a cache application module based on {@see http://www.php.net/apcu APCu}. |
||
| 19 | * |
||
| 20 | * By definition, cache does not ensure the existence of a value |
||
| 21 | * even if it never expires. Cache is not meant to be an persistent storage. |
||
| 22 | * |
||
| 23 | * To use this module, the APCu PHP extension must be loaded and set in the php.ini file. |
||
| 24 | * |
||
| 25 | * Some usage examples of TAPCCache are as follows, |
||
| 26 | * ```php |
||
| 27 | * $cache=new TAPCCache; // TAPCCache may also be loaded as a Prado application module |
||
| 28 | * $cache->init(null); |
||
| 29 | * $cache->add('object',$object); |
||
| 30 | * $object2=$cache->get('object'); |
||
| 31 | * ``` |
||
| 32 | * |
||
| 33 | * If loaded, TAPCCache will register itself with {@see \Prado\TApplication} as the |
||
| 34 | * cache module. It can be accessed via {@see \Prado\TApplication::getCache()}. |
||
| 35 | * |
||
| 36 | * TAPCCache may be configured in application configuration file as follows |
||
| 37 | * ```php |
||
| 38 | * <module id="cache" class="Prado\Caching\TAPCCache" /> |
||
| 39 | * ``` |
||
| 40 | * |
||
| 41 | * @author Alban Hanry <[email protected]> |
||
| 42 | * @author Knut Urdalen <[email protected]> |
||
| 43 | * @since 3.0b |
||
| 44 | */ |
||
| 45 | class TAPCCache extends TCache |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * Initializes this module. |
||
| 49 | * This method is required by the IModule interface. |
||
| 50 | * @param \Prado\Xml\TXmlElement $config configuration for this module, can be null |
||
| 51 | * @throws TConfigurationException if apc extension is not installed or not started, check your php.ini |
||
| 52 | */ |
||
| 53 | public function init($config) |
||
| 54 | { |
||
| 55 | if (!extension_loaded('apcu')) { |
||
| 56 | throw new TConfigurationException('apccache_extension_required'); |
||
| 57 | } |
||
| 58 | |||
| 59 | if (ini_get('apc.enabled') == false) { |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 60 | throw new TConfigurationException('apccache_extension_not_enabled'); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (substr(php_sapi_name(), 0, 3) === 'cli' && ini_get('apc.enable_cli') == false) { |
||
|
0 ignored issues
–
show
|
|||
| 64 | throw new TConfigurationException('apccache_extension_not_enabled_cli'); |
||
| 65 | } |
||
| 66 | |||
| 67 | parent::init($config); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Retrieves a value from cache with a specified key. |
||
| 72 | * This is the implementation of the method declared in the parent class. |
||
| 73 | * @param string $key a unique key identifying the cached value |
||
| 74 | * @return false|string the value stored in cache, false if the value is not in the cache or expired. |
||
| 75 | */ |
||
| 76 | protected function getValue($key) |
||
| 77 | { |
||
| 78 | return apcu_fetch($key); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Stores a value identified by a key in cache. |
||
| 83 | * This is the implementation of the method declared in the parent class. |
||
| 84 | * |
||
| 85 | * @param string $key the key identifying the value to be cached |
||
| 86 | * @param string $value the value to be cached |
||
| 87 | * @param int $expire the number of seconds in which the cached value will expire. 0 means never expire. |
||
| 88 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
| 89 | */ |
||
| 90 | protected function setValue($key, $value, $expire) |
||
| 91 | { |
||
| 92 | return apcu_store($key, $value, $expire); |
||
|
0 ignored issues
–
show
|
|||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Stores a value identified by a key into cache if the cache does not contain this key. |
||
| 97 | * This is the implementation of the method declared in the parent class. |
||
| 98 | * |
||
| 99 | * @param string $key the key identifying the value to be cached |
||
| 100 | * @param string $value the value to be cached |
||
| 101 | * @param int $expire the number of seconds in which the cached value will expire. 0 means never expire. |
||
| 102 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
| 103 | */ |
||
| 104 | protected function addValue($key, $value, $expire) |
||
| 105 | { |
||
| 106 | return apcu_add($key, $value, $expire); |
||
|
0 ignored issues
–
show
|
|||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Deletes a value with the specified key from cache |
||
| 111 | * This is the implementation of the method declared in the parent class. |
||
| 112 | * @param string $key the key of the value to be deleted |
||
| 113 | * @return bool if no error happens during deletion |
||
| 114 | */ |
||
| 115 | protected function deleteValue($key) |
||
| 116 | { |
||
| 117 | return apcu_delete($key); |
||
|
0 ignored issues
–
show
|
|||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Deletes all values from cache. |
||
| 122 | * Be careful of performing this operation if the cache is shared by multiple applications. |
||
| 123 | * @return bool if no error happens during flush |
||
| 124 | */ |
||
| 125 | public function flush() |
||
| 126 | { |
||
| 127 | return apcu_clear_cache(); |
||
| 128 | } |
||
| 129 | } |
||
| 130 |