Skip to content

Commit 77295fc

Browse files
authored
Added support for simple cache (#134)
* Added support for SimpleCache * Added tests * Bugfix with cloning * Style fix * Minor * Updated integration tests version * Bugfix * Allow same keys * cs
1 parent f3d00c5 commit 77295fc

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

ArrayCachePool.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,17 @@ protected function getItemWithoutGenerateCacheKey($key)
8080
*/
8181
protected function fetchObjectFromCache($key)
8282
{
83-
// Not used
84-
}
85-
86-
public function getItem($key)
87-
{
88-
$this->validateKey($key);
89-
if (isset($this->deferred[$key])) {
90-
/** @type CacheItem $item */
91-
$item = clone $this->deferred[$key];
92-
$item->moveTagsToPrevious();
93-
94-
return $item;
83+
if (!isset($this->cache[$key])) {
84+
return [false, null, [], null];
9585
}
9686

97-
$storageKey = $this->getHierarchyKey($key);
98-
if (isset($this->cache[$storageKey])) {
99-
$item = $this->cache[$storageKey];
100-
$item->moveTagsToPrevious();
87+
list($data, $tags, $timestamp) = $this->cache[$key];
10188

102-
return $item;
89+
if (is_object($data)) {
90+
$data = clone $data;
10391
}
10492

105-
return new CacheItem($key, false);
93+
return [true, $data, $tags, $timestamp];
10694
}
10795

10896
/**
@@ -140,7 +128,11 @@ protected function clearOneObjectFromCache($key)
140128
protected function storeItemInCache(PhpCacheItem $item, $ttl)
141129
{
142130
$key = $this->getHierarchyKey($item->getKey());
143-
$this->cache[$key] = $item;
131+
$value = $item->get();
132+
if (is_object($value)) {
133+
$value = clone $value;
134+
}
135+
$this->cache[$key] = [$value, $item->getTags(), $item->getExpirationTimestamp()];
144136

145137
if ($this->limit !== null) {
146138
// Remove the oldest value

Tests/CreatePoolTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public function createCachePool()
2121
{
2222
return new ArrayCachePool(null, $this->cacheArray);
2323
}
24+
25+
public function createSimpleCache()
26+
{
27+
return $this->createCachePool();
28+
}
2429
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\Adapter\PHPArray\Tests;
13+
14+
use Cache\IntegrationTests\SimpleCacheTest;
15+
16+
class IntegrationSimpleCacheTest extends SimpleCacheTest
17+
{
18+
use CreatePoolTrait;
19+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
"require": {
2727
"php": "^5.5 || ^7.0",
2828
"psr/cache": "^1.0",
29+
"psr/simple-cache": "^1.0",
2930
"cache/adapter-common": "^0.4",
3031
"cache/hierarchical-cache": "^0.4"
3132
},
3233
"require-dev": {
3334
"phpunit/phpunit": "^4.0 || ^5.1",
34-
"cache/integration-tests": "^0.14"
35+
"cache/integration-tests": "^0.16"
3536
},
3637
"provide": {
3738
"psr/cache-implementation": "^1.0"

0 commit comments

Comments
 (0)