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.
This commit is contained in:
Juan Basso 2016-05-07 14:26:09 -04:00
parent 4a61f7f23c
commit fa6e74ca22
No known key found for this signature in database
GPG key ID: 0609DC24D13F096C
2 changed files with 44 additions and 42 deletions

View file

@ -74,8 +74,9 @@ class ApcEngine extends CacheEngine {
if ($duration) {
$expires = time() + $duration;
}
$this->_apcCall('store', $key . '_expires', $expires, $duration);
return $this->_apcCall('store', $key, $value, $duration);
$func = $this->_apcExtension . '_store';
$func($key . '_expires', $expires, $duration);
return $func($key, $value, $duration);
}
/**
@ -86,11 +87,12 @@ class ApcEngine extends CacheEngine {
*/
public function read($key) {
$time = time();
$cachetime = (int)$this->_apcCall('fetch', $key . '_expires');
$func = $this->_apcExtension . '_fetch';
$cachetime = (int)$func($key . '_expires');
if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
return false;
}
return $this->_apcCall('fetch', $key);
return $func($key);
}
/**
@ -101,7 +103,8 @@ class ApcEngine extends CacheEngine {
* @return New incremented value, false otherwise
*/
public function increment($key, $offset = 1) {
return $this->_apcCall('inc', $key, $offset);
$func = $this->_apcExtension . '_inc';
return $func($key, $offset);
}
/**
@ -112,7 +115,8 @@ class ApcEngine extends CacheEngine {
* @return New decremented value, false otherwise
*/
public function decrement($key, $offset = 1) {
return $this->_apcCall('dec', $key, $offset);
$func = $this->_apcExtension . '_dec';
return $func($key, $offset);
}
/**
@ -122,7 +126,8 @@ class ApcEngine extends CacheEngine {
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
*/
public function delete($key) {
return $this->_apcCall('delete', $key);
$func = $this->_apcExtension . '_delete';
return $func($key);
}
/**
@ -136,19 +141,20 @@ class ApcEngine extends CacheEngine {
if ($check) {
return true;
}
$func = $this->_apcExtension . '_delete';
if (class_exists('APCIterator', false)) {
$iterator = new APCIterator(
'user',
'/^' . preg_quote($this->settings['prefix'], '/') . '/',
APC_ITER_NONE
);
$this->_apcCall('delete', $iterator);
$func($iterator);
return true;
}
$cache = $this->_apcExtension === 'apc' ? apc_cache_info('user') : apcu_cache_info();
foreach ($cache['cache_list'] as $key) {
if (strpos($key['info'], $this->settings['prefix']) === 0) {
$this->_apcCall('delete', $key['info']);
$func($key['info']);
}
}
return true;
@ -168,11 +174,13 @@ class ApcEngine extends CacheEngine {
}
}
$groups = $this->_apcCall('fetch', $this->_compiledGroupNames);
$fetchFunc = $this->_apcExtension . '_fetch';
$storeFunc = $this->_apcExtension . '_store';
$groups = $fetchFunc($this->_compiledGroupNames);
if (count($groups) !== count($this->settings['groups'])) {
foreach ($this->_compiledGroupNames as $group) {
if (!isset($groups[$group])) {
$this->_apcCall('store', $group, 1);
$storeFunc($group, 1);
$groups[$group] = 1;
}
}
@ -195,7 +203,7 @@ class ApcEngine extends CacheEngine {
* @return bool success
*/
public function clearGroup($group) {
$func = function_exists('apc_inc') ? 'apc_inc' : 'apcu_inc';
$func = $this->_apcExtension . '_inc';
$func($this->settings['prefix'] . $group, 1, $success);
return $success;
}
@ -215,18 +223,8 @@ class ApcEngine extends CacheEngine {
if ($duration) {
$expires = time() + $duration;
}
$this->_apcCall('add', $key . '_expires', $expires, $duration);
return $this->_apcCall('add', $key, $value, $duration);
}
/**
* Call APC(u) function
*
* @return mixed
*/
protected function _apcCall() {
$params = func_get_args();
$func = $this->_apcExtension . '_' . array_shift($params);
return call_user_func_array($func, $params);
$func = $this->_apcExtension . '_add';
$func($key . '_expires', $expires, $duration);
return $func($key, $value, $duration);
}
}

View file

@ -25,6 +25,13 @@ App::uses('Cache', 'Cache');
*/
class ApcEngineTest extends CakeTestCase {
/**
* APC extension to be used
*
* @var string
*/
protected $_apcExtension = 'apc';
/**
* setUp method
*
@ -39,6 +46,10 @@ class ApcEngineTest extends CakeTestCase {
$this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.');
}
if (extension_loaded('apcu')) {
$this->_apcExtension = 'apcu';
}
$this->_cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);
Cache::config('apc', array('engine' => 'Apc', 'prefix' => 'cake_'));
@ -198,14 +209,18 @@ class ApcEngineTest extends CakeTestCase {
* @return void
*/
public function testClear() {
$this->_apcCall('store', 'not_cake', 'survive');
$storeFunc = $this->_apcExtension . '_store';
$fetchFunc = $this->_apcExtension . '_fetch';
$deleteFunc = $this->_apcExtension . '_delete';
$storeFunc('not_cake', 'survive');
Cache::write('some_value', 'value', 'apc');
$result = Cache::clear(false, 'apc');
$this->assertTrue($result);
$this->assertFalse(Cache::read('some_value', 'apc'));
$this->assertEquals('survive', $this->_apcCall('fetch', 'not_cake'));
$this->_apcCall('delete', 'not_cake');
$this->assertEquals('survive', $fetchFunc('not_cake'));
$deleteFunc('not_cake');
}
/**
@ -216,6 +231,7 @@ class ApcEngineTest extends CakeTestCase {
* @return void
*/
public function testGroupsReadWrite() {
$incFunc = $this->_apcExtension . '_inc';
Cache::config('apc_groups', array(
'engine' => 'Apc',
'duration' => 0,
@ -225,12 +241,12 @@ class ApcEngineTest extends CakeTestCase {
$this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));
$this->_apcCall('inc', 'test_group_a');
$incFunc('test_group_a');
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
$this->assertEquals('value2', Cache::read('test_groups', 'apc_groups'));
$this->_apcCall('inc', 'test_group_b');
$incFunc('test_group_b');
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
$this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups'));
$this->assertEquals('value3', Cache::read('test_groups', 'apc_groups'));
@ -295,16 +311,4 @@ class ApcEngineTest extends CakeTestCase {
$result = Cache::add('test_add_key', 'test data 2', 'apc');
$this->assertFalse($result);
}
/**
* Call APC/APCu function
*
* @return mixed
*/
protected function _apcCall() {
$params = func_get_args();
$ext = extension_loaded('apc') ? 'apc' : 'apcu';
$func = $ext . '_' . array_shift($params);
return call_user_func_array($func, $params);
}
}