Skip to content

Commit

Permalink
Merge pull request #1 from ThaDafinser/feature/memory
Browse files Browse the repository at this point in the history
adding memory adapter
  • Loading branch information
ThaDafinser committed Feb 26, 2016
2 parents 628328d + ab990a3 commit e6a348c
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 43 deletions.
57 changes: 28 additions & 29 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
## Run on container environment
sudo: false

language: php

php:
- 5.6
- 7
- hhvm

env:
matrix:
- PREFER_LOWEST="--prefer-lowest"
- PREFER_LOWEST=""

matrix:
allow_failures:
- php: hhvm

before_script:
- composer update -o --prefer-source $PREFER_LOWEST

script:
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- vendor/bin/php-cs-fixer fix --dry-run -vv

after_script:
- if [ "$TRAVIS_PHP_VERSION" != "7" ] && [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "7" ] && [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi
## Run on container environment
sudo: false

language: php

php:
- 7
- hhvm

env:
matrix:
- PREFER_LOWEST="--prefer-lowest"
- PREFER_LOWEST=""

matrix:
allow_failures:
- php: hhvm

before_script:
- composer update -o --prefer-source $PREFER_LOWEST

script:
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- vendor/bin/php-cs-fixer fix --dry-run -vv

after_script:
- if [ "$TRAVIS_PHP_VERSION" != "7" ] && [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "7" ] && [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "thadafinser/psr6-null-cache",
"description": "PSR-6 cache NullObject implementation, to use it as default and or testing",
"description": "PSR-6 cache NullObject implementation, to avoid null checks and for testing",

"license": "MIT",

Expand All @@ -24,7 +24,8 @@
},

"require": {
"php": ">=5.6",
"php": "~7.0",

"psr/cache": "^1.0"
},

Expand Down
196 changes: 196 additions & 0 deletions src/Adapter/MemoryCacheItemPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php
namespace Psr6NullCache\Adapter;

use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheItemInterface;
use DateTime;
use Psr6NullCache\CacheItem;

final class MemoryCacheItemPool implements CacheItemPoolInterface
{

/**
*
* @var array
*/
private $data = [];

private $deferred = [];

/**
* Returns a Cache Item representing the specified key.
*
* This method must always return a CacheItemInterface object, even in case of
* a cache miss. It MUST NOT return null.
*
* @param string $key
* The key for which to return the corresponding Cache Item.
*
* @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
* MUST be thrown.
*
* @return CacheItemInterface
*/
public function getItem($key)
{
if ($this->hasItem($key) !== true) {
$this->data[$key] = new CacheItem($key, null, false);
}

return $this->data[$key];
}

/**
* Returns a traversable set of cache items.
*
* @param array $keys
* An indexed array of keys of items to retrieve.
*
* @throws InvalidArgumentException If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
* MUST be thrown.
*
* @return array|\Traversable A traversable collection of Cache Items keyed by the cache keys of
* each item. A Cache item will be returned for each key, even if that
* key is not found. However, if no keys are specified then an empty
* traversable MUST be returned instead.
*/
public function getItems(array $keys = [])
{
$result = [];

foreach ($keys as $key) {
$result[$key] = $this->getItem($key);
}

return $result;
}

/**
* Confirms if the cache contains specified cache item.
*
* Note: This method MAY avoid retrieving the cached value for performance reasons.
* This could result in a race condition with CacheItemInterface::get(). To avoid
* such situation use CacheItemInterface::isHit() instead.
*
* @param string $key
* The key for which to check existence.
*
* @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
* MUST be thrown.
*
* @return bool True if item exists in the cache, false otherwise.
*/
public function hasItem($key)
{
if (isset($this->data[$key])) {

/* @var $item \Psr6NullCache\CacheItem */
$item = $this->data[$key];

if ($item->isHit() === true && ($item->getExpires() === null || $item->getExpires() > new DateTime())) {
return true;
}
}

return false;
}

/**
* Deletes all items in the pool.
*
* @return bool True if the pool was successfully cleared. False if there was an error.
*/
public function clear()
{
$this->data = [];

return true;
}

/**
* Removes the item from the pool.
*
* @param string $key
* The key for which to delete
*
* @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
* MUST be thrown.
*
* @return bool True if the item was successfully removed. False if there was an error.
*/
public function deleteItem($key)
{
unset($this->data[$key]);

return true;
}

/**
* Removes multiple items from the pool.
*
* @param array $keys
* An array of keys that should be removed from the pool.
*
* @throws InvalidArgumentException If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
* MUST be thrown.
*
* @return bool True if the items were successfully removed. False if there was an error.
*/
public function deleteItems(array $keys)
{
foreach ($keys as $key) {
$this->deleteItem($key);
}

return true;
}

/**
* Persists a cache item immediately.
*
* @param CacheItemInterface $item
* The cache item to save.
*
* @return bool True if the item was successfully persisted. False if there was an error.
*/
public function save(CacheItemInterface $item)
{
$item->setIsHit(true);

$this->data[$item->getKey()] = $item;

return true;
}

/**
* Sets a cache item to be persisted later.
*
* @param CacheItemInterface $item
* The cache item to save.
*
* @return bool False if the item could not be queued or if a commit was attempted and failed. True otherwise.
*/
public function saveDeferred(CacheItemInterface $item)
{
$this->deferred[$item->getKey()] = $item;

return true;
}

/**
* Persists any deferred cache items.
*
* @return bool True if all not-yet-saved items were successfully saved or there were none. False otherwise.
*/
public function commit()
{
foreach ($this->deferred as $item) {
/* @var $item \Psr6NullCache\CacheItem */
$this->save($item);
}

$this->deferred = [];

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
namespace Psr6NullCache;
namespace Psr6NullCache\Adapter;

use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheItemInterface;
use Psr6NullCache\CacheItem;

final class NullCacheItemPool implements CacheItemPoolInterface
{
Expand Down
53 changes: 50 additions & 3 deletions src/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace Psr6NullCache;

use Psr\Cache\CacheItemInterface;
use DateTimeInterface;
use DateInterval;
use DateTime;

final class CacheItem implements CacheItemInterface
{
Expand All @@ -24,11 +27,18 @@ final class CacheItem implements CacheItemInterface
*/
private $isHit;

public function __construct($key, $value, $isHit)
/**
*
* @var null DateTimeInterface
*/
private $expires;

public function __construct($key, $value, $isHit, DateTimeInterface $expires = null)
{
$this->key = $key;
$this->value = $value;
$this->isHit = (bool) $isHit;
$this->expires = $expires;
}

/**
Expand Down Expand Up @@ -57,9 +67,18 @@ public function getKey()
*/
public function get()
{
if ($this->isHit() !== true) {
return null;
}

return $this->value;
}

public function setIsHit($mode = true)
{
$this->isHit = $mode;
}

/**
* Confirms if the cache item lookup resulted in a cache hit.
*
Expand All @@ -73,6 +92,15 @@ public function isHit()
return $this->isHit;
}

/**
*
* @return DateTimeInterface null
*/
public function getExpires()
{
return $this->expires;
}

/**
* Sets the value represented by this cache item.
*
Expand All @@ -95,16 +123,22 @@ public function set($value)
/**
* Sets the expiration time for this cache item.
*
* @param \DateTimeInterface $expiration
* @param \DateTimeInterface $expires
* The point in time after which the item MUST be considered expired.
* If null is passed explicitly, a default value MAY be used. If none is set,
* the value should be stored permanently or for as long as the
* implementation allows.
*
* @return static The called object.
*/
public function expiresAt($expiration)
public function expiresAt($expires)
{
if ($expires instanceof DateTimeInterface) {
$this->expires = $expires;
} else {
$this->expires = null;
}

return $this;
}

Expand All @@ -122,6 +156,19 @@ public function expiresAt($expiration)
*/
public function expiresAfter($time)
{
if ($time instanceof DateInterval) {
$expires = new DateTime();
$expires->add($time);

$this->expires = $expires;
} elseif (is_numeric($time)) {
$expires = new DateTime('now +' . $time . ' seconds');

$this->expires = $expires;
} else {
$this->expires = null;
}

return $this;
}
}
Loading

0 comments on commit e6a348c

Please sign in to comment.