Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit fa6e74c

Browse files
committed
Improved apc calls
Instead of calling the call_user_func_array it does the call directly. It is a little bit more verbose, but performs better.
1 parent 4a61f7f commit fa6e74c

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

lib/Cake/Cache/Engine/ApcEngine.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ public function write($key, $value, $duration) {
7474
if ($duration) {
7575
$expires = time() + $duration;
7676
}
77-
$this->_apcCall('store', $key . '_expires', $expires, $duration);
78-
return $this->_apcCall('store', $key, $value, $duration);
77+
$func = $this->_apcExtension . '_store';
78+
$func($key . '_expires', $expires, $duration);
79+
return $func($key, $value, $duration);
7980
}
8081

8182
/**
@@ -86,11 +87,12 @@ public function write($key, $value, $duration) {
8687
*/
8788
public function read($key) {
8889
$time = time();
89-
$cachetime = (int)$this->_apcCall('fetch', $key . '_expires');
90+
$func = $this->_apcExtension . '_fetch';
91+
$cachetime = (int)$func($key . '_expires');
9092
if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
9193
return false;
9294
}
93-
return $this->_apcCall('fetch', $key);
95+
return $func($key);
9496
}
9597

9698
/**
@@ -101,7 +103,8 @@ public function read($key) {
101103
* @return New incremented value, false otherwise
102104
*/
103105
public function increment($key, $offset = 1) {
104-
return $this->_apcCall('inc', $key, $offset);
106+
$func = $this->_apcExtension . '_inc';
107+
return $func($key, $offset);
105108
}
106109

107110
/**
@@ -112,7 +115,8 @@ public function increment($key, $offset = 1) {
112115
* @return New decremented value, false otherwise
113116
*/
114117
public function decrement($key, $offset = 1) {
115-
return $this->_apcCall('dec', $key, $offset);
118+
$func = $this->_apcExtension . '_dec';
119+
return $func($key, $offset);
116120
}
117121

118122
/**
@@ -122,7 +126,8 @@ public function decrement($key, $offset = 1) {
122126
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
123127
*/
124128
public function delete($key) {
125-
return $this->_apcCall('delete', $key);
129+
$func = $this->_apcExtension . '_delete';
130+
return $func($key);
126131
}
127132

128133
/**
@@ -136,19 +141,20 @@ public function clear($check) {
136141
if ($check) {
137142
return true;
138143
}
144+
$func = $this->_apcExtension . '_delete';
139145
if (class_exists('APCIterator', false)) {
140146
$iterator = new APCIterator(
141147
'user',
142148
'/^' . preg_quote($this->settings['prefix'], '/') . '/',
143149
APC_ITER_NONE
144150
);
145-
$this->_apcCall('delete', $iterator);
151+
$func($iterator);
146152
return true;
147153
}
148154
$cache = $this->_apcExtension === 'apc' ? apc_cache_info('user') : apcu_cache_info();
149155
foreach ($cache['cache_list'] as $key) {
150156
if (strpos($key['info'], $this->settings['prefix']) === 0) {
151-
$this->_apcCall('delete', $key['info']);
157+
$func($key['info']);
152158
}
153159
}
154160
return true;
@@ -168,11 +174,13 @@ public function groups() {
168174
}
169175
}
170176

171-
$groups = $this->_apcCall('fetch', $this->_compiledGroupNames);
177+
$fetchFunc = $this->_apcExtension . '_fetch';
178+
$storeFunc = $this->_apcExtension . '_store';
179+
$groups = $fetchFunc($this->_compiledGroupNames);
172180
if (count($groups) !== count($this->settings['groups'])) {
173181
foreach ($this->_compiledGroupNames as $group) {
174182
if (!isset($groups[$group])) {
175-
$this->_apcCall('store', $group, 1);
183+
$storeFunc($group, 1);
176184
$groups[$group] = 1;
177185
}
178186
}
@@ -195,7 +203,7 @@ public function groups() {
195203
* @return bool success
196204
*/
197205
public function clearGroup($group) {
198-
$func = function_exists('apc_inc') ? 'apc_inc' : 'apcu_inc';
206+
$func = $this->_apcExtension . '_inc';
199207
$func($this->settings['prefix'] . $group, 1, $success);
200208
return $success;
201209
}
@@ -215,18 +223,8 @@ public function add($key, $value, $duration) {
215223
if ($duration) {
216224
$expires = time() + $duration;
217225
}
218-
$this->_apcCall('add', $key . '_expires', $expires, $duration);
219-
return $this->_apcCall('add', $key, $value, $duration);
220-
}
221-
222-
/**
223-
* Call APC(u) function
224-
*
225-
* @return mixed
226-
*/
227-
protected function _apcCall() {
228-
$params = func_get_args();
229-
$func = $this->_apcExtension . '_' . array_shift($params);
230-
return call_user_func_array($func, $params);
226+
$func = $this->_apcExtension . '_add';
227+
$func($key . '_expires', $expires, $duration);
228+
return $func($key, $value, $duration);
231229
}
232230
}

