Skip to content

Commit

Permalink
Supporting APCu on PHP 7
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed May 7, 2016
1 parent 5b83f70 commit 4a61f7f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -41,6 +41,8 @@ before_script:
- sh -c "if [ '$PHPCS' = '1' ]; then composer global require 'cakephp/cakephp-codesniffer:1.*'; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then ~/.composer/vendor/bin/phpcs --config-set installed_paths ~/.composer/vendor/cakephp/cakephp-codesniffer; fi"
- echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.0" ]] ; then print "yes" | pecl install apcu-5.1.3; else print "yes" | pecl install apcu-4.0.11; fi
- echo -e "extension = apcu.so\napc.enable_cli=1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- phpenv rehash
- set +H
- echo "<?php
Expand Down
53 changes: 38 additions & 15 deletions lib/Cake/Cache/Engine/ApcEngine.php
Expand Up @@ -31,6 +31,13 @@ class ApcEngine extends CacheEngine {
*/
protected $_compiledGroupNames = array();

/**
* APC or APCu extension
*
* @var string
*/
protected $_apcExtension = 'apc';

/**
* Initialize the Cache Engine
*
Expand All @@ -47,6 +54,10 @@ public function init($settings = array()) {
}
$settings += array('engine' => 'Apc');
parent::init($settings);
if (function_exists('apcu_dec')) {
$this->_apcExtension = 'apcu';
return true;
}
return function_exists('apc_dec');
}

Expand All @@ -63,8 +74,8 @@ public function write($key, $value, $duration) {
if ($duration) {
$expires = time() + $duration;
}
apc_store($key . '_expires', $expires, $duration);
return apc_store($key, $value, $duration);
$this->_apcCall('store', $key . '_expires', $expires, $duration);
return $this->_apcCall('store', $key, $value, $duration);
}

/**
Expand All @@ -75,11 +86,11 @@ public function write($key, $value, $duration) {
*/
public function read($key) {
$time = time();
$cachetime = (int)apc_fetch($key . '_expires');
$cachetime = (int)$this->_apcCall('fetch', $key . '_expires');
if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
return false;
}
return apc_fetch($key);
return $this->_apcCall('fetch', $key);
}

/**
Expand All @@ -90,7 +101,7 @@ public function read($key) {
* @return New incremented value, false otherwise
*/
public function increment($key, $offset = 1) {
return apc_inc($key, $offset);
return $this->_apcCall('inc', $key, $offset);
}

/**
Expand All @@ -101,7 +112,7 @@ public function increment($key, $offset = 1) {
* @return New decremented value, false otherwise
*/
public function decrement($key, $offset = 1) {
return apc_dec($key, $offset);
return $this->_apcCall('dec', $key, $offset);
}

/**
Expand All @@ -111,7 +122,7 @@ public function decrement($key, $offset = 1) {
* @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 apc_delete($key);
return $this->_apcCall('delete', $key);
}

/**
Expand All @@ -131,13 +142,13 @@ public function clear($check) {
'/^' . preg_quote($this->settings['prefix'], '/') . '/',
APC_ITER_NONE
);
apc_delete($iterator);
$this->_apcCall('delete', $iterator);
return true;
}
$cache = apc_cache_info('user');
$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) {
apc_delete($key['info']);
$this->_apcCall('delete', $key['info']);
}
}
return true;
Expand All @@ -157,11 +168,11 @@ public function groups() {
}
}

$groups = apc_fetch($this->_compiledGroupNames);
$groups = $this->_apcCall('fetch', $this->_compiledGroupNames);
if (count($groups) !== count($this->settings['groups'])) {
foreach ($this->_compiledGroupNames as $group) {
if (!isset($groups[$group])) {
apc_store($group, 1);
$this->_apcCall('store', $group, 1);
$groups[$group] = 1;
}
}
Expand All @@ -184,7 +195,8 @@ public function groups() {
* @return bool success
*/
public function clearGroup($group) {
apc_inc($this->settings['prefix'] . $group, 1, $success);
$func = function_exists('apc_inc') ? 'apc_inc' : 'apcu_inc';
$func($this->settings['prefix'] . $group, 1, $success);
return $success;
}

Expand All @@ -203,7 +215,18 @@ public function add($key, $value, $duration) {
if ($duration) {
$expires = time() + $duration;
}
apc_add($key . '_expires', $expires, $duration);
return apc_add($key, $value, $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);
}
}
31 changes: 23 additions & 8 deletions lib/Cake/Test/Case/Cache/Engine/ApcEngineTest.php
Expand Up @@ -32,7 +32,8 @@ class ApcEngineTest extends CakeTestCase {
*/
public function setUp() {
parent::setUp();
$this->skipIf(!function_exists('apc_store'), 'Apc is not installed or configured properly.');
$hasApc = extension_loaded('apc') || extension_loaded('apcu');
$this->skipIf(!$hasApc, 'Apc is not installed or configured properly.');

if (PHP_SAPI === 'cli') {
$this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.');
Expand Down Expand Up @@ -147,7 +148,8 @@ public function testDeleteCache() {
* @return void
*/
public function testDecrement() {
$this->skipIf(!function_exists('apc_dec'), 'No apc_dec() function, cannot test decrement().');
$hasSupport = function_exists('apc_dec') || function_exists('apcu_dec');
$this->skipIf(!$hasSupport, 'No apc_dec()/apcu_dec() function, cannot test decrement().');

$result = Cache::write('test_decrement', 5, 'apc');
$this->assertTrue($result);
Expand All @@ -171,7 +173,8 @@ public function testDecrement() {
* @return void
*/
public function testIncrement() {
$this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment().');
$hasSupport = function_exists('apc_inc') || function_exists('apcu_inc');
$this->skipIf(!function_exists('apc_inc'), 'No apc_inc()/apcu_inc() function, cannot test increment().');

$result = Cache::write('test_increment', 5, 'apc');
$this->assertTrue($result);
Expand All @@ -195,14 +198,14 @@ public function testIncrement() {
* @return void
*/
public function testClear() {
apc_store('not_cake', 'survive');
$this->_apcCall('store', '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', apc_fetch('not_cake'));
apc_delete('not_cake');
$this->assertEquals('survive', $this->_apcCall('fetch', 'not_cake'));
$this->_apcCall('delete', 'not_cake');
}

/**
Expand All @@ -222,12 +225,12 @@ public function testGroupsReadWrite() {
$this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'apc_groups'));

apc_inc('test_group_a');
$this->_apcCall('inc', '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'));

apc_inc('test_group_b');
$this->_apcCall('inc', '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'));
Expand Down Expand Up @@ -292,4 +295,16 @@ public function testAdd() {
$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);
}
}

0 comments on commit 4a61f7f

Please sign in to comment.