forked from onPHP/onphp-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseAggregateCache.class.php
More file actions
202 lines (159 loc) · 4.22 KB
/
BaseAggregateCache.class.php
File metadata and controls
202 lines (159 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/****************************************************************************
* Copyright (C) 2011 by Anton E. Lebedevich, Konstantin V. Arkhipov, *
* Evgeny V. Kokovikhin *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
****************************************************************************/
/**
* Base common parent for all aggregate caches
*
* @ingroup Cache
**/
abstract class BaseAggregateCache extends SelectivePeer
{
protected $peers = array();
/**
* @return BaseAggregateCache
**/
public function dropPeer($label)
{
if (!isset($this->peers[$label]))
throw new MissingElementException(
"there is no peer with '{$label}' label"
);
unset($this->peer[$label]);
return $this;
}
/**
* @return BaseAggregateCache
**/
public function checkAlive()
{
$this->alive = false;
foreach ($this->peers as $label => $peer)
if ($peer['object']->isAlive())
$this->alive = true;
else
unset($this->peers[$label]);
return $this->alive;
}
abstract protected function guessLabel($key);
/**
* @return BaseAggregateCache
**/
protected function doAddPeer($label, CachePeer $peer)
{
if (isset($this->peers[$label]))
throw new WrongArgumentException(
'use unique names for your peers'
);
if ($peer->isAlive())
$this->alive = true;
$this->peers[$label]['object'] = $peer;
$this->peers[$label]['stat'] = array();
return $this;
}
/**
* low-level cache access
**/
public function increment($key, $value)
{
$label = $this->guessLabel($key);
if ($this->peers[$label]['object']->isAlive())
return $this->peers[$label]['object']->increment($key, $value);
else
$this->checkAlive();
return null;
}
public function decrement($key, $value)
{
$label = $this->guessLabel($key);
if ($this->peers[$label]['object']->isAlive())
return $this->peers[$label]['object']->decrement($key, $value);
else
$this->checkAlive();
return null;
}
public function get($key)
{
$label = $this->guessLabel($key);
if ($this->peers[$label]['object']->isAlive())
return $this->peers[$label]['object']->get($key);
else
$this->checkAlive();
return null;
}
public function getList($indexes)
{
$labels = array();
$out = array();
foreach ($indexes as $index)
$labels[$this->guessLabel($index)][] = $index;
foreach ($labels as $label => $indexList) {
/** @var CachePeer $peer **/
$peer = $this->peers[$label]['object'];
if ($peer->isAlive()) {
if ($list = $peer->getList($indexList))
$out = array_merge($out, $list);
} else
$this->checkAlive();
}
return $out;
}
public function delete($key)
{
$label = $this->guessLabel($key);
if (!$this->peers[$label]['object']->isAlive()) {
$this->checkAlive();
return false;
}
return $this->peers[$label]['object']->delete($key);
}
/**
* @return AggregateCache
**/
public function clean()
{
foreach ($this->peers as $peer)
$peer['object']->clean();
$this->checkAlive();
return parent::clean();
}
public function getStats()
{
$stats = array();
foreach ($this->peers as $level => $peer)
$stats[$level] = $peer['stat'];
return $stats;
}
public function append($key, $data)
{
$label = $this->guessLabel($key);
if ($this->peers[$label]['object']->isAlive())
return $this->peers[$label]['object']->append($key, $data);
else
$this->checkAlive();
return false;
}
protected function store(
$action, $key, $value, $expires = Cache::EXPIRES_MINIMUM
)
{
$label = $this->guessLabel($key);
if ($this->peers[$label]['object']->isAlive())
return
$this->peers[$label]['object']->$action(
$key,
$value,
$expires
);
else
$this->checkAlive();
return false;
}
}
?>