-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jordan Hall
committed
Jan 4, 2018
1 parent
0a56409
commit cd3ba48
Showing
8 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "rapidwebltd/rw-file-cache-psr-6", | ||
"description": "PSR-6 adapter for RW File Cache", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Jordan Hall", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"license": "LGPL3", | ||
"require": { | ||
"rapidwebltd/rw-file-cache": "^1.1", | ||
"psr/cache": "^1.0" | ||
}, | ||
"require-dev": { | ||
"cache/integration-tests": "^0.16.0", | ||
"phpunit/phpunit": "^5.7" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"rapidweb\\RWFileCachePSR6\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Unit Tests"> | ||
<directory suffix="Test.php">./tests/Unit</directory> | ||
</testsuite> | ||
<testsuite name="Functional Tests"> | ||
<directory suffix="Test.php">./tests/Functional</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
|
||
</filter> | ||
<php> | ||
|
||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace rapidweb\RWFileCachePSR6; | ||
|
||
use Psr\Cache\InvalidArgumentException; | ||
use Exception; | ||
|
||
class CacheInvalidArgumentException extends Exception implements InvalidArgumentException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace rapidweb\RWFileCachePSR6; | ||
|
||
use Psr\Cache\CacheItemInterface; | ||
|
||
class CacheItem implements CacheItemInterface | ||
{ | ||
private $key; | ||
private $value; | ||
private $expires = 0; | ||
public $isDeferred = false; | ||
|
||
public function __construct($key, $value) | ||
{ | ||
$this->key = $key; | ||
$this->value = $value; | ||
} | ||
|
||
public function getKey() | ||
{ | ||
return $this->key; | ||
} | ||
|
||
public function get() | ||
{ | ||
if ($this->isHit()===false) { | ||
return null; | ||
} | ||
|
||
return $this->value; | ||
} | ||
|
||
public function getExpires() | ||
{ | ||
return $this->expires; | ||
} | ||
|
||
public function isHit() | ||
{ | ||
return $this->value !== false; | ||
} | ||
|
||
public function set($value) | ||
{ | ||
if ($this->isDeferred) { | ||
return; | ||
} | ||
|
||
$this->value = $value; | ||
return $this; | ||
} | ||
|
||
public function expiresAt($expiration) | ||
{ | ||
return $this; | ||
} | ||
|
||
public function expiresAfter($time) | ||
{ | ||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace rapidweb\RWFileCachePSR6; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use rapidweb\RWFileCache\RWFileCache; | ||
use Psr\Cache\CacheItemInterface; | ||
|
||
class CacheItemPool implements CacheItemPoolInterface | ||
{ | ||
private $rwFileCache; | ||
private $deferredItems = []; | ||
|
||
public function __construct() | ||
{ | ||
$this->rwFileCache = new RWFileCache(); | ||
} | ||
|
||
public function changeConfig(array $config) | ||
{ | ||
return $this->rwFileCache->changeConfig($config); | ||
} | ||
|
||
private function sanityCheckKey($key) | ||
{ | ||
if (!is_string($key)) { | ||
throw new CacheInvalidArgumentException; | ||
} | ||
|
||
$invalidChars = ['{', '}', '(', ')', '/', '\\', '@', ':']; | ||
|
||
foreach($invalidChars as $invalidChar) { | ||
if (stripos($key, $invalidChar)!==false) { | ||
throw new CacheInvalidArgumentException; | ||
} | ||
} | ||
|
||
} | ||
|
||
public function getItem($key) | ||
{ | ||
$this->sanityCheckKey($key); | ||
|
||
if (array_key_exists($key, $this->deferredItems)) { | ||
return $this->deferredItems[$key]; | ||
} | ||
|
||
return new CacheItem($key, $this->rwFileCache->get($key)); | ||
} | ||
|
||
public function getItems(array $keys = []) | ||
{ | ||
$results = []; | ||
|
||
foreach($keys as $key) { | ||
$results[$key] = $this->getItem($key); | ||
} | ||
|
||
return $results; | ||
} | ||
|
||
public function hasItem($key) | ||
{ | ||
return $this->getItem($key)->isHit(); | ||
} | ||
|
||
public function clear() | ||
{ | ||
$this->deferredItems = []; | ||
return $this->rwFileCache->flush(); | ||
} | ||
|
||
public function deleteItem($key) | ||
{ | ||
$this->sanityCheckKey($key); | ||
|
||
if (array_key_exists($key, $this->deferredItems)) { | ||
unset($this->deferredItems[$key]); | ||
return true; | ||
} | ||
|
||
$this->rwFileCache->delete($key); | ||
|
||
return true; | ||
} | ||
|
||
public function deleteItems(array $keys) | ||
{ | ||
|
||
|
||
foreach($keys as $key) { | ||
$this->deleteItem($key); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function save(CacheItemInterface $item) | ||
{ | ||
return $this->rwFileCache->set($item->getKey(), $item->get(), $item->getExpires()); | ||
} | ||
|
||
public function saveDeferred(CacheItemInterface $item) | ||
{ | ||
$item->isDeferred = true; | ||
$this->deferredItems[$item->getKey()] = $item; | ||
return true; | ||
} | ||
|
||
public function commit() | ||
{ | ||
foreach($this->deferredItems as $item) { | ||
$this->save($item); | ||
} | ||
$this->deferredItems = []; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
use Cache\IntegrationTests\CachePoolTest; | ||
use rapidweb\RWFileCachePSR6\CacheItemPool; | ||
|
||
class PoolIntegrationTest extends CachePoolTest | ||
{ | ||
public function createCachePool() | ||
{ | ||
$cacheItemPool = new CacheItemPool(); | ||
$cacheItemPool->changeConfig(['cacheDirectory' => __DIR__.'/Data/']); | ||
return $cacheItemPool; | ||
} | ||
} |
Empty file.