|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHPProm package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace PHPProm\Storage; |
|
13
|
|
|
|
|
14
|
|
|
class Memcached implements StorageInterface { |
|
15
|
|
|
|
|
16
|
|
|
protected $memcached; |
|
17
|
|
|
|
|
18
|
|
|
protected $prefix; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct($host, $port = 11211, $prefix = 'PHPProm:', $weight = 0) { |
|
21
|
|
|
$this->memcached = new \Memcached(); |
|
22
|
|
|
$this->memcached->addServer($host, $port, $weight); |
|
23
|
|
|
$this->prefix = $prefix; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function storeMeasurement($prefix, $key, $value) { |
|
27
|
|
|
$this->memcached->set($this->prefix.$prefix.':'.$key, $value); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function incrementMeasurement($prefix, $key) { |
|
31
|
|
|
$this->memcached->increment($this->prefix.$prefix.':'.$key); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getMeasurements($prefix, array $keys, $defaultValue = 'Nan') { |
|
35
|
|
|
$measurements = []; |
|
36
|
|
|
foreach ($keys as $key) { |
|
37
|
|
|
$measurements[$key] = $defaultValue; |
|
38
|
|
|
} |
|
39
|
|
|
$prefixedKeys = array_map(function($key) use ($prefix) { |
|
40
|
|
|
return $this->prefix.$prefix.':'.$key; |
|
41
|
|
|
}, $keys); |
|
42
|
|
|
foreach ($this->memcached->getMulti($prefixedKeys) as $key => $value) { |
|
43
|
|
|
$unprefixedKey = substr($key, strlen($this->prefix) + strlen($prefix) + 1); |
|
|
|
|
|
|
44
|
|
|
$measurements[$unprefixedKey] = $value !== false ? (float)$value : $defaultValue; |
|
45
|
|
|
} |
|
46
|
|
|
return $measurements; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.