1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MatthiasMullie\Scrapbook\Adapters\Collections; |
4
|
|
|
|
5
|
|
|
use MatthiasMullie\Scrapbook\Adapters\Collections\Utils\PrefixKeys; |
6
|
|
|
use MatthiasMullie\Scrapbook\Adapters\MemoryStore as Adapter; |
7
|
|
|
use ReflectionObject; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MemoryStore adapter for a subset of data. |
11
|
|
|
* |
12
|
|
|
* @author Matthias Mullie <[email protected]> |
13
|
|
|
* @copyright Copyright (c) 2014, Matthias Mullie. All rights reserved |
14
|
|
|
* @license LICENSE MIT |
15
|
|
|
*/ |
16
|
|
|
class MemoryStore extends PrefixKeys |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param string $name |
20
|
|
|
*/ |
21
|
|
|
public function __construct(Adapter $cache, $name) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($cache, $name.':'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function flush() |
30
|
|
|
{ |
31
|
|
|
/* |
32
|
|
|
* It's not done to use ReflectionObject, but: |
33
|
|
|
* - I *really* don't want to expose $cache->items publicly |
34
|
|
|
* - This is very specific to MemoryStore implementation, it can assume |
35
|
|
|
* these kind of implementation details (like how it's ok for a child |
36
|
|
|
* to use protected methods - this just can't be a subclass for |
37
|
|
|
* practical reasons, but it mostly acts like one) |
38
|
|
|
* - Reflection is not the most optimized thing, but that doesn't matter |
39
|
|
|
* too much for MemoryStore, which is not a *real* cache |
40
|
|
|
*/ |
41
|
|
|
$object = new \ReflectionObject($this->cache); |
42
|
|
|
$property = $object->getProperty('items'); |
43
|
|
|
$property->setAccessible(true); |
44
|
|
|
$items = $property->getValue($this->cache); |
45
|
|
|
|
46
|
|
|
foreach ($items as $key => $value) { |
47
|
|
|
if (0 === strpos($key, $this->prefix)) { |
48
|
|
|
$this->cache->delete($key); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|