1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Cache; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Combines different cache instances into a combinatory cache |
9
|
|
|
* |
10
|
|
|
* @license GNU GPL v2+ |
11
|
|
|
* @since 1.0 |
12
|
|
|
* |
13
|
|
|
* @author mwjames |
14
|
|
|
*/ |
15
|
|
|
class CompositeCache implements Cache { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $caches = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @note The access hierarchy is determined by the order of the invoked cache |
24
|
|
|
* instances and it is assumed that the faster cache is accessible first. |
25
|
|
|
* |
26
|
|
|
* @since 1.0 |
27
|
|
|
* |
28
|
|
|
* @param Cache[] $caches |
29
|
|
|
*/ |
30
|
12 |
|
public function __construct( array $caches ) { |
31
|
|
|
|
32
|
12 |
|
foreach ( $caches as $key => $cache ) { |
33
|
|
|
|
34
|
12 |
|
if ( !is_int( $key ) ) { |
35
|
1 |
|
throw new RuntimeException( 'Associative key is not permitted' ); |
36
|
|
|
} |
37
|
|
|
|
38
|
11 |
|
if ( !$cache instanceOf Cache ) { |
39
|
1 |
|
throw new RuntimeException( 'The composite contains an invalid cache instance' ); |
40
|
|
|
} |
41
|
10 |
|
} |
42
|
|
|
|
43
|
10 |
|
$this->caches = $caches; |
44
|
10 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @since 1.0 |
48
|
|
|
* |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
3 |
|
public function fetch( $id ) { |
52
|
|
|
|
53
|
3 |
|
reset( $this->caches ); |
54
|
|
|
|
55
|
3 |
|
foreach ( $this->caches as $key => $cache ) { |
56
|
|
|
|
57
|
3 |
|
if ( !$cache->contains( $id ) ) { |
58
|
2 |
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
$data = $cache->fetch( $id ); |
62
|
|
|
|
63
|
|
|
// The data were not available in a previous instance and it is assumed |
64
|
|
|
// that a preceding cache is faster (in-memory lookup etc.) than the |
65
|
|
|
// current instance therefore the content is stored one level up for |
66
|
|
|
// the next lookup |
67
|
2 |
|
if ( $key > 0 ) { |
68
|
1 |
|
$this->caches[ $key - 1 ]->save( $id, $data ); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
2 |
|
return $data; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @since 1.0 |
79
|
|
|
* |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
3 |
|
public function contains( $id ) { |
83
|
|
|
|
84
|
3 |
|
foreach ( $this->caches as $cache ) { |
85
|
3 |
|
if ( $cache->contains( $id ) ) { |
86
|
2 |
|
return true; |
87
|
|
|
} |
88
|
2 |
|
} |
89
|
|
|
|
90
|
2 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @since 1.0 |
95
|
|
|
* |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
2 |
|
public function save( $id, $data, $ttl = 0 ) { |
99
|
2 |
|
foreach ( $this->caches as $cache ) { |
100
|
2 |
|
$cache->save( $id, $data, $ttl ); |
101
|
2 |
|
} |
102
|
2 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @since 1.0 |
106
|
|
|
* |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
2 |
|
public function delete( $id ) { |
110
|
2 |
|
foreach ( $this->caches as $cache ) { |
111
|
2 |
|
$cache->delete( $id ); |
112
|
2 |
|
} |
113
|
2 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @since 1.0 |
117
|
|
|
* |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
1 |
|
public function getStats() { |
121
|
|
|
|
122
|
1 |
|
$stats = array(); |
123
|
|
|
|
124
|
1 |
|
foreach ( $this->caches as $cache ) { |
125
|
1 |
|
$stats[$cache->getName()] = $cache->getStats(); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
1 |
|
return $stats; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @since 1.2 |
133
|
|
|
* |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
1 |
|
public function getName() { |
137
|
|
|
|
138
|
1 |
|
$name = ''; |
139
|
|
|
|
140
|
1 |
|
foreach ( $this->caches as $cache ) { |
141
|
1 |
|
$name = $name . '::' . $cache->getName(); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
1 |
|
return __CLASS__ . $name; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|