Skip to content

Commit

Permalink
Merge pull request #4 from php-cache/updates
Browse files Browse the repository at this point in the history
Updated to reflect changed in adapter common an tagging
  • Loading branch information
cryptiklemur committed Jan 20, 2016
2 parents 1985f7a + b3444f9 commit a156a53
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 71 deletions.
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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);
```
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":
{
Expand Down
18 changes: 12 additions & 6 deletions src/FilesystemCachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions tests/CreatePoolTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of php-cache\filesystem-adapter package.
*
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* 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;
}
}
21 changes: 14 additions & 7 deletions tests/FilesystemCachePoolTest.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
<?php

namespace Cache\Adapter\Filesystem\tests;
/*
* This file is part of php-cache\filesystem-adapter package.
*
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
*
* 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 <tobias.nyholm@gmail.com>
*/
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();
}
}
}
24 changes: 2 additions & 22 deletions tests/IntegrationPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
24 changes: 2 additions & 22 deletions tests/IntegrationTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit a156a53

Please sign in to comment.