forked from onPHP/onphp-framework
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCyclicAggregateCache.class.php
98 lines (74 loc) · 2.32 KB
/
CyclicAggregateCache.class.php
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
<?php
/****************************************************************************
* Copyright (C) 2011 by 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. *
* *
****************************************************************************/
/**
* One more Aggregate cache.
*
* @ingroup Cache
**/
class CyclicAggregateCache extends BaseAggregateCache
{
const DEFAULT_SUMMARY_WEIGHT = 1000;
private $summaryWeight = self::DEFAULT_SUMMARY_WEIGHT;
private $sorted = false;
/**
* @return CyclicAggregateCache
**/
public static function create()
{
return new self();
}
public function setSummaryWeight($weight)
{
Assert::isPositiveInteger($weight);
$this->summaryWeight = $weight;
$this->sorted = false;
return $this;
}
public function addPeer($label, CachePeer $peer, $mountPoint)
{
Assert::isLesserOrEqual($mountPoint, $this->summaryWeight);
Assert::isGreaterOrEqual($mountPoint, 0);
$this->doAddPeer($label, $peer);
$this->peers[$label]['mountPoint'] = $mountPoint;
$this->sorted = false;
return $this;
}
protected function guessLabel($key)
{
if (!$this->sorted)
$this->sortPeers();
$point = hexdec(substr(sha1($key), 0, 5)) % $this->summaryWeight;
$firstPeer = reset($this->peers);
while ($peer = current($this->peers)) {
if ($point <= $peer['mountPoint'])
return key($this->peers);
next($this->peers);
}
if ($point <= ($firstPeer['mountPoint'] + $this->summaryWeight)) {
reset($this->peers);
return key($this->peers);
}
Assert::isUnreachable();
}
private function sortPeers()
{
uasort($this->peers, array('self', 'comparePeers'));
$this->sorted = true;
return $this;
}
private static function comparePeers(array $first, array $second)
{
if ($first['mountPoint'] == $second['mountPoint'])
return 0;
return
($first['mountPoint'] < $second['mountPoint']) ? -1 : 1;
}
}
?>