Skip to content

Commit

Permalink
Using APCu instead of APC
Browse files Browse the repository at this point in the history
The APC extension is gone in PHP 5.5. Since we require this version now,
we need to adapt the engine so it is usable again
  • Loading branch information
lorenzo committed Dec 25, 2015
1 parent 017b795 commit 4957a5c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
48 changes: 19 additions & 29 deletions src/Cache/Engine/ApcEngine.php
Expand Up @@ -14,7 +14,7 @@
*/
namespace Cake\Cache\Engine;

use APCIterator;
use APCUIterator;
use Cake\Cache\CacheEngine;

/**
Expand Down Expand Up @@ -42,7 +42,7 @@ class ApcEngine extends CacheEngine
*/
public function init(array $config = [])
{
if (!extension_loaded('apc')) {
if (!extension_loaded('apcu')) {
return false;
}

Expand All @@ -66,8 +66,8 @@ public function write($key, $value)
if ($duration) {
$expires = time() + $duration;
}
apc_store($key . '_expires', $expires, $duration);
return apc_store($key, $value, $duration);
apcu_store($key . '_expires', $expires, $duration);
return apcu_store($key, $value, $duration);
}

/**
Expand All @@ -82,11 +82,11 @@ public function read($key)
$key = $this->_key($key);

$time = time();
$cachetime = (int)apc_fetch($key . '_expires');
$cachetime = (int)apcu_fetch($key . '_expires');
if ($cachetime !== 0 && ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime)) {
return false;
}
return apc_fetch($key);
return apcu_fetch($key);
}

/**
Expand All @@ -100,7 +100,7 @@ public function increment($key, $offset = 1)
{
$key = $this->_key($key);

return apc_inc($key, $offset);
return apcu_inc($key, $offset);
}

/**
Expand All @@ -114,7 +114,7 @@ public function decrement($key, $offset = 1)
{
$key = $this->_key($key);

return apc_dec($key, $offset);
return apcu_dec($key, $offset);
}

/**
Expand All @@ -127,7 +127,7 @@ public function delete($key)
{
$key = $this->_key($key);

return apc_delete($key);
return apcu_delete($key);
}

/**
Expand All @@ -142,21 +142,11 @@ public function clear($check)
if ($check) {
return true;
}
if (class_exists('APCIterator', false)) {
$iterator = new APCIterator(
'user',
'/^' . preg_quote($this->_config['prefix'], '/') . '/',
APC_ITER_NONE
);
apc_delete($iterator);
return true;
}
$cache = apc_cache_info('user');
foreach ($cache['cache_list'] as $key) {
if (strpos($key['info'], $this->_config['prefix']) === 0) {
apc_delete($key['info']);
}
}
$iterator = new APCUIterator(
'/^' . preg_quote($this->_config['prefix'], '/') . '/',
APC_ITER_NONE
);
apcu_delete($iterator);
return true;
}

Expand All @@ -178,8 +168,8 @@ public function add($key, $value)
if ($duration) {
$expires = time() + $duration;
}
apc_add($key . '_expires', $expires, $duration);
return apc_add($key, $value, $duration);
apcu_add($key . '_expires', $expires, $duration);
return apcu_add($key, $value, $duration);
}

/**
Expand All @@ -197,11 +187,11 @@ public function groups()
}
}

$groups = apc_fetch($this->_compiledGroupNames);
$groups = apcu_fetch($this->_compiledGroupNames);
if (count($groups) !== count($this->_config['groups'])) {
foreach ($this->_compiledGroupNames as $group) {
if (!isset($groups[$group])) {
apc_store($group, 1);
apcu_store($group, 1);
$groups[$group] = 1;
}
}
Expand All @@ -225,7 +215,7 @@ public function groups()
*/
public function clearGroup($group)
{
apc_inc($this->_config['prefix'] . $group, 1, $success);
apcu_inc($this->_config['prefix'] . $group, 1, $success);
return $success;
}
}
16 changes: 6 additions & 10 deletions tests/TestCase/Cache/Engine/ApcEngineTest.php
Expand Up @@ -34,7 +34,7 @@ class ApcEngineTest extends TestCase
public function setUp()
{
parent::setUp();
$this->skipIf(!function_exists('apc_store'), 'Apc is not installed or configured properly.');
$this->skipIf(!function_exists('apcu_store'), 'Apc is not installed or configured properly.');

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

$result = Cache::write('test_decrement', 5, 'apc');
$this->assertTrue($result);

Expand All @@ -180,8 +178,6 @@ public function testDecrement()
*/
public function testIncrement()
{
$this->skipIf(!function_exists('apc_inc'), 'No apc_inc() function, cannot test increment().');

$result = Cache::write('test_increment', 5, 'apc');
$this->assertTrue($result);

Expand All @@ -205,14 +201,14 @@ public function testIncrement()
*/
public function testClear()
{
apc_store('not_cake', 'survive');
apcu_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', apcu_fetch('not_cake'));
apcu_delete('not_cake');
}

/**
Expand All @@ -233,12 +229,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');
apcu_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');
apcu_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

0 comments on commit 4957a5c

Please sign in to comment.