lib/Cake/Test/Case/Cache/Engine/ApcEngineTest.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
*/
2626
class ApcEngineTest extends CakeTestCase {
2727

28+
/**
29+
* APC extension to be used
30+
*
31+
* @var string
32+
*/
33+
protected $_apcExtension = 'apc';
34+
2835
/**
2936
* setUp method
3037
*
@@ -39,6 +46,10 @@ public function setUp() {
3946
$this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.');
4047
}
4148

49+
if (extension_loaded('apcu')) {
50+
$this->_apcExtension = 'apcu';
51+
}
52+
4253
$this->_cacheDisable = Configure::read('Cache.disable');
4354
Configure::write('Cache.disable', false);
4455
Cache::config('apc', array('engine' => 'Apc', 'prefix' => 'cake_'));
@@ -198,14 +209,18 @@ public function testIncrement() {
198209
* @return void
199210
*/
200211
public function testClear() {
201-
$this->_apcCall('store', 'not_cake', 'survive');
212+
$storeFunc = $this->_apcExtension . '_store';
213+
$fetchFunc = $this->_apcExtension . '_fetch';
214+
$deleteFunc = $this->_apcExtension . '_delete';
215+
216+
$storeFunc('not_cake', 'survive');
202217
Cache::write('some_value', 'value', 'apc');
203218

204219
$result = Cache::clear(false, 'apc');
205220
$this->assertTrue($result);
206221
$this->assertFalse(Cache::read('some_value', 'apc'));
207-
$this->assertEquals('survive', $this->_apcCall('fetch', 'not_cake'));
208-
$this->_apcCall('delete', 'not_cake');
222+
$this->assertEquals('survive', $fetchFunc('not_cake'));
223+
$deleteFunc('not_cake');
209224
}
210225

211226
/**
@@ -216,6 +231,7 @@ public function testClear() {
216231
* @return void
217232
*/
218233
public function testGroupsReadWrite() {
234+
$incFunc = $this->_apcExtension . '_inc';
219235
Cache::config('apc_groups', array(
220236
'engine' => 'Apc',
221237
'duration' => 0,
@@ -225,12 +241,12 @@ public function testGroupsReadWrite() {
225241
$this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
226242
$this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
227243

228-
$this->_apcCall('inc', 'test_group_a');
244+
$incFunc('test_group_a');
229245
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
230246
$this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
231247
$this->assertEquals('value2', Cache::read('test_groups', 'apc_groups'));
232248

233-
$this->_apcCall('inc', 'test_group_b');
249+
$incFunc('test_group_b');
234250
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
235251
$this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups'));
236252
$this->assertEquals('value3', Cache::read('test_groups', 'apc_groups'));
@@ -295,16 +311,4 @@ public function testAdd() {
295311
$result = Cache::add('test_add_key', 'test data 2', 'apc');
296312
$this->assertFalse($result);
297313
}
298-
299-
/**
300-
* Call APC/APCu function
301-
*
302-
* @return mixed
303-
*/
304-
protected function _apcCall() {
305-
$params = func_get_args();
306-
$ext = extension_loaded('apc') ? 'apc' : 'apcu';
307-
$func = $ext . '_' . array_shift($params);
308-
return call_user_func_array($func, $params);
309-
}
310314
}

0 commit comments

Comments
 (0)