| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\WRStats; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class ArrayStatsStore implements StatsStore { |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
private $data = []; |
| 14 |
|
| 15 |
public function makeKey( $prefix, $internals, $entity ) { |
| 16 |
$globality = $entity->isGlobal() ? 'global' : 'local'; |
| 17 |
return implode( ':', |
| 18 |
array_merge( [ $globality ], $prefix, $internals, $entity->getComponents() ) |
| 19 |
); |
| 20 |
} |
| 21 |
|
| 22 |
public function incr( array $values, $ttl ) { |
| 23 |
foreach ( $values as $key => $value ) { |
| 24 |
if ( !isset( $this->data[$key] ) ) { |
| 25 |
$this->data[$key] = [ 0, $ttl ]; |
| 26 |
} |
| 27 |
$this->data[$key][0] += $value; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public function delete( array $keys ) { |
| 32 |
foreach ( $keys as $key ) { |
| 33 |
unset( $this->data[$key] ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function query( array $keys ) { |
| 38 |
$values = []; |
| 39 |
foreach ( $keys as $key ) { |
| 40 |
if ( isset( $this->data[$key] ) ) { |
| 41 |
$values[$key] = $this->data[$key][0]; |
| 42 |
} |
| 43 |
} |
| 44 |
return $values; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function getData() { |
| 51 |
return $this->data; |
| 52 |
} |
| 53 |
} |