Skip to content

Commit

Permalink
Merge pull request #253 from tedious/v1.0.0-dev
Browse files Browse the repository at this point in the history
[WIP] v1.0.0 / v0.14.0
  • Loading branch information
tedivm committed Feb 1, 2016
2 parents 3489b13 + b10e2f5 commit 569112b
Show file tree
Hide file tree
Showing 53 changed files with 1,318 additions and 806 deletions.
43 changes: 43 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
## Stash v1.0.0 Changelog

### v1.0.0

* Implemented PSR-6 interfaces.

* Removed `Driver::setOptions($options)` in favor of `Driver::constructor($options)`

* Removed deprecated DriverList::getDrivers function.

* Removed deprecated invalidation constants in the Item class.

* Removed SQLite Extension support (SQLite3 is still available).

* The `set` function no longer persists data.

* Removed expiration time for `set` function

* Added `expiresAt` and `expiresAfter` functions to the Item class.

* `getExpiration` to return current datetime when no record exists.

* Added `save` function to PoolInterface.

* Changed `getItemIterator` to `getItems`

* RuntimeException now extends from \RuntimeException

* Added `isHit` function to ItemInterface.

* Added the `hasItem` function to the Pool, which should mostly be avoided.

* Renamed `Pool::purge` to `Pool::clear`.

* Added `Pool::deleteItem` and `Pool::deleteItems`.

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

* Added support for "APCU" functions.

* Removed sqlite2 support (sqlite3 is still supported).


## Stash v0.13 Changelog


Expand Down
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"sessions",
"memcached",
"redis",
"apc"
"apc",
"psr-6",
"psr6"
],
"homepage": "http://github.com/tedious/Stash",
"type": "library",
Expand All @@ -23,16 +25,20 @@
}
],
"require": {
"php": "^5.4|^7.0"
"php": "^5.4|^7.0",
"psr/cache": "~1.0"
},
"require-dev": {
"fabpot/php-cs-fixer": "^1.9",
"phpunit/phpunit": "4.7.*",
"phpunit/phpunit": "4.8.*",
"satooshi/php-coveralls": "1.0.*"
},
"autoload": {
"psr-4": {
"Stash\\": "src/Stash/"
}
},
"provide": {
"psr/cache-implementation": "1.0.0"
}
}
2 changes: 1 addition & 1 deletion src/Stash/Driver/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getDefaultOptions()
/**
* {@inheritdoc}
*/
public function setOptions(array $options = array())
protected function setOptions(array $options = array())
{
// empty
}
Expand Down
51 changes: 37 additions & 14 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 @@ -62,12 +74,13 @@ public function getDefaultOptions()
*
* @param array $options
*/
public function setOptions(array $options = array())
protected function setOptions(array $options = array())
{
$options += $this->getDefaultOptions();

$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,10 @@ 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 +113,18 @@ 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() : 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);
$it = $this->apcu ? new \APCUIterator($keyRegex, \APC_ITER_KEY, $chunkSize) : new \APCIterator('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 @@ -124,14 +140,13 @@ public function purge()
$now = time();
$keyRegex = '[' . $this->makeKey(array()) . '*]';
$chunkSize = isset($this->chunkSize) && is_numeric($this->chunkSize) ? $this->chunkSize : 100;

$it = new \APCIterator('user', $keyRegex, \APC_ITER_KEY, $chunkSize);
$it = $this->apcu ? new \APCUIterator($keyRegex, \APC_ITER_KEY, $chunkSize) : 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,13 +160,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 (extension_loaded('apc') && ini_get('apc.enabled'))
&& ((php_sapi_name() !== 'cli') || ini_get('apc.enable_cli'));
return function_exists('apcu_fetch') || function_exists('apc_fetch');
}

/**
Expand Down Expand Up @@ -187,4 +201,13 @@ protected function getCacheTime($expiration)

return $this->ttl < $life ? $this->ttl : $life;
}


/**
* {@inheritdoc}
*/
public function isPersistent()
{
return true;
}
}
31 changes: 17 additions & 14 deletions src/Stash/Driver/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Stash;
use Stash\Exception\RuntimeException;
use Stash\Exception\InvalidArgumentException;
use Stash\Interfaces\DriverInterface;

