Skip to content

Commit

Permalink
Added APCU support.
Browse files Browse the repository at this point in the history
This should solve ##274.
  • Loading branch information
tedivm committed Dec 14, 2015
1 parent 6b8c33d commit 00bcc6b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### v1.0.0

* Implemented PSR-6 interfaces.
* Implemented PSR-6 interfaces.

* Removed deprecated DriverList::getDrivers function.

Expand Down Expand Up @@ -34,6 +34,8 @@

* Removed legacy methods for defining keys- keys must be defined as strings.

* Added support for "APCU" functions.


## Stash v0.13 Changelog

Expand Down
38 changes: 28 additions & 10 deletions src/Stash/Driver/Apc.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class Apc extends AbstractDriver
*/
protected $apcNamespace;

/**
* Whether to use the APCu functions or the original APC ones.
*
* @var string
*/
protected $apcu = false;


/**
* The number of records \ApcIterator will grab at once.
*
Expand All @@ -51,6 +59,10 @@ public function getDefaultOptions()
return array(
'ttl' => 300,
'namespace' => md5(__FILE__),

// Test using the APCUIterator, as some versions of APCU have the
// custom functions but not the iterator class.
'apcu' => class_exists('\APCUIterator')
);
}

Expand All @@ -68,6 +80,7 @@ public function setOptions(array $options = array())

$this->ttl = (int) $options['ttl'];
$this->apcNamespace = $options['namespace'];
$this->apcu = $options['apcu'];
}

/**
Expand All @@ -77,7 +90,7 @@ public function getData($key)
{
$keyString = self::makeKey($key);
$success = null;
$data = apc_fetch($keyString, $success);
$data = $this->apcu ? apcu_fetch($keyString, $success) : apc_fetch($keyString, $success);

return $success ? $data : false;
}
Expand All @@ -88,8 +101,11 @@ public function getData($key)
public function storeData($key, $data, $expiration)
{
$life = $this->getCacheTime($expiration);
$apckey = $this->makeKey($key);
$store = array('data' => $data, 'expiration' => $expiration);

return apc_store($this->makeKey($key), array('data' => $data, 'expiration' => $expiration), $life);

return $this->apcu ? apcu_store($apckey, $store, $life) : apc_store($apckey, $store, $life);
}

/**
Expand All @@ -98,17 +114,19 @@ public function storeData($key, $data, $expiration)
public function clear($key = null)
{
if (!isset($key)) {
return apc_clear_cache('user');
return $this->apcu ? apcu_clear_cache('user') : apc_clear_cache('user');
} else {
$keyRegex = '[' . $this->makeKey($key) . '*]';
$chunkSize = isset($this->chunkSize) && is_numeric($this->chunkSize) ? $this->chunkSize : 100;

do {
$emptyIterator = true;
$it = new \APCIterator('user', $keyRegex, \APC_ITER_KEY, $chunkSize);
$iteratorClass = $this->apcu ? '\APCUIterator' : '\APCIterator';
$it = new $iteratorClass('user', $keyRegex, \APC_ITER_KEY, $chunkSize);

foreach ($it as $item) {
$emptyIterator = false;
apc_delete($item['key']);
$this->apcu ? apcu_delete($item['key']) : apc_delete($item['key']);
}
} while (!$emptyIterator);
}
Expand All @@ -128,10 +146,10 @@ public function purge()
$it = new \APCIterator('user', $keyRegex, \APC_ITER_KEY, $chunkSize);
foreach ($it as $item) {
$success = null;
$data = apc_fetch($item['key'], $success);
$data = $this->apcu ? apcu_fetch($item['key'], $success): apc_fetch($item['key'], $success);

if ($success && is_array($data) && $data['expiration'] <= $now) {
apc_delete($item['key']);
$this->apcu ? apcu_delete($item['key']) : apc_delete($item['key']);
}
}

Expand All @@ -145,12 +163,12 @@ public function purge()
*/
public static function isAvailable()
{
// HHVM has some of the APC extension, but not all of it.
if (!class_exists('\APCIterator')) {
// Some versions of HHVM are missing the APCIterator
if (!class_exists('\APCIterator') && !class_exists('\APCUIterator')) {
return false;
}

return function_exists('apc_fetch');
return function_exists('apcu_fetch') || function_exists('apc_fetch');
}

/**
Expand Down

0 comments on commit 00bcc6b

Please sign in to comment.