From b3444f999f80039ca141260a928ba0a6f6d09615 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 20 Jan 2016 08:53:34 +0100 Subject: [PATCH] Updated to reflect changed in adapter common an tagging --- README.md | 35 ++++++++++++++++++++-------- composer.json | 10 ++++---- src/FilesystemCachePool.php | 18 ++++++++++----- tests/CreatePoolTrait.php | 38 +++++++++++++++++++++++++++++++ tests/FilesystemCachePoolTest.php | 21 +++++++++++------ tests/IntegrationPoolTest.php | 24 ++----------------- tests/IntegrationTagTest.php | 24 ++----------------- 7 files changed, 99 insertions(+), 71 deletions(-) create mode 100644 tests/CreatePoolTrait.php diff --git a/README.md b/README.md index 1e8961a..102b5e6 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,28 @@ -# Filesystem PSR-6 adapter using Flysystem -[![Build Status](https://travis-ci.org/php-cache/filesystem-adapter.svg?branch=master)](https://travis-ci.org/php-cache/filesystem-adapter) [![codecov.io](https://codecov.io/github/php-cache/filesystem-adapter/coverage.svg?branch=master)](https://codecov.io/github/php-cache/filesystem-adapter?branch=master) +# Filesystem PSR-6 Cache pool +[![Latest Stable Version](https://poser.pugx.org/cache/filesystem-adapter/v/stable)](https://packagist.org/packages/cache/filesystem-adapter) [![codecov.io](https://codecov.io/github/php-cache/filesystem-adapter/coverage.svg?branch=master)](https://codecov.io/github/php-cache/filesystem-adapter?branch=master) [![Build Status](https://travis-ci.org/php-cache/filesystem-adapter.svg?branch=master)](https://travis-ci.org/php-cache/filesystem-adapter) [![Total Downloads](https://poser.pugx.org/cache/filesystem-adapter/downloads)](https://packagist.org/packages/cache/filesystem-adapter) [![Monthly Downloads](https://poser.pugx.org/cache/filesystem-adapter/d/monthly.png)](https://packagist.org/packages/cache/filesystem-adapter) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) -This is a implementation for the PSR-6 for a filesystem cache. This implementation supports tags. This adapter -requires [Flysystem](http://flysystem.thephpleague.com/). +This is a PSR-6 cache implementation for Filesystem. It is a part of the PHP Cache organisation. To read about +features like tagging and hierarchy support please read the shared documentation at [www.php-cache.com](http://www.php-cache.com). -| Feature | Supported | -| ------- | --------- | -| Flush everything | Yes -| Expiration time | Yes -| Support for tags | Yes +This implementation is using the excellent [Flysystem](http://flysystem.thephpleague.com/). + +### Install + +```bash +composer require cache/filesystem-adapter +``` + +### Configure + +To create an instance of `FilesystemCachePool` you need to configure a `Filesystem` and its adapter. + +```php +use League\Flysystem\Adapter\Local; +use League\Flysystem\Filesystem; +use Cache\Adapter\Filesystem\FilesystemCachePool; + +$filesystemAdapter = new Local(__DIR__.'/'); +$filesystem = new Filesystem($filesystemAdapter); + +$pool = new FilesystemCachePool($filesystem); +``` diff --git a/composer.json b/composer.json index 9685c8b..51996c8 100644 --- a/composer.json +++ b/composer.json @@ -28,19 +28,19 @@ "require": { "php": "^5.5|^7.0", - "psr/cache": "1.0.0", - "cache/adapter-common": "^0.1", - "cache/taggable-cache": "^0.2", + "psr/cache": "~1.0", + "cache/adapter-common": "^0.2", + "cache/taggable-cache": "^0.3", "league/flysystem": "^1.0" }, "require-dev": { "phpunit/phpunit": "^5.1|^4.0", - "cache/integration-tests": "dev-master" + "cache/integration-tests": "^0.7" }, "provide": { - "psr/cache-implementation": "1.0.0" + "psr/cache-implementation": "~1.0" }, "autoload": { diff --git a/src/FilesystemCachePool.php b/src/FilesystemCachePool.php index 315a766..65c1944 100644 --- a/src/FilesystemCachePool.php +++ b/src/FilesystemCachePool.php @@ -41,10 +41,15 @@ protected function fetchObjectFromCache($key) { $file = $this->getFilePath($key); if (!$this->filesystem->has($file)) { - return; + return [false, null]; } - return unserialize($this->filesystem->read($file)); + $data = unserialize($this->filesystem->read($file)); + if (time() > $data[0]) { + return [false, null]; + } + + return [true, $data[1]]; } protected function clearAllObjectsFromCache() @@ -71,19 +76,20 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl) $this->filesystem->delete($file); } - return $this->filesystem->write($file, serialize($item)); + return $this->filesystem->write($file, serialize([time() + $ttl, $item->get()])); } /** * @param string $key * - * @return string * @throws InvalidArgumentException + * + * @return string */ private function getFilePath($key) { - if (!preg_match('|^[a-zA-Z0-9_\.: ]+$|', $key)) { - throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.:].', $key)); + if (!preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) { + throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.! ].', $key)); } return sprintf('%s/%s', self::CACHE_PATH, $key); diff --git a/tests/CreatePoolTrait.php b/tests/CreatePoolTrait.php new file mode 100644 index 0000000..575ad35 --- /dev/null +++ b/tests/CreatePoolTrait.php @@ -0,0 +1,38 @@ +, Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Cache\Adapter\Filesystem\Tests; + +use Cache\Adapter\Filesystem\FilesystemCachePool; +use League\Flysystem\Adapter\Local; +use League\Flysystem\Filesystem; + +trait CreatePoolTrait +{ + /** + * @type Filesystem + */ + private $filesystem; + + public function createCachePool() + { + return new FilesystemCachePool($this->getFilesystem()); + } + + private function getFilesystem() + { + if ($this->filesystem === null) { + $this->filesystem = new Filesystem(new Local(__DIR__.'/')); + } + + return $this->filesystem; + } +} diff --git a/tests/FilesystemCachePoolTest.php b/tests/FilesystemCachePoolTest.php index eff9023..a3c77af 100644 --- a/tests/FilesystemCachePoolTest.php +++ b/tests/FilesystemCachePoolTest.php @@ -1,23 +1,30 @@ , Tobias Nyholm + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ -use Cache\Adapter\Filesystem\FilesystemCachePool; -use League\Flysystem\Filesystem; -use League\Flysystem\Adapter\Local; +namespace Cache\Adapter\Filesystem\Tests; /** * @author Tobias Nyholm */ class FilesystemCachePoolTest extends \PHPUnit_Framework_TestCase { + use CreatePoolTrait; + /** * @expectedException \Psr\Cache\InvalidArgumentException */ public function testInvalidKey() { - $pool = new FilesystemCachePool(new Filesystem(new Local(__DIR__.'/'))); + $pool = $this->createCachePool(); - $pool->getItem('test%string'); + $pool->getItem('test%string')->get(); } -} \ No newline at end of file +} diff --git a/tests/IntegrationPoolTest.php b/tests/IntegrationPoolTest.php index bd84f50..dd9586e 100644 --- a/tests/IntegrationPoolTest.php +++ b/tests/IntegrationPoolTest.php @@ -9,31 +9,11 @@ * with this source code in the file LICENSE. */ -namespace Cache\Adapter\Filesystem\tests; +namespace Cache\Adapter\Filesystem\Tests; -use Cache\Adapter\Filesystem\FilesystemCachePool; use Cache\IntegrationTests\CachePoolTest; -use League\Flysystem\Adapter\Local; -use League\Flysystem\Filesystem; class IntegrationPoolTest extends CachePoolTest { - /** - * @type Filesystem - */ - private $filesystem; - - public function createCachePool() - { - return new FilesystemCachePool($this->getFilesystem()); - } - - private function getFilesystem() - { - if ($this->filesystem === null) { - $this->filesystem = new Filesystem(new Local(__DIR__.'/')); - } - - return $this->filesystem; - } + use CreatePoolTrait; } diff --git a/tests/IntegrationTagTest.php b/tests/IntegrationTagTest.php index f5368e4..fe9a483 100644 --- a/tests/IntegrationTagTest.php +++ b/tests/IntegrationTagTest.php @@ -9,31 +9,11 @@ * with this source code in the file LICENSE. */ -namespace Cache\Adapter\Filesystem\tests; +namespace Cache\Adapter\Filesystem\Tests; -use Cache\Adapter\Filesystem\FilesystemCachePool; use Cache\IntegrationTests\TaggableCachePoolTest; -use League\Flysystem\Adapter\Local; -use League\Flysystem\Filesystem; class IntegrationTagTest extends TaggableCachePoolTest { - /** - * @type Filesystem - */ - private $filesystem; - - public function createCachePool() - { - return new FilesystemCachePool($this->getFilesystem()); - } - - private function getFilesystem() - { - if ($this->filesystem === null) { - $this->filesystem = new Filesystem(new Local(__DIR__.'/')); - } - - return $this->filesystem; - } + use CreatePoolTrait; }