Skip to content

Commit d72c007

Browse files
authored
Patch simple cache (#131)
* Added test and fixed minor bug * Added meta files * Added class comment * Updated integration test version * Require Symfony for testing * Require mockery
1 parent f4330b2 commit d72c007

9 files changed

+205
-2
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+

.travis.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: php
2+
sudo: false
3+
4+
matrix:
5+
include:
6+
- php: 7.0
7+
8+
cache:
9+
directories:
10+
- "$HOME/.composer/cache"
11+
12+
install:
13+
- composer update
14+
15+
script:
16+
- ./vendor/bin/phpunit --coverage-clover=coverage.xml
17+
18+
after_success:
19+
- pip install --user codecov && codecov
20+
21+
notifications:
22+
email: false

Changelog.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## UNRELEASED
6+
7+
## 0.1.0
8+
9+
First release

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Aaron Scherer, Tobias Nyholm
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PSR-6 to PSR-16 Bridge (Simple cache)
2+
[![Gitter](https://badges.gitter.im/php-cache/cache.svg)](https://gitter.im/php-cache/cache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
3+
[![Latest Stable Version](https://poser.pugx.org/cache/simple-cache-bridge/v/stable)](https://packagist.org/packages/cache/simple-cache-bridge)
4+
[![codecov.io](https://codecov.io/github/php-cache/simple-cache-bridge/coverage.svg?branch=master)](https://codecov.io/github/array-cache/apc-adapter?branch=master)
5+
[![Total Downloads](https://poser.pugx.org/cache/simple-cache-bridge/downloads)](https://packagist.org/packages/cache/simple-cache-bridge)
6+
[![Monthly Downloads](https://poser.pugx.org/cache/simple-cache-bridge/d/monthly.png)](https://packagist.org/packages/cache/simple-cache-bridge)
7+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
8+
9+
This is a PSR-6 cache implementation using PHP array. It is a part of the PHP Cache organisation. To read about
10+
features like tagging and hierarchy support please read the shared documentation at [www.php-cache.com](http://www.php-cache.com).
11+
12+
### Install
13+
14+
```bash
15+
composer require cache/simple-cache-bridge
16+
```
17+
18+
### Use
19+
20+
You need an existing PSR-6 pool as a cnstructor argument to the bridge.
21+
22+
```php
23+
$psr6pool = new ArrayCachePool();
24+
$simpleCache = new SimpleCacheBridge($psr6pool);
25+
```
26+
27+
### Contribute
28+
29+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/php-cache/cache) or
30+
report any issues you find on the [issue tracker](http://issues.php-cache.com).

SimpleCacheBridge.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
use Psr\Cache\InvalidArgumentException as CacheInvalidArgumentException;
1818
use Psr\SimpleCache\CacheInterface;
1919

20+
/**
21+
* Adds a SimpleCache interface on a PSR-6 cache pool.
22+
*
23+
* @author Magnus Nordlander <[email protected]>
24+
*/
2025
class SimpleCacheBridge implements CacheInterface
2126
{
2227
/**
@@ -57,12 +62,12 @@ public function set($key, $value, $ttl = null)
5762
{
5863
try {
5964
$item = $this->cacheItemPool->getItem($key);
65+
$item->expiresAfter($ttl);
6066
} catch (CacheInvalidArgumentException $e) {
6167
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
6268
}
6369

6470
$item->set($value);
65-
$item->expiresAfter($ttl);
6671

6772
return $this->cacheItemPool->save($item);
6873
}
@@ -144,7 +149,12 @@ public function setMultiple($values, $ttl = null)
144149
foreach ($items as $key => $item) {
145150
/* @var $item CacheItemInterface */
146151
$item->set($values[$key]);
147-
$item->expiresAfter($ttl);
152+
153+
try {
154+
$item->expiresAfter($ttl);
155+
} catch (CacheInvalidArgumentException $e) {
156+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
157+
}
148158

149159
$itemSuccess = $itemSuccess && $this->cacheItemPool->saveDeferred($item);
150160
}

Tests/IntegrationTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 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\Bridge\SimpleCache\Tests;
13+
14+
use Cache\Bridge\SimpleCache\SimpleCacheBridge;
15+
use Cache\IntegrationTests\SimpleCacheTest as BaseTest;
16+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
17+
18+
class IntegrationTest extends BaseTest
19+
{
20+
public function createSimpleCache()
21+
{
22+
return new SimpleCacheBridge(new ArrayAdapter());
23+
}
24+
}

composer.json

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "cache/simple-cache-bridge",
3+
"description": "A PSR-6 bridge to PSR-16. This will make any PSR-6 cache compatible with SimpleCache.",
4+
"type": "library",
5+
"license": "MIT",
6+
"minimum-stability": "dev",
7+
"keywords": [
8+
"cache",
9+
"psr-6",
10+
"psr-16",
11+
"bridge"
12+
],
13+
"homepage": "http://www.php-cache.com/en/latest/",
14+
"authors": [
15+
{
16+
"name": "Magnus Nordlander",
17+
"email": "[email protected]",
18+
"homepage": "https://github.com/magnusnordlander"
19+
}
20+
],
21+
"require": {
22+
"php": "^5.5 || ^7.0",
23+
"psr/cache": "^1.0",
24+
"psr/simple-cache": "^1.0"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^4.0 || ^5.1",
28+
"cache/integration-tests": "^0.14",
29+
"mockery/mockery": "^0.9",
30+
"symfony/cache": "^3.2"
31+
},
32+
"provide": {
33+
"psr/simple-cache-implementation": "^1.0"
34+
},
35+
"autoload": {
36+
"psr-4": {
37+
"Cache\\Bridge\\SimpleCache\\": ""
38+
},
39+
"exclude-from-classmap": [
40+
"/Tests/"
41+
]
42+
},
43+
"extra": {
44+
"branch-alias": {
45+
"dev-master": "0.2-dev"
46+
}
47+
}
48+
}

phpunit.xml.dist

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
<testsuites>
15+
<testsuite name="Main Test Suite">
16+
<directory>./Tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<groups>
21+
<exclude>
22+
<group>benchmark</group>
23+
</exclude>
24+
</groups>
25+
26+
<filter>
27+
<whitelist>
28+
<directory>./</directory>
29+
<exclude>
30+
<directory>./Tests</directory>
31+
<directory>./vendor</directory>
32+
</exclude>
33+
</whitelist>
34+
</filter>
35+
</phpunit>

0 commit comments

Comments
 (0)