Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/set mode #227

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Stash/Interfaces/ItemInterface.php
Expand Up @@ -32,6 +32,13 @@ public function setPool(PoolInterface $driver);
*/
public function setKey($key, $namespace = null);

/**
* Sets the pool mode
*
* @param $mode
*/
public function setMode($mode);

/**
* This disables any IO operations by this object, effectively preventing
* the reading and writing of new data.
Expand Down
19 changes: 17 additions & 2 deletions src/Stash/Interfaces/PoolInterface.php
Expand Up @@ -24,12 +24,27 @@ interface PoolInterface
*
* Using this function developers can have the pool class generate custom Item objects.
*
* @param string $class
* @param string $class
* @return bool
* @throws \InvalidArgumentException When passed invalid or nonexistant classes.
* @throws \InvalidArgumentException When passed invalid or nonexistent classes.
*/
public function setItemClass($class);

/**
* Changes the mode of the pool
*
* Using this function developers can determine how Stash handles the cache
* @param $mode
* @return bool
*/
public function setMode($mode);

/**
* Returns the mode of the pool
* @return int
*/
public function getMode();

/**
* Returns an initialized Item for a given Key.
*
Expand Down
32 changes: 30 additions & 2 deletions src/Stash/Item.php
Expand Up @@ -51,6 +51,13 @@ class Item implements ItemInterface
*/
const SP_PRECOMPUTE = 4;

/**
* The pool's mode
*
* @var int
*/
private $mode;

/**
* This is the default time, in seconds, that objects are cached for.
*
Expand Down Expand Up @@ -211,7 +218,11 @@ private function executeClear()
public function get($invalidation = 0, $arg = null, $arg2 = null)
{
try {
return $this->executeGet($invalidation, $arg, $arg2);
if ($this->mode == Mode::WRITE_ONLY) {
return null;
} else {
return $this->executeGet($invalidation, $arg, $arg2);
}
} catch (Exception $e) {
$this->logException('Retrieving from cache caused exception.', $e);
$this->disable();
Expand Down Expand Up @@ -262,6 +273,10 @@ private function executeGet($invalidation, $arg, $arg2)
*/
public function isMiss()
{
if ($this->mode == Mode::FORCE_MISS) {
return true;
}

if (!isset($this->isHit)) {
$this->get();
}
Expand Down Expand Up @@ -303,7 +318,11 @@ public function lock($ttl = null)
public function set($data, $ttl = null)
{
try {
return $this->executeSet($data, $ttl);
if ($this->mode == Mode::READ_ONLY) {
return false;
} else {
return $this->executeSet($data, $ttl);
}
} catch (Exception $e) {
$this->logException('Setting value in cache caused exception.', $e);
$this->disable();
Expand Down Expand Up @@ -373,6 +392,7 @@ public function isDisabled()
{
return self::$runtimeDisable
|| !$this->cacheEnabled
|| $this->mode == Mode::DISABLED
|| (defined('STASH_DISABLE_CACHE') && STASH_DISABLE_CACHE);
}

Expand Down Expand Up @@ -616,4 +636,12 @@ protected function setupKey($key)
array_unshift($key, 'cache');
$this->key = array_map('strtolower', $key);
}

/**
* {@inheritdoc}
*/
public function setMode($mode)
{
$this->mode = $mode;
}
}
43 changes: 43 additions & 0 deletions src/Stash/Mode.php
@@ -0,0 +1,43 @@
<?php namespace Stash;

class Mode
{
/**
* Normal Mode
*
* Default value
*/
const NORMAL = 0;

/**
* Read only mode
*
* Set() has no effect
*/
const READ_ONLY = 1;

/**
* Write only mode
*
* Get() has no effect
*/
const WRITE_ONLY = 2;

/**
* Disabled mode
*
* Get() and Set() has no effect.
*
* Get() will return null
*/
const DISABLED = 3;

/**
* Forced miss mode
*
* Get() and Set() work normally.
*
* isMiss() will always return true
*/
const FORCE_MISS = 4;
}
34 changes: 33 additions & 1 deletion src/Stash/Pool.php
Expand Up @@ -46,6 +46,14 @@ class Pool implements PoolInterface
*/
protected $itemClass = '\Stash\Item';


/**
* Default "mode" for the pool to use.
*
* @var int
*/
protected $mode = Mode::NORMAL;

/**
* If set various then errors and exceptions will get passed to the PSR Compliant logging library. This
* can be set using the setLogger() function in this class.
Expand All @@ -67,14 +75,19 @@ class Pool implements PoolInterface
* default.
*
* @param DriverInterface $driver
* @param Mode $mode
*/
public function __construct(DriverInterface $driver = null)
public function __construct(DriverInterface $driver = null, $mode = null)
{
if (isset($driver)) {
$this->setDriver($driver);
} else {
$this->driver = new Ephemeral();
}

if (isset($mode)) {
$this->setMode($mode);
}
}

/**
Expand Down Expand Up @@ -138,6 +151,7 @@ public function getItem()
$item = new $this->itemClass();
$item->setPool($this);
$item->setKey($key, $namespace);
$item->setMode($this->mode);

if ($this->isDisabled) {
$item->disable();
Expand Down Expand Up @@ -290,4 +304,22 @@ protected function logException($message, $exception)

return true;
}

/**
* {@inheritdoc}
*/
public function setMode($mode)
{
$this->mode = $mode;

return true;
}

/**
* {@inheritdoc}
*/
public function getMode()
{
return $this->mode;
}
}
16 changes: 16 additions & 0 deletions tests/Stash/Test/AbstractPoolTest.php
Expand Up @@ -11,6 +11,7 @@

namespace Stash\Test;

use Stash\Mode;
use Stash\Pool;
use Stash\Driver\Ephemeral;
use Stash\Test\Stubs\LoggerStub;
Expand Down Expand Up @@ -68,6 +69,21 @@ public function testGetItem()
$this->assertAttributeEquals('TestNamespace', 'namespace', $item, 'Pool sets Item namespace.');
}

public function testModeGetterSetters()
{
$pool = $this->getTestPool();

$mode_set = Mode::DISABLED;

$mode = $pool->setMode($mode_set);

$this->assertTrue($mode, 'Mode should return true upon being set.');

$mode_returned = $pool->getMode();

$this->assertTrue($mode_set === $mode_returned, 'The returned mode is not the same as the set mode');
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Item constructor requires a key.
Expand Down
10 changes: 10 additions & 0 deletions tests/Stash/Test/Stubs/PoolGetDriverStub.php
Expand Up @@ -72,4 +72,14 @@ public function setLogger($logger)
{
return false;
}

public function setMode($mode)
{
return false;
}

public function getMode()
{
return false;
}
}