MediaWiki  master
KeyValueDependencyStore.php
Go to the documentation of this file.
1 <?php
22 
23 use BagOStuff;
24 use InvalidArgumentException;
25 
38  private $stash;
39 
43  public function __construct( BagOStuff $stash ) {
44  $this->stash = $stash;
45  }
46 
47  public function retrieveMulti( $type, array $entities ) {
48  $entitiesByKey = [];
49  foreach ( $entities as $entity ) {
50  $entitiesByKey[$this->getStoreKey( $type, $entity )] = $entity;
51  }
52 
53  $blobsByKey = $this->stash->getMulti( array_keys( $entitiesByKey ) );
54 
55  $results = [];
56  foreach ( $entitiesByKey as $key => $entity ) {
57  $blob = $blobsByKey[$key] ?? null;
58  $data = is_string( $blob ) ? json_decode( $blob, true ) : null;
59  $results[$entity] = $this->newEntityDependencies(
60  $data[self::KEY_PATHS] ?? [],
61  $data[self::KEY_AS_OF] ?? null
62  );
63  }
64 
65  return $results;
66  }
67 
68  public function storeMulti( $type, array $dataByEntity, $ttl ) {
69  $blobsByKey = [];
70  foreach ( $dataByEntity as $entity => $data ) {
71  if ( !is_array( $data[self::KEY_PATHS] ) || !is_int( $data[self::KEY_AS_OF] ) ) {
72  throw new InvalidArgumentException( "Invalid entry for '$entity'" );
73  }
74 
75  // Normalize the list by removing duplicates and sorting
76  $data[self::KEY_PATHS] = array_values( array_unique( $data[self::KEY_PATHS] ) );
77  sort( $data[self::KEY_PATHS], SORT_STRING );
78 
79  $blob = json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
80  $blobsByKey[$this->getStoreKey( $type, $entity )] = $blob;
81  }
82 
83  if ( $blobsByKey ) {
84  $this->stash->setMulti( $blobsByKey, $ttl, BagOStuff::WRITE_BACKGROUND );
85  }
86  }
87 
88  public function remove( $type, $entities ) {
89  $keys = [];
90  foreach ( (array)$entities as $entity ) {
91  $keys[] = $this->getStoreKey( $type, $entity );
92  }
93 
94  if ( $keys ) {
95  $this->stash->deleteMulti( $keys, BagOStuff::WRITE_BACKGROUND );
96  }
97  }
98 
99  public function renew( $type, $entities, $ttl ) {
100  $keys = [];
101  foreach ( (array)$entities as $entity ) {
102  $keys[] = $this->getStoreKey( $type, $entity );
103  }
104 
105  if ( $keys ) {
106  $this->stash->changeTTLMulti( $keys, $ttl, BagOStuff::WRITE_BACKGROUND );
107  }
108  }
109 
115  private function getStoreKey( $type, $entity ) {
116  return $this->stash->makeKey( "{$type}-dependencies", $entity );
117  }
118 }
Wikimedia\DependencyStore
Definition: DependencyStore.php:21
Wikimedia\DependencyStore\KeyValueDependencyStore\getStoreKey
getStoreKey( $type, $entity)
Definition: KeyValueDependencyStore.php:115
Wikimedia\DependencyStore\KeyValueDependencyStore\renew
renew( $type, $entities, $ttl)
Set the expiry for the currently tracked dependencies for an entity or set of entities.
Definition: KeyValueDependencyStore.php:99
Wikimedia\DependencyStore\KeyValueDependencyStore\$stash
BagOStuff $stash
Definition: KeyValueDependencyStore.php:38
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:71
$blob
$blob
Definition: testCompression.php:70
BagOStuff\WRITE_BACKGROUND
const WRITE_BACKGROUND
Definition: BagOStuff.php:94
Wikimedia\DependencyStore\KeyValueDependencyStore\__construct
__construct(BagOStuff $stash)
Definition: KeyValueDependencyStore.php:43
Wikimedia\DependencyStore\KeyValueDependencyStore
Lightweight class for tracking path dependencies lists via an object cache instance.
Definition: KeyValueDependencyStore.php:36
Wikimedia\DependencyStore\DependencyStore\newEntityDependencies
newEntityDependencies(array $paths=[], $asOf=null)
Definition: DependencyStore.php:39
Wikimedia\DependencyStore\KeyValueDependencyStore\retrieveMulti
retrieveMulti( $type, array $entities)
Get the currently tracked dependencies for a set of entities.
Definition: KeyValueDependencyStore.php:47
Wikimedia\DependencyStore\KeyValueDependencyStore\storeMulti
storeMulti( $type, array $dataByEntity, $ttl)
Set the currently tracked dependencies for a set of entities.
Definition: KeyValueDependencyStore.php:68
$keys
$keys
Definition: testCompression.php:72
Wikimedia\DependencyStore\DependencyStore
Class for tracking per-entity dependency path lists that are expensive to mass compute.
Definition: DependencyStore.php:28
$type
$type
Definition: testCompression.php:52