Skip to content

Commit

Permalink
Expirable interface to mark items can expire (e.g. by TTL)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-mabe committed Jul 6, 2012
1 parent bf1076e commit 8645bf7
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 22 deletions.
1 change: 0 additions & 1 deletion library/Zend/Cache/Storage/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public function __construct($options = null)
* detach all registered plugins to free
* event handles of event manager
*
*
* @return void
*/
public function __destruct()
Expand Down
6 changes: 1 addition & 5 deletions library/Zend/Cache/Storage/Adapter/Apc.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function setOptions($options)
* Get options.
*
* @return ApcOptions
* @see setOptions()
* @see setOptions()
*/
public function getOptions()
{
Expand Down Expand Up @@ -308,10 +308,6 @@ protected function internalHasItems(array & $normalizedKeys)
* @param string $normalizedKey
* @return array|boolean Metadata on success, false on failure
* @throws Exception\ExceptionInterface
*
* @triggers getMetadata.pre(PreEvent)
* @triggers getMetadata.post(PostEvent)
* @triggers getMetadata.exception(ExceptionEvent)
*/
protected function internalGetMetadata(& $normalizedKey)
{
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Cache/Storage/Adapter/ApcIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(Apc $storage, BaseApcIterator $baseIterator, $prefix
/**
* Get storage instance
*
* @return StorageInterface
* @return Apc
*/
public function getStorage()
{
Expand Down
5 changes: 3 additions & 2 deletions library/Zend/Cache/Storage/Adapter/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public function getIterator()
/**
* Optimize the storage
*
* @return void
* @return boolean
* @return Exception\RuntimeException
*/
public function optimize()
Expand Down Expand Up @@ -421,8 +421,9 @@ public function getTotalSpace()
$events->detach($handle);
}
};
$handle = $this->getEventManager()->attach($callback);
$handle = $events->attach($callback);
}

return $this->totalSpace;
}

Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Cache/Storage/Adapter/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ protected function internalGetCapabilities()
'object' => true,
'resource' => true,
),
'supportedMetadata' => array('mtime'),
'supportedMetadata' => array('mtime'),
'maxTtl' => PHP_INT_MAX,
'staticTtl' => false,
'ttlPrecision' => 0.05,
Expand Down
55 changes: 50 additions & 5 deletions library/Zend/Cache/Storage/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,56 +58,101 @@ class Capabilities

/**
* Expire read
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|boolean
*/
protected $_expiredRead;

/**
* Max key length
* Max. key length
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|int
*/
protected $_maxKeyLength;

/**
* Max ttl
* Max. TTL (0 means infinite)
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|int
*/
protected $_maxTtl;

/**
* Namespace is prefix
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|boolean
*/
protected $_namespaceIsPrefix;

/**
* Namespace separator
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|string
*/
protected $_namespaceSeparator;

/**
* Static ttl
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|boolean
*/
protected $_staticTtl;

/**
* Capability property
* Supported datatypes
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|mixed
* @var null|array
*/
protected $_supportedDatatypes;

/**
* Supported metdata
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|array
*/
protected $_supportedMetadata;

/**
* Ttl precision
* TTL precision
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|int
*/
protected $_ttlPrecision;

/**
* Use request time
*
* If it's NULL the capability isn't set and the getter
* returns the base capability or the default value.
*
* @var null|boolean
*/
protected $_useRequestTime;

Expand Down
24 changes: 24 additions & 0 deletions library/Zend/Cache/Storage/ExpirableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Zend\Cache\Storage;

interface ExpirableInterface
{
/**
* Reset lifetime of an item
*
* @param string $key
* @return boolean
* @throws \Zend\Cache\Exception\ExceptionInterface
*/
public function touchItem($key);

/**
* Reset lifetime of multiple items.
*
* @param array $keys
* @return array Array of not updated keys
* @throws \Zend\Cache\Exception\ExceptionInterface
*/
public function touchItems(array $keys);
}
31 changes: 24 additions & 7 deletions tests/Zend/Cache/Storage/Adapter/CommonAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@

