forked from onPHP/onphp-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequentialCache.class.php
More file actions
132 lines (111 loc) · 3.06 KB
/
SequentialCache.class.php
File metadata and controls
132 lines (111 loc) · 3.06 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
<?php
/****************************************************************************
* Copyright (C) 2012 by Artem Naumenko *
* *
* 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. *
* *
****************************************************************************/
final class SequentialCache extends CachePeer
{
/**
* List of all peers, including master
* @var array of CachePeer
*/
private $list = array();
/**
* List of slaves only
* @var array of CachePeer
*/
private $slaves = array();
/**
* @var CachePeer
*/
private $master = null;
/**
* @param CachePeer $master
* @param array $slaves or CachePeer
* @return SequentialCache
*/
public static function create(CachePeer $master, array $slaves = array())
{
return new self($master, $slaves);
}
/**
* @param CachePeer $master
* @param array $slaves or CachePeer
*/
public function __construct(CachePeer $master, array $slaves = array())
{
$this->setMaster($master);
foreach ($slaves as $cache) {
$this->addPeer($cache);
}
}
/**
* @param CachePeer $master
* @return \SequentialCache
*/
public function setMaster(CachePeer $master)
{
$this->master = $master;
$this->list = $this->slaves;
array_unshift($this->list, $this->master);
return $this;
}
/**
* @param CachePeer $master
* @return \SequentialCache
*/
public function addPeer(CachePeer $peer)
{
$this->list[] = $peer;
$this->slaves[] = $peer;
return $this;
}
public function get($key)
{
foreach ($this->list as $val) {
/* @var $val CachePeer */
$result = $val->get($key);
if (
!empty($result)
|| $val->isAlive()
) {
return $result;
}
}
throw new RuntimeException('All peers are dead');
}
public function append($key, $data)
{
return $this->foreachItem(__METHOD__, func_get_args());
}
public function decrement($key, $value)
{
throw new UnsupportedMethodException('decrement is not supported');
}
public function delete($key)
{
return $this->foreachItem(__METHOD__, func_get_args());
}
public function increment($key, $value)
{
throw new UnsupportedMethodException('increment is not supported');
}
protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM)
{
return $this->foreachItem(__METHOD__, func_get_args());
}
private function foreachItem($method, array $args)
{
$result = true;
foreach ($this->list as $peer) {
/* @var $peer CachePeer */
$result = call_user_func_array(array($peer, $method), $args) && $result;
}
return $result;
}
}