Skip to content

Commit

Permalink
Merge 98aaa45 into 46c83b6
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbowers committed Apr 4, 2015
2 parents 46c83b6 + 98aaa45 commit 1db3f48
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/Stash/Interfaces/ItemInterface.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public function setLogger($logger)
{
return false;
}

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

public function getMode()
{
return false;
}
}

0 comments on commit 1db3f48

Please sign in to comment.