namespace ZendTest\Cache\Storage\Adapter;

use Zend\Http\Header\Expires;

use Zend\Cache\Storage\IterableInterface,
Zend\Cache\Storage\IteratorInterface,
Zend\Cache\Storage\StorageInterface,
Zend\Cache\Storage\ClearExpiredInterface,
Zend\Cache\Storage\ClearByNamespaceInterface,
Zend\Cache\Storage\ClearByPrefixInterface,
Zend\Cache\Storage\ExpirableInterface,
Zend\Cache\Storage\FlushableInterface,
Zend\Cache\Storage\OptimizableInterface,
Zend\Cache\Storage\TagableInterface,
Expand Down Expand Up @@ -232,8 +235,11 @@ public function testHasItemReturnsFalseOnMissingItem()

public function testHasItemReturnsFalseOnExpiredItem()
{
$capabilities = $this->_storage->getCapabilities();
if (!$this->_storage instanceof ExpirableInterface) {
$this->markTestSkipped("Adapter doesn't support TTL expiration");
}

$capabilities = $this->_storage->getCapabilities();
$ttl = $capabilities->getTtlPrecision();
$this->_options->setTtl($ttl);

Expand Down Expand Up @@ -295,6 +301,10 @@ public function testGetItemSetsSuccessFlag()

public function testGetItemReturnsNullOnExpiredItem()
{
if (!$this->_storage instanceof ExpirableInterface) {
$this->markTestSkipped("Adapter doesn't support TTL expiration");
}

$capabilities = $this->_storage->getCapabilities();
if ($capabilities->getUseRequestTime()) {
$this->markTestSkipped("Can't test get expired item if request time will be used");
Expand Down Expand Up @@ -405,6 +415,7 @@ public function testSetGetHasAndRemoveItem()
$this->assertTrue($this->_storage->setItem('key', 'value'));
$this->assertEquals('value', $this->_storage->getItem('key'));
$this->assertTrue($this->_storage->hasItem('key'));

$this->assertTrue($this->_storage->removeItem('key'));
$this->assertFalse($this->_storage->hasItem('key'));
$this->assertNull($this->_storage->getItem('key'));
Expand Down Expand Up @@ -532,8 +543,11 @@ public function testSetGetHasAndRemoveItemsWithNamespace()

public function testSetAndGetExpiredItem()
{
$capabilities = $this->_storage->getCapabilities();
if (!$this->_storage instanceof ExpirableInterface) {
$this->markTestSkipped("Adapter doesn't support TTL expiration");
}

$capabilities = $this->_storage->getCapabilities();
$ttl = $capabilities->getTtlPrecision();
$this->_options->setTtl($ttl);

Expand All @@ -559,8 +573,11 @@ public function testSetAndGetExpiredItem()

public function testSetAndGetExpiredItems()
{
$capabilities = $this->_storage->getCapabilities();
if (!$this->_storage instanceof ExpirableInterface) {
$this->markTestSkipped("Adapter doesn't support TTL expiration");
}

$capabilities = $this->_storage->getCapabilities();
$ttl = $capabilities->getTtlPrecision();
$this->_options->setTtl($ttl);

Expand Down Expand Up @@ -808,6 +825,10 @@ public function testDecrementItemsReturnsEmptyArrayIfNonWritable()

public function testTouchItem()
{
if (!$this->_storage instanceof ExpirableInterface) {
$this->markTestSkipped("Adapter doesn't support TTL expiration");
}

$capabilities = $this->_storage->getCapabilities();
$this->_options->setTtl(2 * $capabilities->getTtlPrecision());

Expand Down Expand Up @@ -988,10 +1009,6 @@ public function testTagable()
$this->markTestSkipped("Storage doesn't implement TagableInterface");
}

$capabilities = $this->_storage->getCapabilities();
$ttl = $capabilities->getTtlPrecision();
$this->_options->setTtl($ttl);

$this->assertSame(array(), $this->_storage->setItems(array(
'key1' => 'value1',
'key2' => 'value2',
Expand Down

0 comments on commit 8645bf7

Please sign in to comment.