Skip to content

Commit 9185243

Browse files
committed
Added all adapters
0 parents  commit 9185243

13 files changed

Lines changed: 467 additions & 0 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+

.gush.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Gush configuration file, any comments will be lost.
2+
repo_adapter: github
3+
issue_tracker: github
4+
repo_org: php-cache
5+
repo_name: array-adapter
6+
issue_project_org: php-cache
7+
issue_project_name: array-adapter
8+
meta-header: "This file is part of php-cache\\array-adapter package.\n\n(c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmai.com> \n\nThis source file is subject to the MIT license that is bundled\nwith this source code in the file LICENSE."
9+
pr_type:
10+
- feature
11+
- bugfix
12+
- refactor
13+
- tests
14+
- remove
15+
- style
16+
- documentation
17+
- security
18+
table-pr:
19+
fixed_tickets: ['Fixed Tickets', '']

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: php
2+
php:
3+
- 7.0
4+
- 5.6
5+
- 5.5
6+
- hhvm
7+
8+
sudo: false
9+
10+
matrix:
11+
fast_finish: true
12+
13+
before_install:
14+
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]]; then phpenv config-rm xdebug.ini; fi;
15+
- pip install --user codecov
16+
17+
before_script:
18+
- travis_retry composer self-update
19+
- travis_retry composer install --prefer-source --no-interaction
20+
21+
script:
22+
- php -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-clover=coverage.xml
23+
24+
after_success:
25+
- codecov

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Aaron Scherer
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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Array PSR-6 Cache pool
2+
[![Latest Stable Version](https://poser.pugx.org/cache/array-adapter/v/stable)](https://packagist.org/packages/cache/array-adapter) [![codecov.io](https://codecov.io/github/php-cache/array-adapter/coverage.svg?branch=master)](https://codecov.io/github/php-cache/array-adapter?branch=master) [![Build Status](https://travis-ci.org/php-cache/array-adapter.svg?branch=master)](https://travis-ci.org/php-cache/array-adapter) [![Total Downloads](https://poser.pugx.org/cache/array-adapter/downloads)](https://packagist.org/packages/cache/array-adapter) [![Monthly Downloads](https://poser.pugx.org/cache/array-adapter/d/monthly.png)](https://packagist.org/packages/cache/array-adapter) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
3+
4+
This is a PSR-6 cache implementation using a PHP array. 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).
6+
7+
This adapter could also be called Ephemeral or Memory adapter.
8+
9+
### Install
10+
11+
```bash
12+
composer require cache/array-adapter
13+
```
14+
15+
### Configure
16+
17+
No configuration is needed.
18+
19+
```php
20+
$pool = new ArrayCachePool();
21+
```

composer.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "cache/array-adapter",
3+
"description": "A PSR-6 cache implementation using a php array. This implementation supports tags",
4+
"type": "library",
5+
"license": "MIT",
6+
"minimum-stability": "stable",
7+
"keywords":
8+
[
9+
"cache",
10+
"psr-6",
11+
"array",
12+
"tag"
13+
],
14+
"homepage": "https://github.com/php-cache/array-adapter",
15+
"authors":
16+
[
17+
{
18+
"name": "Aaron Scherer",
19+
"email": "aequasi@gmail.com",
20+
"homepage": "https://github.com/aequasi"
21+
},
22+
{
23+
"name": "Tobias Nyholm",
24+
"email": "tobias.nyholm@gmail.com",
25+
"homepage": "https://github.com/nyholm"
26+
}
27+
],
28+
"require":
29+
{
30+
"php": "^5.5|^7.0",
31+
"psr/cache": "~1.0",
32+
"cache/adapter-common": "^0.2",
33+
"cache/hierarchical-cache": "^0.2",
34+
"cache/taggable-cache": "^0.3"
35+
},
36+
"require-dev":
37+
{
38+
"phpunit/phpunit": "^5.1|^4.0",
39+
"cache/integration-tests": "^0.7"
40+
},
41+
"provide":
42+
{
43+
"psr/cache-implementation": "~1.0"
44+
},
45+
"autoload":
46+
{
47+
"psr-4": {
48+
"Cache\\Adapter\\PHPArray\\": "src/"
49+
}
50+
},
51+
"autoload-dev":
52+
{
53+
"psr-4": {
54+
"Cache\\Adapter\\PHPArray\\Tests\\": "tests/"
55+
}
56+
}
57+
}

phpunit.xml.dist

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
<logging>
21+
<log type="coverage-text" target="php://stdout"/>
22+
<log type="coverage-html" target="./coverageReport"/>
23+
</logging>
24+
25+
<groups>
26+
<exclude>
27+
<group>benchmark</group>
28+
</exclude>
29+
</groups>
30+
31+
<filter>
32+
<whitelist>
33+
<directory>src/</directory>
34+
</whitelist>
35+
</filter>
36+
</phpunit>