/**
Expand All @@ -39,26 +40,28 @@ class Composite extends AbstractDriver
*
* @throws \Stash\Exception\RuntimeException
*/
public function setOptions(array $options = array())
protected function setOptions(array $options = array())
{
$options += $this->getDefaultOptions();

if (isset($options['drivers'])) {
if (count($options['drivers']) < 1) {
throw new RuntimeException('One or more secondary drivers are required.');
}
$this->drivers = array();
if (!isset($options['drivers']) || (count($options['drivers']) < 1)) {
throw new RuntimeException('One or more secondary drivers are required.');
}

foreach ($options['drivers'] as $driver) {
if (!(is_object($driver) && $driver instanceof DriverInterface)) {
continue;
}
$this->drivers[] = $driver;
}
if (!is_array($options['drivers'])) {
throw new InvalidArgumentException('Drivers option requires an array.');
}

if (count($this->drivers) < 1) {
throw new RuntimeException('None of the secondary drivers can be enabled.');
$this->drivers = array();
foreach ($options['drivers'] as $driver) {
if (!(is_object($driver) && $driver instanceof DriverInterface)) {
continue;
}
$this->drivers[] = $driver;
}

if (count($this->drivers) < 1) {
throw new RuntimeException('None of the secondary drivers can be enabled.');
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/Stash/Driver/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getDefaultOptions()
*
* @throws \Stash\Exception\RuntimeException
*/
public function setOptions(array $options = array())
protected function setOptions(array $options = array())
{
$options += $this->getDefaultOptions();
if (!isset($options['path'])) {
Expand All @@ -156,10 +156,11 @@ public function setOptions(array $options = array())
}
$this->encoder = new $encoder;
} else {
$encoderInterface = 'Stash\Driver\FileSystem\EncoderInterface';
$encoderClass = 'Stash\Driver\FileSystem\\' . $encoder . 'Encoder';
if (class_exists($encoder)) {
if (class_exists($encoder) && in_array($encoderInterface, class_implements($encoder))) {
$this->encoder = new $encoder();
} elseif (class_exists($encoderClass)) {
} elseif (class_exists($encoderClass) && in_array($encoderInterface, class_implements($encoderClass))) {
$this->encoder = new $encoderClass();
} else {
throw new RuntimeException('Invalid Encoder: ' . $encoder);
Expand Down
23 changes: 8 additions & 15 deletions src/Stash/Driver/FileSystem/NativeEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ public function deserialize($path)
return false;
}

$expiration = null;
include($path);

if (!isset($loaded)) {
return false;
}

if (!isset($expiration)) {
$expiration = null;
}

if (!isset($data)) {
$data = null;
}
Expand Down Expand Up @@ -92,22 +89,18 @@ protected function encode($data)
$dataString = (bool) $data ? 'true' : 'false';
break;

case 'serialize':
$dataString = 'unserialize(base64_decode(\'' . base64_encode(serialize($data)) . '\'))';
break;

case 'string':
$dataString = sprintf('"%s"', addcslashes($data, "\t\"\$\\"));
break;

case 'none':
default :
if (is_numeric($data)) {
$dataString = (string) $data;
} else {
$dataString = 'base64_decode(\'' . base64_encode($data) . '\')';
}
case 'numeric':
$dataString = (string) $data;
break;

default :
case 'serialize':
$dataString = 'unserialize(base64_decode(\'' . base64_encode(serialize($data)) . '\'))';
break;
}

return $dataString;
Expand Down
2 changes: 1 addition & 1 deletion src/Stash/Driver/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getDefaultOptions()
*
* @throws RuntimeException
*/
public function setOptions(array $options = array())
protected function setOptions(array $options = array())
{
$options += $this->getDefaultOptions();

Expand Down
2 changes: 1 addition & 1 deletion src/Stash/Driver/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Redis extends AbstractDriver
*
* @param array $options
*/
public function setOptions(array $options = array())
protected function setOptions(array $options = array())
{
$options += $this->getDefaultOptions();

Expand Down
Loading

0 comments on commit 569112b

Please sign in to comment.