MediaWiki REL1_37
KeyValueDependencyStore.php
Go to the documentation of this file.
1<?php
22
23use BagOStuff;
24use 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}
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:86
Class for tracking per-entity dependency path lists that are expensive to mass compute.
newEntityDependencies(array $paths=[], $asOf=null)
Lightweight class for tracking path dependencies lists via an object cache instance.
storeMulti( $type, array $dataByEntity, $ttl)
Set the currently tracked dependencies for a set of entities.
renew( $type, $entities, $ttl)
Set the expiry for the currently tracked dependencies for an entity or set of entities.
retrieveMulti( $type, array $entities)
Get the currently tracked dependencies for a set of entities.