wiki.techinc.nl/includes/libs/WRStats/ArrayStatsStore.php
Tim Starling bcbfc9ccfc Introduce new WRStats library for write-read stats
A library for storage of counter value time series statistics, based
around the observation that memcached getMulti() is apparently fast
enough to do this in a simple manner, with incremented values stored
in time window buckets.

Bug: T310662
Change-Id: I26b1cdba0a06ad16ad8bb71b455e1b6180924d17
2022-07-05 10:35:19 +10:00

53 lines
1.1 KiB
PHP

<?php
namespace Wikimedia\WRStats;
/**
* In memory stats store.
*/
class ArrayStatsStore implements StatsStore {
/**
* @var array[] Associative array mapping data keys to arrays where the
* first entry is the value and the second is the TTL.
*/
private $data = [];
public function makeKey( $prefix, $internals, $entity ) {
$globality = $entity->isGlobal() ? 'global' : 'local';
return implode( ':',
array_merge( [ $globality ], $prefix, $internals, $entity->getComponents() )
);
}
public function incr( array $values, $ttl ) {
foreach ( $values as $key => $value ) {
if ( !isset( $this->data[$key] ) ) {
$this->data[$key] = [ 0, $ttl ];
}
$this->data[$key][0] += $value;
}
}
public function delete( array $keys ) {
foreach ( $keys as $key ) {
unset( $this->data[$key] );
}
}
public function query( array $keys ) {
$values = [];
foreach ( $keys as $key ) {
if ( isset( $this->data[$key] ) ) {
$values[$key] = $this->data[$key][0];
}
}
return $values;
}
/**
* @return array
*/
public function getData() {
return $this->data;
}
}