17 private const VIRTUAL_DOMAIN =
'virtual-translate';
18 private const TABLE_NAME =
'translate_cache';
19 private IConnectionProvider $dbProvider;
20 private JsonCodec $jsonCodec;
22 public function __construct( IConnectionProvider $dbProvider, JsonCodec $jsonCodec ) {
23 $this->dbProvider = $dbProvider;
24 $this->jsonCodec = $jsonCodec;
28 public function get(
string ...$keynames ): array {
29 $dbr = $this->dbProvider->getReplicaDatabase( self::VIRTUAL_DOMAIN );
30 $rows = $dbr->newSelectQueryBuilder()
31 ->select( [
'tc_key',
'tc_value',
'tc_exptime',
'tc_tag' ] )
32 ->from( self::TABLE_NAME )
33 ->where( [
'tc_key' => $keynames ] )
34 ->caller( __METHOD__ )
37 return $this->buildEntries( $rows );
41 public function getByTag(
string $tag ): array {
42 $dbr = $this->dbProvider->getReplicaDatabase( self::VIRTUAL_DOMAIN );
43 $rows = $dbr->newSelectQueryBuilder()
44 ->select( [
'tc_key',
'tc_value',
'tc_exptime',
'tc_tag' ] )
45 ->from( self::TABLE_NAME )
46 ->where( [
'tc_tag' => $tag ] )
47 ->caller( __METHOD__ )
50 return $this->buildEntries( $rows );
53 public function has(
string $keyname ): bool {
54 $dbr = $this->dbProvider->getReplicaDatabase( self::VIRTUAL_DOMAIN );
55 $hasRow = $dbr->newSelectQueryBuilder()
57 ->from( self::TABLE_NAME )
58 ->where( [
'tc_key' => $keyname ] )
59 ->caller( __METHOD__ )
65 public function hasEntryWithTag(
string $tag ): bool {
66 $dbr = $this->dbProvider->getReplicaDatabase( self::VIRTUAL_DOMAIN );
67 $hasRow = $dbr->newSelectQueryBuilder()
69 ->from( self::TABLE_NAME )
70 ->where( [
'tc_tag' => $tag ] )
71 ->caller( __METHOD__ )
77 public function set( PersistentCacheEntry ...$entries ): void {
78 $dbw = $this->dbProvider->getPrimaryDatabase( self::VIRTUAL_DOMAIN );
80 foreach ( $entries as $entry ) {
81 $value = $this->jsonCodec->serialize( $entry->value() );
82 $dbw->newInsertQueryBuilder()
83 ->insertInto( self::TABLE_NAME )
85 'tc_key' => $entry->key(),
87 'tc_exptime' => $dbw->timestampOrNull( $entry->exptime() ),
88 'tc_tag' => $entry->tag()
90 ->onDuplicateKeyUpdate()
91 ->uniqueIndexFields( [
'tc_key' ] )
94 'tc_exptime' => $dbw->timestampOrNull( $entry->exptime() ),
95 'tc_tag' => $entry->tag()
97 ->caller( __METHOD__ )
102 public function setExpiry(
string $keyname,
int $expiryTime ): void {
103 $dbw = $this->dbProvider->getPrimaryDatabase( self::VIRTUAL_DOMAIN );
104 $dbw->newUpdateQueryBuilder()
105 ->update( self::TABLE_NAME )
106 ->set( [
'tc_exptime' => $dbw->timestamp( $expiryTime ) ] )
107 ->where( [
'tc_key' => $keyname ] )
108 ->caller( __METHOD__ )
112 public function delete(
string ...$keynames ): void {
113 $dbw = $this->dbProvider->getPrimaryDatabase( self::VIRTUAL_DOMAIN );
114 $dbw->newDeleteQueryBuilder()
115 ->deleteFrom( self::TABLE_NAME )
116 ->where( [
'tc_key' => $keynames ] )
117 ->caller( __METHOD__ )
121 public function deleteEntriesWithTag(
string $tag ): void {
122 $dbw = $this->dbProvider->getPrimaryDatabase( self::VIRTUAL_DOMAIN );
123 $dbw->newDeleteQueryBuilder()
124 ->deleteFrom( self::TABLE_NAME )
125 ->where( [
'tc_tag' => $tag ] )
126 ->caller( __METHOD__ )
130 public function clear(): void {
131 $dbw = $this->dbProvider->getPrimaryDatabase( self::VIRTUAL_DOMAIN );
132 $dbw->newDeleteQueryBuilder()
133 ->deleteFrom( self::TABLE_NAME )
135 ->caller( __METHOD__ )
140 private function buildEntries( Iterator $rows ): array {
142 foreach ( $rows as $row ) {
143 $entries[] =
new PersistentCacheEntry(
145 $this->jsonCodec->deserialize( $row->tc_value ),
146 $row->tc_exptime ? (
int)wfTimestamp( TS_UNIX, $row->tc_exptime ) : null,