Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ A flexible memoization library for memory caching.
- [Using with a dependency injection container](#using-with-a-dependency-injection-container)
- [Drivers](#drivers)
- [MemoryDriver](#memorydriver)
- [Traits](#traits)

## Memoization

Expand Down
14 changes: 14 additions & 0 deletions src/Memoize/Contracts/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace StellarWP\Memoize\Contracts;

use InvalidArgumentException;

interface DriverInterface
{
/**
* Get a value from the cache.
*
* @param ?string $key The cache key using dot notation. If null, the entire cache will be returned.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return mixed
*/
public function get(?string $key = null);
Expand All @@ -19,6 +24,9 @@ public function get(?string $key = null);
*
* @param string $key The cache key using dot notation.
* @param mixed $value The value to store in the cache.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return void
*/
public function set(string $key, $value): void;
Expand All @@ -27,6 +35,9 @@ public function set(string $key, $value): void;
* Check if a key exists in the cache.
*
* @param string $key The cache key using dot notation.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return boolean
*/
public function has(string $key): bool;
Expand All @@ -35,6 +46,9 @@ public function has(string $key): bool;
* Remove a key from the cache.
*
* @param ?string $key The cache key using dot notation. If null, the entire cache will be cleared.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return void
*/
public function forget(?string $key = null): void;
Expand Down
14 changes: 14 additions & 0 deletions src/Memoize/Contracts/MemoizerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace StellarWP\Memoize\Contracts;

use InvalidArgumentException;

interface MemoizerInterface
{
/**
* Get a value from the memoization cache.
*
* @param ?string $key The cache key using dot notation. If null, the entire cache will be returned.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return mixed
*/
public function get(?string $key = null);
Expand All @@ -19,6 +24,9 @@ public function get(?string $key = null);
*
* @param string $key The cache key using dot notation.
* @param mixed $value The value to store in the cache.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return void
*/
public function set(string $key, $value): void;
Expand All @@ -27,6 +35,9 @@ public function set(string $key, $value): void;
* Check if a key exists in the memoization cache.
*
* @param string $key The cache key using dot notation.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return bool
*/
public function has(string $key): bool;
Expand All @@ -35,6 +46,9 @@ public function has(string $key): bool;
* Remove a key from the memoization cache.
*
* @param ?string $key The cache key using dot notation. If null, the entire cache will be cleared.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return void
*/
public function forget(?string $key = null): void;
Expand Down
13 changes: 13 additions & 0 deletions src/Memoize/Memoizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace StellarWP\Memoize;

use InvalidArgumentException;
use StellarWP\Memoize\Contracts\DriverInterface;
use StellarWP\Memoize\Contracts\MemoizerInterface;
use StellarWP\Memoize\Drivers\MemoryDriver;
Expand Down Expand Up @@ -37,6 +38,9 @@ public function __construct(DriverInterface $driver = null)
* Get a value from the memoization cache.
*
* @param ?string $key The cache key using dot notation. If null, the entire cache will be returned.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return mixed
*/
public function get(?string $key = null)
Expand All @@ -49,6 +53,9 @@ public function get(?string $key = null)
*
* @param string $key The cache key using dot notation.
* @param mixed $value The value to store in the cache.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return void
*/
public function set(string $key, $value): void
Expand All @@ -60,6 +67,9 @@ public function set(string $key, $value): void
* Check if a key exists in the memoization cache.
*
* @param string $key The cache key using dot notation.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return boolean
*/
public function has(string $key): bool
Expand All @@ -71,6 +81,9 @@ public function has(string $key): bool
* Remove a key from the memoization cache.
*
* @param ?string $key The cache key using dot notation. If null, the entire cache will be cleared.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return void
*/
public function forget(?string $key = null): void
Expand Down
29 changes: 27 additions & 2 deletions src/Memoize/Traits/MemoizeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace StellarWP\Memoize\Traits;

use Closure;
use InvalidArgumentException;
use StellarWP\Arrays\Arr;
use StellarWP\Memoize\Contracts\DriverInterface;
use StellarWP\Memoize\Contracts\MemoizerInterface;
Expand All @@ -27,14 +28,20 @@ trait MemoizeTrait
*
* @param ?string $key The cache key using dot notation. If null, the entire cache will be returned.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return mixed
*/
public function get(?string $key = null)
{
if (!$key) {
if ($key === null) {
return static::$cached;
}

if ($key === '') {
throw new InvalidArgumentException('Memoize key cannot be an empty string');
}

return Arr::get(static::$cached, $key);
}

Expand All @@ -44,10 +51,16 @@ public function get(?string $key = null)
* @param string $key The cache key using dot notation.
* @param mixed $value The value to store in the cache.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return void
*/
public function set(string $key, $value): void
{
if ($key === '') {
throw new InvalidArgumentException('Memoize key cannot be an empty string');
}

if ($value instanceof Closure) {
$value = $value();
}
Expand All @@ -60,10 +73,16 @@ public function set(string $key, $value): void
*
* @param string $key The cache key using dot notation.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return boolean
*/
public function has(string $key): bool
{
if ($key === '') {
throw new InvalidArgumentException('Memoize key cannot be an empty string');
}

return Arr::has(static::$cached, $key);
}

Expand All @@ -72,11 +91,17 @@ public function has(string $key): bool
*
* @param ?string $key The cache key using dot notation. If null, the entire cache will be cleared.
*
* @throws InvalidArgumentException If the key is an empty string.
*
* @return void
*/
public function forget(?string $key = null): void
{
if ($key) {
if ($key === '') {
throw new InvalidArgumentException('Memoize key cannot be an empty string');
}

if ($key !== null) {
Arr::forget(static::$cached, $key);
} else {
static::$cached = [];
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/MemoizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace StellarWP\Memoize\Tests\Unit;

use InvalidArgumentException;
use StellarWP\Memoize\Contracts\MemoizerInterface;
use StellarWP\Memoize\Drivers\MemoryDriver;
use StellarWP\Memoize\Memoizer;
Expand Down Expand Up @@ -96,4 +97,63 @@ public function testForgetsSimpleValues(MemoizerInterface $memoizer): void
$this->assertFalse($memoizer->has('foo'));
$this->assertTrue($memoizer->has('bork'));
}

/**
* @dataProvider driverProvider
*/
public function testItAllowsEmptyNonNullValues(MemoizerInterface $memoizer): void
{
$memoizer->set('0', 'baz');
$memoizer->set('false', 'lol');
$this->assertTrue($memoizer->has('0'));
$this->assertTrue($memoizer->has('false'));
$this->assertSame('baz', $memoizer->get('0'));
$this->assertSame('lol', $memoizer->get('false'));

$memoizer->forget('0');
$memoizer->forget('false');
$this->assertFalse($memoizer->has('0'));
$this->assertFalse($memoizer->has('false'));
}

/**
* @dataProvider driverProvider
*/
public function testItThrowsExceptionSettingEmptyString(MemoizerInterface $memoizer): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Memoize key cannot be an empty string');
$memoizer->set('', 'baz');
}

/**
* @dataProvider driverProvider
*/
public function testItThrowsExceptionGettingEmptyString(MemoizerInterface $memoizer): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Memoize key cannot be an empty string');
$memoizer->get('');
}

/**
* @dataProvider driverProvider
*/
public function testItThrowsExceptionHasEmptyString(MemoizerInterface $memoizer): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Memoize key cannot be an empty string');
$memoizer->has('');
}

/**
* @dataProvider driverProvider
*/
public function testItThrowsExceptionForgettingEmptyString(MemoizerInterface $memoizer): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Memoize key cannot be an empty string');
$memoizer->forget('');
}

}
Loading