Supporting APCu on PHP 7

This commit is contained in:
Juan Basso 2016-05-07 02:42:37 -04:00
parent 5b83f702c9
commit 4a61f7f23c
No known key found for this signature in database
GPG key ID: 0609DC24D13F096C
3 changed files with 63 additions and 23 deletions

View file

@ -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 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" - 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 - 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 - phpenv rehash
- set +H - set +H
- echo "<?php - echo "<?php

View file

@ -31,6 +31,13 @@ class ApcEngine extends CacheEngine {
*/ */
protected $_compiledGroupNames = array(); protected $_compiledGroupNames = array();
/**
* APC or APCu extension
*
* @var string
*/
protected $_apcExtension = 'apc';
/** /**
* Initialize the Cache Engine * Initialize the Cache Engine
* *
@ -47,6 +54,10 @@ class ApcEngine extends CacheEngine {
} }
$settings += array('engine' => 'Apc'); $settings += array('engine' => 'Apc');
parent::init($settings); parent::init($settings);
if (function_exists('apcu_dec')) {
$this->_apcExtension = 'apcu';
return true;
}
return function_exists('apc_dec'); return function_exists('apc_dec');
} }
@ -63,8 +74,8 @@ class ApcEngine extends CacheEngine {
if ($duration) { if ($duration) {
$expires = time() + $duration; $expires = time() + $duration;
} }
apc_store($key . '_expires', $expires, $duration); $this->_apcCall('store', $key . '_expires', $expires, $duration);
return apc_store($key, $value, $duration); return $this->_apcCall('store', $key, $value, $duration);
} }
/** /**
@ -75,11 +86,11 @@ class ApcEngine extends CacheEngine {
*/ */
public function read($key) { public function read($key) {
$time = time(); $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)) { if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime)) {
return false; return false;
} }
return apc_fetch($key); return $this->_apcCall('fetch', $key);
} }
/** /**
@ -90,7 +101,7 @@ class ApcEngine extends CacheEngine {
* @return New incremented value, false otherwise * @return New incremented value, false otherwise
*/ */
public function increment($key, $offset = 1) { public function increment($key, $offset = 1) {
return apc_inc($key, $offset); return $this->_apcCall('inc', $key, $offset);
} }
/** /**
@ -101,7 +112,7 @@ class ApcEngine extends CacheEngine {
* @return New decremented value, false otherwise * @return New decremented value, false otherwise
*/ */
public function decrement($key, $offset = 1) { public function decrement($key, $offset = 1) {
return apc_dec($key, $offset); return $this->_apcCall('dec', $key, $offset);
} }
/** /**
@ -111,7 +122,7 @@ class ApcEngine extends CacheEngine {
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
*/ */
public function delete($key) { public function delete($key) {
return apc_delete($key); return $this->_apcCall('delete', $key);
} }
/** /**
@ -131,13 +142,13 @@ class ApcEngine extends CacheEngine {
'/^' . preg_quote($this->settings['prefix'], '/') . '/', '/^' . preg_quote($this->settings['prefix'], '/') . '/',
APC_ITER_NONE APC_ITER_NONE
); );
apc_delete($iterator); $this->_apcCall('delete', $iterator);
return true; return true;
} }
$cache = apc_cache_info('user'); $cache = $this->_apcExtension === 'apc' ? apc_cache_info('user') : apcu_cache_info();
foreach ($cache['cache_list'] as $key) { foreach ($cache['cache_list'] as $key) {
if (strpos($key['info'], $this->settings['prefix']) === 0) { if (strpos($key['info'], $this->settings['prefix']) === 0) {
apc_delete($key['info']); $this->_apcCall('delete', $key['info']);
} }
} }
return true; return true;
@ -157,11 +168,11 @@ class ApcEngine extends CacheEngine {
} }
} }
$groups = apc_fetch($this->_compiledGroupNames); $groups = $this->_apcCall('fetch', $this->_compiledGroupNames);
if (count($groups) !== count($this->settings['groups'])) { if (count($groups) !== count($this->settings['groups'])) {
foreach ($this->_compiledGroupNames as $group) { foreach ($this->_compiledGroupNames as $group) {
if (!isset($groups[$group])) { if (!isset($groups[$group])) {
apc_store($group, 1); $this->_apcCall('store', $group, 1);
$groups[$group] = 1; $groups[$group] = 1;
} }
} }
@ -184,7 +195,8 @@ class ApcEngine extends CacheEngine {
* @return bool success * @return bool success
*/ */
public function clearGroup($group) { 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; return $success;
} }
@ -203,7 +215,18 @@ class ApcEngine extends CacheEngine {
if ($duration) { if ($duration) {
$expires = time() + $duration; $expires = time() + $duration;
} }
apc_add($key . '_expires', $expires, $duration); $this->_apcCall('add', $key . '_expires', $expires, $duration);
return apc_add($key, $value, $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);
} }
} }

View file

@ -32,7 +32,8 @@ class ApcEngineTest extends CakeTestCase {
*/ */
public function setUp() { public function setUp() {
parent::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') { if (PHP_SAPI === 'cli') {
$this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.'); $this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.');
@ -147,7 +148,8 @@ class ApcEngineTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testDecrement() { 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'); $result = Cache::write('test_decrement', 5, 'apc');
$this->assertTrue($result); $this->assertTrue($result);
@ -171,7 +173,8 @@ class ApcEngineTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testIncrement() { 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'); $result = Cache::write('test_increment', 5, 'apc');
$this->assertTrue($result); $this->assertTrue($result);
@ -195,14 +198,14 @@ class ApcEngineTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testClear() { public function testClear() {
apc_store('not_cake', 'survive'); $this->_apcCall('store', 'not_cake', 'survive');
Cache::write('some_value', 'value', 'apc'); Cache::write('some_value', 'value', 'apc');
$result = Cache::clear(false, 'apc'); $result = Cache::clear(false, 'apc');
$this->assertTrue($result); $this->assertTrue($result);
$this->assertFalse(Cache::read('some_value', 'apc')); $this->assertFalse(Cache::read('some_value', 'apc'));
$this->assertEquals('survive', apc_fetch('not_cake')); $this->assertEquals('survive', $this->_apcCall('fetch', 'not_cake'));
apc_delete('not_cake'); $this->_apcCall('delete', 'not_cake');
} }
/** /**
@ -222,12 +225,12 @@ class ApcEngineTest extends CakeTestCase {
$this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups')); $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
$this->assertEquals('value', Cache::read('test_groups', '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->assertFalse(Cache::read('test_groups', 'apc_groups'));
$this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups')); $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
$this->assertEquals('value2', Cache::read('test_groups', '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->assertFalse(Cache::read('test_groups', 'apc_groups'));
$this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups')); $this->assertTrue(Cache::write('test_groups', 'value3', 'apc_groups'));
$this->assertEquals('value3', Cache::read('test_groups', 'apc_groups')); $this->assertEquals('value3', Cache::read('test_groups', 'apc_groups'));
@ -292,4 +295,16 @@ class ApcEngineTest extends CakeTestCase {
$result = Cache::add('test_add_key', 'test data 2', 'apc'); $result = Cache::add('test_add_key', 'test data 2', 'apc');
$this->assertFalse($result); $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);
}
} }