src/ArrayCachePool.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\array-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
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\PHPArray;
13+
14+
use Cache\Adapter\Common\AbstractCachePool;
15+
use Cache\Adapter\Common\CacheItem;
16+
use Cache\Hierarchy\HierarchicalCachePoolTrait;
17+
use Cache\Hierarchy\HierarchicalPoolInterface;
18+
use Psr\Cache\CacheItemInterface;
19+
20+
/**
21+
* Array cache pool. You could set a limit of how many items you wantt to be stored to avoid memory leaks.
22+
*
23+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
24+
*/
25+
class ArrayCachePool extends AbstractCachePool implements HierarchicalPoolInterface
26+
{
27+
use HierarchicalCachePoolTrait;
28+
29+
/**
30+
* @type array
31+
*/
32+
private $cache;
33+
34+
/**
35+
* @type array
36+
* A map to hold keys
37+
*/
38+
private $keyMap = [];
39+
40+
/**
41+
* @type int
42+
* The maximum number of keys in the map
43+
*/
44+
private $limit;
45+
46+
/**
47+
* @type int
48+
* The next key that we should remove from the cache
49+
*/
50+
private $currentPosition = 0;
51+
52+
/**
53+
* @param int $limit the amount if items stored in the cache. Using a limit will reduce memory leaks.
54+
* @param array $cache
55+
*/
56+
public function __construct($limit = null, array &$cache = [])
57+
{
58+
$this->cache = &$cache;
59+
$this->limit = $limit;
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
protected function getItemWithoutGenerateCacheKey($key)
66+
{
67+
if (isset($this->deferred[$key])) {
68+
$item = $this->deferred[$key];
69+
70+
return is_object($item) ? clone $item : $item;
71+
}
72+
73+
return $this->fetchObjectFromCache($key);
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
protected function fetchObjectFromCache($key)
80+
{
81+
$storageKey = $this->getHierarchyKey($key);
82+
if (isset($this->cache[$storageKey])) {
83+
return $this->cache[$storageKey];
84+
}
85+
86+
return new CacheItem($key, false);
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
protected function clearAllObjectsFromCache()
93+
{
94+
$this->cache = [];
95+
96+
return true;
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
protected function clearOneObjectFromCache($key)
103+
{
104+
$this->commit();
105+
$keyString = $this->getHierarchyKey($key, $path);
106+
if (isset($this->cache[$path])) {
107+
$this->cache[$path]++;
108+
} else {
109+
$this->cache[$path] = 0;
110+
}
111+
$this->clearHierarchyKeyCache();
112+
113+
unset($this->cache[$keyString]);
114+
115+
return true;
116+
}
117+
118+
/**
119+
* {@inheritdoc}
120+
*/
121+
protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
122+
{
123+
$key = $this->getHierarchyKey($key);
124+
$this->cache[$key] = $item;
125+
126+
if ($this->limit !== null) {
127+
// Remove the oldest value
128+
if (isset($this->keyMap[$this->currentPosition])) {
129+
unset($this->cache[$this->keyMap[$this->currentPosition]]);
130+
}
131+
132+
// Add the new key to the current position
133+
$this->keyMap[$this->currentPosition] = $key;
134+
135+
// Increase the current position
136+
$this->currentPosition = ($this->currentPosition + 1) % $this->limit;
137+
}
138+
139+
return true;
140+
}
141+
142+
/**
143+
* {@inheritdoc}
144+
*/
145+
protected function getValueFormStore($key)
146+
{
147+
if (isset($this->cache[$key])) {
148+
return $this->cache[$key];
149+
}
150+
}
151+
}

tests/ArrayCachePoolTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\array-adapter package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
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\PHPArray\Tests;
13+
14+
use Cache\Adapter\PHPArray\ArrayCachePool;
15+
16+
class ArrayCachePoolTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testLimit()
19+
{
20+
$pool = new ArrayCachePool(2);
21+
$item = $pool->getItem('key1')->set('value1');
22+
$pool->save($item);
23+
24+
$item = $pool->getItem('key2')->set('value2');
25+
$pool->save($item);
26+
27+
// Both items should be in the pool, nothing strange yet
28+
$this->assertTrue($pool->hasItem('key1'));
29+
$this->assertTrue($pool->hasItem('key2'));
30+
31+
$item = $pool->getItem('key3')->set('value3');
32+
$pool->save($item);
33+
34+
// First item should be dropped
35+
$this->assertFalse($pool->hasItem('key1'));
36+
$this->assertTrue($pool->hasItem('key2'));
37+
$this->assertTrue($pool->hasItem('key3'));
38+
39+
$this->assertFalse($pool->getItem('key1')->isHit());
40+
$this->assertTrue($pool->getItem('key2')->isHit());
41+
$this->assertTrue($pool->getItem('key3')->isHit());
42+
43+
$item = $pool->getItem('key4')->set('value4');
44+
$pool->save($item);
45+
46+
// Only the last two items should be in place
47+
$this->assertFalse($pool->hasItem('key1'));
48+
$this->assertFalse($pool->hasItem('key2'));
49+
$this->assertTrue($pool->hasItem('key3'));
50+
$this->assertTrue($pool->hasItem('key4'));
51+
}
52+
}

0 commit comments

Comments
 (0)