18 private const TABLE_NAME =
'translate_cache';
21 private $loadBalancer;
25 public function __construct( ILoadBalancer $loadBalancer, JsonCodec $jsonCodec ) {
26 $this->loadBalancer = $loadBalancer;
27 $this->jsonCodec = $jsonCodec;
31 public function get(
string ...$keynames ): array {
32 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
35 [
'tc_key',
'tc_value',
'tc_exptime',
'tc_tag' ],
36 [
'tc_key ' => $keynames ],
40 return $this->buildEntries( $rows );
44 $dbr = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
46 $conds = [
'tc_key' => $keyname ];
50 [
'tc_key',
'tc_value',
'tc_exptime',
'tc_tag' ],
56 $entries = $this->buildEntries( $rows );
57 return count( $entries ) ? $entries[0] :
null;
61 public function getByTag(
string $tag ): array {
62 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
65 [
'tc_key',
'tc_value',
'tc_exptime',
'tc_tag' ],
70 return $this->buildEntries( $rows );
73 public function has(
string $keyname ): bool {
74 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
75 $hasRow = $dbr->selectRow(
78 [
'tc_key' => $keyname ],
85 public function hasEntryWithTag(
string $tag ): bool {
86 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
87 $hasRow = $dbr->selectRow(
97 public function hasExpiredEntry(
string $keyname ): bool {
98 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
99 $row = $dbr->selectRow(
102 [
'tc_key' => $keyname ],
106 if ( $row ===
false ) {
110 $rows =
new ArrayIterator( [ $row ] );
111 $entry = $this->buildEntries( $rows )[0];
112 return $entry->hasExpired();
115 public function set( PersistentCacheEntry ...$entries ): void {
116 $dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
118 foreach ( $entries as $entry ) {
119 $value = $this->jsonCodec->serialize( $entry->value() );
121 'tc_key' => $entry->key(),
122 'tc_value' => $value,
123 'tc_exptime' => $entry->exptime(),
124 'tc_tag' => $entry->tag()
128 'tc_value' => $value,
129 'tc_exptime' => $entry->exptime(),
130 'tc_tag' => $entry->tag()
143 public function setExpiry(
string $keyname,
int $expiryTime ): void {
144 $dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
147 [
'tc_exptime' => $expiryTime ],
148 [
'tc_key' => $keyname ],
153 public function delete(
string ...$keynames ): void {
154 $dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
157 [
'tc_key' => $keynames ],
162 public function deleteEntriesWithTag(
string $tag ): void {
163 $dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
166 [
'tc_tag' => $tag ],
171 public function clear(): void {
172 $dbw = $this->loadBalancer->getConnectionRef( DB_PRIMARY );
181 private function buildEntries( Iterator $rows ): array {
183 foreach ( $rows as $row ) {
184 $entries[] =
new PersistentCacheEntry(
186 $this->jsonCodec->unserialize( $row->tc_value ),
187 $row->tc_exptime ? (
int)$row->tc_exptime : null,