Skip to content

Commit 0629e41

Browse files
authored
Merge pull request #3 from moufmouf/classBoundMemoryAdapter
Adding a ClassBoundMemoryAdapter
2 parents e1b391b + dfd0d2c commit 0629e41

5 files changed

Lines changed: 109 additions & 7 deletions

File tree

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ $fileBoundCache->set('cache_key', $myDataToCache,
3737
$myDataToCache = $fileBoundCache->get('cache_key');
3838
```
3939

40-
You can also use the `MemoryAdapter` to store the cache in memory for even faster access in the same query.
40+
You can also use the `FileBoundMemoryAdapter` to store the cache in memory for even faster access in the same query.
4141

4242
```php
4343
use TheCodingMachine\CacheUtils\FileBoundCache;
44-
use TheCodingMachine\CacheUtils\MemoryAdapter;
44+
use TheCodingMachine\CacheUtils\FileBoundMemoryAdapter;
4545

46-
$fileBoundCache = new MemoryAdapter(new FileBoundCache($psr16Cache));
46+
$fileBoundCache = new FileBoundMemoryAdapter(new FileBoundCache($psr16Cache));
4747
```
4848

4949
### Class bound cache
@@ -79,3 +79,12 @@ class ClassBoundCache implements ClassBoundCacheInterface
7979
- `$analyzeParentClasses`: if set to true, the cache will be invalidated if one of the parent classes is modified
8080
- `$analyzeTraits`: if set to true, the cache will be invalidated if one of the traits is modified
8181
- `$analyzeInterfaces`: if set to true, the cache will be invalidated if one of the interfaces implemented is modified
82+
83+
You can also use the `ClassBoundMemoryAdapter` to store the cache in memory for even faster access in the same query.
84+
85+
```php
86+
use TheCodingMachine\CacheUtils\ClassBoundCache;
87+
use TheCodingMachine\CacheUtils\ClassBoundMemoryAdapter;
88+
89+
$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr16Cache));
90+
```

src/ClassBoundMemoryAdapter.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\CacheUtils;
6+
7+
use ReflectionClass;
8+
9+
/**
10+
* An adapter around a FileBoundCacheInterface that stores values in memory for the current request (for maximum performance)
11+
*/
12+
class ClassBoundMemoryAdapter implements ClassBoundCacheInterface
13+
{
14+
/** @var array<string, mixed> */
15+
private $items;
16+
/** @var ClassBoundCacheInterface */
17+
private $classBoundCache;
18+
19+
public function __construct(ClassBoundCacheInterface $classBoundCache)
20+
{
21+
$this->classBoundCache = $classBoundCache;
22+
}
23+
24+
/**
25+
* Fetches an element from the cache by key.
26+
*
27+
* @return mixed
28+
*/
29+
public function get(string $key)
30+
{
31+
if (isset($this->items[$key])) {
32+
return $this->items[$key];
33+
}
34+
35+
return $this->items[$key] = $this->classBoundCache->get($key);
36+
}
37+
38+
/**
39+
* Stores an item in the cache.
40+
*
41+
* @param mixed $item The item must be serializable.
42+
* @param ReflectionClass $refClass If the class is modified, the cache item is invalidated.
43+
*/
44+
public function set(string $key, $item, ReflectionClass $refClass, ?int $ttl = null): void
45+
{
46+
$this->items[$key] = $item;
47+
$this->classBoundCache->set($key, $item, $refClass, $ttl);
48+
}
49+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* An adapter around a FileBoundCacheInterface that stores values in memory for the current request (for maximum performance)
99
*/
10-
class MemoryAdapter implements FileBoundCacheInterface
10+
class FileBoundMemoryAdapter implements FileBoundCacheInterface
1111
{
1212
/** @var array<string, mixed> */
1313
private $items;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace TheCodingMachine\CacheUtils;
4+
5+
use function clearstatcache;
6+
use function file_get_contents;
7+
use function file_put_contents;
8+
use ReflectionClass;
9+
use function sleep;
10+
use function str_replace;
11+
use Symfony\Component\Cache\Simple\ArrayCache;
12+
use function sys_get_temp_dir;
13+
use PHPUnit\Framework\TestCase;
14+
use TheCodingMachine\CacheUtils\Fixtures\A;
15+
use function touch;
16+
17+
class ClassBoundMemoryAdapterTest extends TestCase
18+
{
19+
public function testMemory()
20+
{
21+
$cache = new ArrayCache();
22+
$fileBoundCache = new FileBoundCache($cache, 'prefix');
23+
$classBoundCache = new ClassBoundCache($fileBoundCache);
24+
$adapter = new ClassBoundMemoryAdapter($classBoundCache);
25+
26+
$classToTouch = new ReflectionClass(A::class);
27+
sleep(1);
28+
clearstatcache($classToTouch->getFileName());
29+
touch($classToTouch->getFileName());
30+
31+
$adapter->set('foo', 'bar', $classToTouch);
32+
33+
$this->assertSame('bar', $adapter->get('foo'));
34+
35+
$adapter2 = new ClassBoundMemoryAdapter($classBoundCache);
36+
$this->assertSame('bar', $adapter2->get('foo'));
37+
38+
sleep(1);
39+
clearstatcache($classToTouch->getFileName());
40+
touch($classToTouch->getFileName());
41+
42+
$this->assertSame('bar', $adapter->get('foo'));
43+
}
44+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
use PHPUnit\Framework\TestCase;
1313
use function touch;
1414

15-
class MemoryAdapterTest extends TestCase
15+
class FileBoundMemoryAdapterTest extends TestCase
1616
{
1717
public function testMemory()
1818
{
1919
$cache = new ArrayCache();
2020
$fileBoundCache = new FileBoundCache($cache, 'prefix');
21-
$adapter = new MemoryAdapter($fileBoundCache);
21+
$adapter = new FileBoundMemoryAdapter($fileBoundCache);
2222

2323
$tmpPath = sys_get_temp_dir().'/tmpCacheTest';
2424
touch($tmpPath);
@@ -29,7 +29,7 @@ public function testMemory()
2929

3030
$this->assertSame('bar', $adapter->get('foo'));
3131

32-
$adapter2 = new MemoryAdapter($fileBoundCache);
32+
$adapter2 = new FileBoundMemoryAdapter($fileBoundCache);
3333
$this->assertSame('bar', $adapter2->get('foo'));
3434

3535
sleep(1);

0 commit comments

Comments
 (0)