17 private const TABLE_NAME =
'translate_cache';
18 private ILoadBalancer $loadBalancer;
19 private JsonCodec $jsonCodec;
21 public function __construct( ILoadBalancer $loadBalancer, JsonCodec $jsonCodec ) {
22 $this->loadBalancer = $loadBalancer;
23 $this->jsonCodec = $jsonCodec;
27 public function get(
string ...$keynames ): array {
28 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
29 $rows = $dbr->newSelectQueryBuilder()
30 ->select( [
'tc_key',
'tc_value',
'tc_exptime',
'tc_tag' ] )
31 ->from( self::TABLE_NAME )
32 ->where( [
'tc_key' => $keynames ] )
33 ->caller( __METHOD__ )
36 return $this->buildEntries( $rows );
40 public function getByTag(
string $tag ): array {
41 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
42 $rows = $dbr->newSelectQueryBuilder()
43 ->select( [
'tc_key',
'tc_value',
'tc_exptime',
'tc_tag' ] )
44 ->from( self::TABLE_NAME )
45 ->where( [
'tc_tag' => $tag ] )
46 ->caller( __METHOD__ )
49 return $this->buildEntries( $rows );
52 public function has(
string $keyname ): bool {
53 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
54 $hasRow = $dbr->newSelectQueryBuilder()
56 ->from( self::TABLE_NAME )
57 ->where( [
'tc_key' => $keyname ] )
58 ->caller( __METHOD__ )
64 public function hasEntryWithTag(
string $tag ): bool {
65 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
66 $hasRow = $dbr->newSelectQueryBuilder()
68 ->from( self::TABLE_NAME )
69 ->where( [
'tc_tag' => $tag ] )
70 ->caller( __METHOD__ )
76 public function set( PersistentCacheEntry ...$entries ): void {
77 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
79 foreach ( $entries as $entry ) {
80 $value = $this->jsonCodec->serialize( $entry->value() );
82 'tc_key' => $entry->key(),
84 'tc_exptime' => $dbw->timestampOrNull( $entry->exptime() ),
85 'tc_tag' => $entry->tag()
90 'tc_exptime' => $dbw->timestampOrNull( $entry->exptime() ),
91 'tc_tag' => $entry->tag()
104 public function setExpiry(
string $keyname,
int $expiryTime ): void {
105 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
108 [
'tc_exptime' => $dbw->timestamp( $expiryTime ) ],
109 [
'tc_key' => $keyname ],
114 public function delete(
string ...$keynames ): void {
115 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
118 [
'tc_key' => $keynames ],
123 public function deleteEntriesWithTag(
string $tag ): void {
124 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
127 [
'tc_tag' => $tag ],
132 public function clear(): void {
133 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
142 private function buildEntries( Iterator $rows ): array {
144 foreach ( $rows as $row ) {
145 $entries[] =
new PersistentCacheEntry(
147 $this->jsonCodec->unserialize( $row->tc_value ),
148 $row->tc_exptime ? (
int)wfTimestamp( TS_UNIX, $row->tc_exptime ) : null,