|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleCache\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Allows combining multiple caches. |
|
9
|
|
|
* |
|
10
|
|
|
* The caches first in the list are hit first. Therefore cheap caches |
|
11
|
|
|
* (ie in memory caches) should be placed before more expensive ones |
|
12
|
|
|
* (ie memcached). |
|
13
|
|
|
* |
|
14
|
|
|
* When a cache entry is requested and is not found in the first cache, |
|
15
|
|
|
* a new entry will be written to this cache. In case the requested entry |
|
16
|
|
|
* was found in a later cache, its value will be written to the first one. |
|
17
|
|
|
* |
|
18
|
|
|
* @licence GNU GPL v2+ |
|
19
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
20
|
|
|
*/ |
|
21
|
|
|
class CombinatoryCache implements Cache { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Cache[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $caches; |
|
27
|
|
|
|
|
28
|
|
|
protected $hasMoreThenOneCache; |
|
29
|
|
|
|
|
30
|
12 |
|
public function __construct( array $caches ) { |
|
31
|
12 |
|
$this->assertCachesListIsValid( $caches ); |
|
32
|
|
|
|
|
33
|
8 |
|
$this->caches = $caches; |
|
34
|
8 |
|
$this->hasMoreThenOneCache = count( $this->caches ) > 1; |
|
35
|
8 |
|
} |
|
36
|
|
|
|
|
37
|
12 |
|
protected function assertCachesListIsValid( array $caches ) { |
|
38
|
12 |
|
$this->assertNotEmpty( $caches ); |
|
39
|
11 |
|
$this->assertAreCaches( $caches ); |
|
40
|
8 |
|
} |
|
41
|
|
|
|
|
42
|
12 |
|
protected function assertNotEmpty( $caches ) { |
|
43
|
12 |
|
if ( empty( $caches ) ) { |
|
44
|
1 |
|
throw new InvalidArgumentException( 'The caches list needs to contain at least one cache' ); |
|
45
|
|
|
} |
|
46
|
11 |
|
} |
|
47
|
|
|
|
|
48
|
11 |
|
protected function assertAreCaches( $caches ) { |
|
49
|
11 |
|
foreach ( $caches as $cache ) { |
|
50
|
11 |
|
if ( !is_object( $cache ) || !( $cache instanceof Cache ) ) { |
|
51
|
11 |
|
throw new InvalidArgumentException( 'The cache list can only contain instances of Cache' ); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
8 |
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
public function get( $key ) { |
|
57
|
4 |
|
reset( $this->caches ); |
|
58
|
4 |
|
$firstCacheIndex = key( $this->caches ); |
|
59
|
|
|
|
|
60
|
4 |
|
foreach ( $this->caches as $currentIndex => $cache ) { |
|
61
|
4 |
|
$value = $cache->get( $key ); |
|
62
|
|
|
|
|
63
|
4 |
|
if ( $value !== null ) { |
|
64
|
3 |
|
if ( $currentIndex !== $firstCacheIndex ) { |
|
65
|
2 |
|
$this->setInFirstCache( $firstCacheIndex, $key, $value ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
return $value; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
protected function setInFirstCache( $firstCacheIndex, $key, $value ) { |
|
76
|
2 |
|
$this->caches[$firstCacheIndex]->set( $key, $value ); |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
public function has( $key ) { |
|
80
|
2 |
|
foreach ( $this->caches as $cache ) { |
|
81
|
2 |
|
if ( $cache->has( $key ) ) { |
|
82
|
2 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
public function set( $key, $value ) { |
|
90
|
2 |
|
foreach ( $this->caches as $cache ) { |
|
91
|
2 |
|
$cache->set( $key, $value ); |
|
92
|
|
|
} |
|
93
|
2 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|