-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathAggregateCache.php
More file actions
137 lines (113 loc) · 3.08 KB
/
AggregateCache.php
File metadata and controls
137 lines (113 loc) · 3.08 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
<?php
/****************************************************************************
* Copyright (C) 2005-2008 by Anton E. Lebedevich, Konstantin V. Arkhipov *
* *
* 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. *
* *
****************************************************************************/
namespace OnPHP\Core\Cache;
/**
* A wrapper to multiple cache for workload
* distribution using CachePeer childs.
*
* @ingroup Cache
**/
class AggregateCache extends BaseAggregateCache
{
const LEVEL_ULTRAHIGH = 0xFFFF;
const LEVEL_HIGH = 0xC000;
const LEVEL_NORMAL = 0x8000;
const LEVEL_LOW = 0x4000;
const LEVEL_VERYLOW = 0x0001;
private $levels = array();
/**
* @return AggregateCache
**/
public static function create()
{
return new self;
}
/**
* @return AggregateCache
**/
public function addPeer(
$label, CachePeer $peer, $level = self::LEVEL_NORMAL
)
{
$this->doAddPeer($label, $peer);
$this->peers[$label]['level'] = $level;
return $this;
}
/**
* @return AggregateCache
**/
public function setClassLevel($class, $level)
{
$this->levels[$class] = $level;
return $this;
}
/**
* brain
**/
protected function guessLabel($key)
{
$class = $this->getClassName();
if (isset($this->levels[$class])) {
$classLevel = $this->levels[$class];
} else {
$classLevel = self::LEVEL_NORMAL;
}
// init by $key, randomness will be restored later
mt_srand(intval(hexdec(substr(md5($key), 3, 7))));
$zeroDistances = array();
$weights = array();
foreach ($this->peers as $label => $peer) {
$distance = abs($classLevel - $peer['level']);
if (!$distance) {
$zeroDistances[] = $label;
} else {
$weights[$peer['level']] = 1 / pow($distance, 2); // BOVM
}
}
if (count($zeroDistances)) {
$selectedLabel =
$zeroDistances[mt_rand(0, count($zeroDistances) - 1)];
} else {
// weighted random level selection
$sum = mt_rand() * array_sum($weights) / mt_getrandmax();
$peerLevel = null;
foreach ($weights as $level => $weight) {
if ($sum <= $weight) {
$peerLevel = $level;
break;
} else {
$sum -= $weight;
}
}
$selectedPeers = array();
foreach ($this->peers as $label => $peer) {
if ($peer['level'] == $peerLevel) {
$selectedPeers[] = $label;
}
}
$selectedLabel = $selectedPeers[mt_rand(0, count($selectedPeers) - 1)];
}
if (isset($this->peers[$selectedLabel]['stat'][$class])) {
++$this->peers[$selectedLabel]['stat'][$class];
} else {
$this->peers[$selectedLabel]['stat'][$class] = 1;
}
// restore randomness
mt_srand(
(int) (
(int) (microtime(true) << 2)
* (rand(time() / 2, time()) >> 2)
)
);
return $selectedLabel;
}
}
?>