22/**
33 * APC storage engine for cache.
44 *
5- * CakePHP(tm) : Rapid Development Framework (http ://cakephp.org)
6- * Copyright (c) Cake Software Foundation, Inc. (http ://cakefoundation.org)
5+ * CakePHP(tm) : Rapid Development Framework (https ://cakephp.org)
6+ * Copyright (c) Cake Software Foundation, Inc. (https ://cakefoundation.org)
77 *
88 * Licensed under The MIT License
99 * For full copyright and license information, please see the LICENSE.txt
1010 * Redistributions of files must retain the above copyright notice.
1111 *
12- * @copyright Copyright (c) Cake Software Foundation, Inc. (http ://cakefoundation.org)
13- * @link http ://cakephp.org CakePHP(tm) Project
12+ * @copyright Copyright (c) Cake Software Foundation, Inc. (https ://cakefoundation.org)
13+ * @link https ://cakephp.org CakePHP(tm) Project
1414 * @package Cake.Cache.Engine
1515 * @since CakePHP(tm) v 1.2.0.4933
16- * @license http ://www. opensource.org/licenses/mit-license.php MIT License
16+ * @license https ://opensource.org/licenses/mit-license.php MIT License
1717 */
1818
1919/**
@@ -31,6 +31,13 @@ class ApcEngine extends CacheEngine {
3131 */
3232 protected $ _compiledGroupNames = array ();
3333
34+ /**
35+ * APC or APCu extension
36+ *
37+ * @var string
38+ */
39+ protected $ _apcExtension = 'apc ' ;
40+
3441/**
3542 * Initialize the Cache Engine
3643 *
@@ -47,6 +54,10 @@ public function init($settings = array()) {
4754 }
4855 $ settings += array ('engine ' => 'Apc ' );
4956 parent ::init ($ settings );
57+ if (function_exists ('apcu_dec ' )) {
58+ $ this ->_apcExtension = 'apcu ' ;
59+ return true ;
60+ }
5061 return function_exists ('apc_dec ' );
5162 }
5263
@@ -63,8 +74,9 @@ public function write($key, $value, $duration) {
6374 if ($ duration ) {
6475 $ expires = time () + $ duration ;
6576 }
66- apc_store ($ key . '_expires ' , $ expires , $ duration );
67- return apc_store ($ key , $ value , $ duration );
77+ $ func = $ this ->_apcExtension . '_store ' ;
78+ $ func ($ key . '_expires ' , $ expires , $ duration );
79+ return $ func ($ key , $ value , $ duration );
6880 }
6981
7082/**
@@ -75,11 +87,12 @@ public function write($key, $value, $duration) {
7587 */
7688 public function read ($ key ) {
7789 $ time = time ();
78- $ cachetime = (int )apc_fetch ($ key . '_expires ' );
90+ $ func = $ this ->_apcExtension . '_fetch ' ;
91+ $ cachetime = (int )$ func ($ key . '_expires ' );
7992 if ($ cachetime !== 0 && ($ cachetime < $ time || ($ time + $ this ->settings ['duration ' ]) < $ cachetime )) {
8093 return false ;
8194 }
82- return apc_fetch ($ key );
95+ return $ func ($ key );
8396 }
8497
8598/**
@@ -90,7 +103,8 @@ public function read($key) {
90103 * @return New incremented value, false otherwise
91104 */
92105 public function increment ($ key , $ offset = 1 ) {
93- return apc_inc ($ key , $ offset );
106+ $ func = $ this ->_apcExtension . '_inc ' ;
107+ return $ func ($ key , $ offset );
94108 }
95109
96110/**
@@ -101,7 +115,8 @@ public function increment($key, $offset = 1) {
101115 * @return New decremented value, false otherwise
102116 */
103117 public function decrement ($ key , $ offset = 1 ) {
104- return apc_dec ($ key , $ offset );
118+ $ func = $ this ->_apcExtension . '_dec ' ;
119+ return $ func ($ key , $ offset );
105120 }
106121
107122/**
@@ -111,7 +126,8 @@ public function decrement($key, $offset = 1) {
111126 * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
112127 */
113128 public function delete ($ key ) {
114- return apc_delete ($ key );
129+ $ func = $ this ->_apcExtension . '_delete ' ;
130+ return $ func ($ key );
115131 }
116132
117133/**
@@ -125,19 +141,20 @@ public function clear($check) {
125141 if ($ check ) {
126142 return true ;
127143 }
144+ $ func = $ this ->_apcExtension . '_delete ' ;
128145 if (class_exists ('APCIterator ' , false )) {
129146 $ iterator = new APCIterator (
130147 'user ' ,
131148 '/^ ' . preg_quote ($ this ->settings ['prefix ' ], '/ ' ) . '/ ' ,
132149 APC_ITER_NONE
133150 );
134- apc_delete ($ iterator );
151+ $ func ($ iterator );
135152 return true ;
136153 }
137- $ cache = apc_cache_info ('user ' );
154+ $ cache = $ this -> _apcExtension === ' apc ' ? apc_cache_info ('user ' ) : apcu_cache_info ( );
138155 foreach ($ cache ['cache_list ' ] as $ key ) {
139156 if (strpos ($ key ['info ' ], $ this ->settings ['prefix ' ]) === 0 ) {
140- apc_delete ($ key ['info ' ]);
157+ $ func ($ key ['info ' ]);
141158 }
142159 }
143160 return true ;
@@ -157,11 +174,13 @@ public function groups() {
157174 }
158175 }
159176
160- $ groups = apc_fetch ($ this ->_compiledGroupNames );
177+ $ fetchFunc = $ this ->_apcExtension . '_fetch ' ;
178+ $ storeFunc = $ this ->_apcExtension . '_store ' ;
179+ $ groups = $ fetchFunc ($ this ->_compiledGroupNames );
161180 if (count ($ groups ) !== count ($ this ->settings ['groups ' ])) {
162181 foreach ($ this ->_compiledGroupNames as $ group ) {
163182 if (!isset ($ groups [$ group ])) {
164- apc_store ($ group , 1 );
183+ $ storeFunc ($ group , 1 );
165184 $ groups [$ group ] = 1 ;
166185 }
167186 }
@@ -184,7 +203,8 @@ public function groups() {
184203 * @return bool success
185204 */
186205 public function clearGroup ($ group ) {
187- apc_inc ($ this ->settings ['prefix ' ] . $ group , 1 , $ success );
206+ $ func = $ this ->_apcExtension . '_inc ' ;
207+ $ func ($ this ->settings ['prefix ' ] . $ group , 1 , $ success );
188208 return $ success ;
189209 }
190210
@@ -203,7 +223,8 @@ public function add($key, $value, $duration) {
203223 if ($ duration ) {
204224 $ expires = time () + $ duration ;
205225 }
206- apc_add ($ key . '_expires ' , $ expires , $ duration );
207- return apc_add ($ key , $ value , $ duration );
226+ $ func = $ this ->_apcExtension . '_add ' ;
227+ $ func ($ key . '_expires ' , $ expires , $ duration );
228+ return $ func ($ key , $ value , $ duration );
208229 }
209230}
0 commit comments