Skip to content

Commit a156a53

Browse files
committed
Merge pull request #4 from php-cache/updates
Updated to reflect changed in adapter common an tagging
2 parents 1985f7a + b3444f9 commit a156a53

7 files changed

+99
-71
lines changed

README.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
# Filesystem PSR-6 adapter using Flysystem
2-
[![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)
1+
# Filesystem PSR-6 Cache pool
2+
[![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)
33

4-
This is a implementation for the PSR-6 for a filesystem cache. This implementation supports tags. This adapter
5-
requires [Flysystem](http://flysystem.thephpleague.com/).
4+
This is a PSR-6 cache implementation for Filesystem. It is a part of the PHP Cache organisation. To read about
5+
features like tagging and hierarchy support please read the shared documentation at [www.php-cache.com](http://www.php-cache.com).
66

7-
| Feature | Supported |
8-
| ------- | --------- |
9-
| Flush everything | Yes
10-
| Expiration time | Yes
11-
| Support for tags | Yes
7+
This implementation is using the excellent [Flysystem](http://flysystem.thephpleague.com/).
8+
9+
### Install
10+
11+
```bash
12+
composer require cache/filesystem-adapter
13+
```
14+
15+
### Configure
16+
17+
To create an instance of `FilesystemCachePool` you need to configure a `Filesystem` and its adapter.
18+
19+
```php
20+
use League\Flysystem\Adapter\Local;
21+
use League\Flysystem\Filesystem;
22+
use Cache\Adapter\Filesystem\FilesystemCachePool;
23+
24+
$filesystemAdapter = new Local(__DIR__.'/');
25+
$filesystem = new Filesystem($filesystemAdapter);
26+
27+
$pool = new FilesystemCachePool($filesystem);
28+
```

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@
2828
"require":
2929
{
3030
"php": "^5.5|^7.0",
31-
"psr/cache": "1.0.0",
32-
"cache/adapter-common": "^0.1",
33-
"cache/taggable-cache": "^0.2",
31+
"psr/cache": "~1.0",
32+
"cache/adapter-common": "^0.2",
33+
"cache/taggable-cache": "^0.3",
3434
"league/flysystem": "^1.0"
3535
},
3636
"require-dev":
3737
{
3838
"phpunit/phpunit": "^5.1|^4.0",
39-
"cache/integration-tests": "dev-master"
39+
"cache/integration-tests": "^0.7"
4040
},
4141
"provide":
4242
{
43-
"psr/cache-implementation": "1.0.0"
43+
"psr/cache-implementation": "~1.0"
4444
},
4545
"autoload":
4646
{

src/FilesystemCachePool.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ protected function fetchObjectFromCache($key)
4141
{
4242
$file = $this->getFilePath($key);
4343
if (!$this->filesystem->has($file)) {
44-
return;
44+
return [false, null];
4545
}
4646

47-
return unserialize($this->filesystem->read($file));
47+
$data = unserialize($this->filesystem->read($file));
48+
if (time() > $data[0]) {
49+
return [false, null];
50+
}
51+
52+
return [true, $data[1]];
4853
}
4954

5055
protected function clearAllObjectsFromCache()
@@ -71,19 +76,20 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
7176
$this->filesystem->delete($file);
7277
}
7378

74-
return $this->filesystem->write($file, serialize($item));
79+
return $this->filesystem->write($file, serialize([time() + $ttl, $item->get()]));
7580
}
7681

7782
/**
7883
* @param string $key
7984
*
80-
* @return string
8185
* @throws InvalidArgumentException
86+
*
87+
* @return string
8288
*/
8389
private function getFilePath($key)
8490
{
85-
if (!preg_match('|^[a-zA-Z0-9_\.: ]+$|', $key)) {
86-
throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.:].', $key));
91+
if (!preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) {
92+
throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.! ].', $key));
8793
}
8894

8995
return sprintf('%s/%s', self::CACHE_PATH, $key);

tests/CreatePoolTrait.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\filesystem-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Filesystem\Tests;
13+
14+
use Cache\Adapter\Filesystem\FilesystemCachePool;
15+
use League\Flysystem\Adapter\Local;
16+
use League\Flysystem\Filesystem;
17+
18+
trait CreatePoolTrait
19+
{
20+
/**
21+
* @type Filesystem
22+
*/
23+
private $filesystem;
24+
25+
public function createCachePool()
26+
{
27+
return new FilesystemCachePool($this->getFilesystem());
28+
}
29+
30+
private function getFilesystem()
31+
{
32+
if ($this->filesystem === null) {
33+
$this->filesystem = new Filesystem(new Local(__DIR__.'/'));
34+
}
35+
36+
return $this->filesystem;
37+
}
38+
}

tests/FilesystemCachePoolTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
<?php
22

3-
namespace Cache\Adapter\Filesystem\tests;
3+
/*
4+
* This file is part of php-cache\filesystem-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
411

5-
use Cache\Adapter\Filesystem\FilesystemCachePool;
6-
use League\Flysystem\Filesystem;
7-
use League\Flysystem\Adapter\Local;
12+
namespace Cache\Adapter\Filesystem\Tests;
813

914
/**
1015
* @author Tobias Nyholm <[email protected]>
1116
*/
1217
class FilesystemCachePoolTest extends \PHPUnit_Framework_TestCase
1318
{
19+
use CreatePoolTrait;
20+
1421
/**
1522
* @expectedException \Psr\Cache\InvalidArgumentException
1623
*/
1724
public function testInvalidKey()
1825
{
19-
$pool = new FilesystemCachePool(new Filesystem(new Local(__DIR__.'/')));
26+
$pool = $this->createCachePool();
2027

21-
$pool->getItem('test%string');
28+
$pool->getItem('test%string')->get();
2229
}
23-
}
30+
}

tests/IntegrationPoolTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,11 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Cache\Adapter\Filesystem\tests;
12+
namespace Cache\Adapter\Filesystem\Tests;
1313

14-
use Cache\Adapter\Filesystem\FilesystemCachePool;
1514
use Cache\IntegrationTests\CachePoolTest;
16-
use League\Flysystem\Adapter\Local;
17-
use League\Flysystem\Filesystem;
1815

1916
class IntegrationPoolTest extends CachePoolTest
2017
{
21-
/**
22-
* @type Filesystem
23-
*/
24-
private $filesystem;
25-
26-
public function createCachePool()
27-
{
28-
return new FilesystemCachePool($this->getFilesystem());
29-
}
30-
31-
private function getFilesystem()
32-
{
33-
if ($this->filesystem === null) {
34-
$this->filesystem = new Filesystem(new Local(__DIR__.'/'));
35-
}
36-
37-
return $this->filesystem;
38-
}
18+
use CreatePoolTrait;
3919
}

tests/IntegrationTagTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,11 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Cache\Adapter\Filesystem\tests;
12+
namespace Cache\Adapter\Filesystem\Tests;
1313

14-
use Cache\Adapter\Filesystem\FilesystemCachePool;
1514
use Cache\IntegrationTests\TaggableCachePoolTest;
16-
use League\Flysystem\Adapter\Local;
17-
use League\Flysystem\Filesystem;
1815

1916
class IntegrationTagTest extends TaggableCachePoolTest
2017
{
21-
/**
22-
* @type Filesystem
23-
*/
24-
private $filesystem;
25-
26-
public function createCachePool()
27-
{
28-
return new FilesystemCachePool($this->getFilesystem());
29-
}
30-
31-
private function getFilesystem()
32-
{
33-
if ($this->filesystem === null) {
34-
$this->filesystem = new Filesystem(new Local(__DIR__.'/'));
35-
}
36-
37-
return $this->filesystem;
38-
}
18+
use CreatePoolTrait;
3919
}

0 commit comments

Comments
 (0)