Skip to content

Commit ab03317

Browse files
committed
Add SQLite cache provider
1 parent ca53a2e commit ab03317

11 files changed

+473
-164
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ phpcs.xml
2121
.idea/**/.name
2222
.idea/**/codeStyles
2323
.idea/**/php.xml
24+
25+
## Tests
26+
tests/assets/test-sqlite.db

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
"phpcompatibility/phpcompatibility-wp": "^2.1",
4141
"overtrue/phplint": "^3.4"
4242
},
43+
"suggest": {
44+
"ext-pdo": "PDO is required to use the SQLite cache provider"
45+
},
4346
"scripts": {
4447
"suite": [
4548
"composer test",

src/Cache/ArrayCacheProvider.php

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
*
1515
* @since $ver$
1616
*/
17-
final class ArrayCacheProvider implements CacheProvider {
17+
final class ArrayCacheProvider extends BaseCacheProvider {
1818
/**
1919
* The cached items.
2020
*
2121
* @since $ver$
2222
*
2323
* @var CacheItem[]
2424
*/
25-
private array $items;
25+
private array $items = [];
2626

2727
/**
2828
* Contains the reference to the tags with their tagged cache keys.
@@ -33,26 +33,17 @@ final class ArrayCacheProvider implements CacheProvider {
3333
*/
3434
private array $tags = [];
3535

36-
/**
37-
* The clock instance.
38-
*
39-
* @since $ver$
40-
*
41-
* @var Clock
42-
*/
43-
private Clock $clock;
44-
4536
/**
4637
* Creates an Array cache provider.
4738
*
4839
* @since $ver$
4940
*
50-
* @param Clock|null $clock The clock instance.
51-
* @param CacheItem[] $items The pre-filled cache items.
41+
* @param Clock|null $clock The clock instance.
5242
*/
53-
public function __construct( ?Clock $clock = null, array $items = [] ) {
43+
public function __construct( ?Clock $clock = null ) {
44+
parent::__construct( $clock );
45+
5446
$this->clock = $clock ?? new SystemClock();
55-
$this->items = $items;
5647
}
5748

5849
/**
@@ -74,41 +65,6 @@ public function set( string $key, $value, ?int $ttl = null, array $tags = [] ):
7465
$this->add_tags( $key, $tags );
7566
}
7667

77-
/**
78-
* @inheritDoc
79-
*
80-
* @since $ver$
81-
*/
82-
public function get( string $key, $fallback = null ) {
83-
$item = $this->items[ $key ] ?? null;
84-
85-
if ( ! $item || $item->is_expired( $this->clock ) ) {
86-
unset( $this->items[ $key ] );
87-
88-
return $fallback;
89-
}
90-
91-
return $item->value();
92-
}
93-
94-
/**
95-
* @inheritDoc
96-
*
97-
* @since $ver$
98-
*/
99-
public function has( string $key ): bool {
100-
if (
101-
isset( $this->items[ $key ] )
102-
&& ! $this->items[ $key ]->is_expired( $this->clock )
103-
) {
104-
return true;
105-
}
106-
107-
unset( $this->items[ $key ] );
108-
109-
return false;
110-
}
111-
11268
/**
11369
* @inheritDoc
11470
*
@@ -168,4 +124,13 @@ private function add_tags( string $key, array $tags ): void {
168124
$this->tags[ $tag ] = array_unique( array_merge( $this->tags[ $tag ], [ $key ] ) );
169125
}
170126
}
127+
128+
/**
129+
* @inheritDoc
130+
*
131+
* @since $ver$
132+
*/
133+
protected function doGet( string $key ): ?CacheItem {
134+
return $this->items[ $key ] ?? null;
135+
}
171136
}

src/Cache/BaseCacheProvider.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace DataKit\DataViews\Cache;
4+
5+
use DataKit\DataViews\Clock\Clock;
6+
use DataKit\DataViews\Clock\SystemClock;
7+
8+
/**
9+
* An abstract cache provider that implements default logic.
10+
*
11+
* @since $ver$
12+
*/
13+
abstract class BaseCacheProvider implements CacheProvider {
14+
/**
15+
* The clock.
16+
*
17+
* @since $ver$
18+
* @var Clock
19+
*/
20+
protected Clock $clock;
21+
22+
/**
23+
* Creates the base cache provider.
24+
*
25+
* @since $ver$
26+
*
27+
* @param Clock|null $clock The clock.
28+
*/
29+
public function __construct( ?Clock $clock = null ) {
30+
$this->clock = $clock ?? new SystemClock();
31+
}
32+
33+
/**
34+
* Returns the {@see CacheItem} if found by key.
35+
*
36+
* @param string $key The key.
37+
*
38+
* @return CacheItem|null The cache item.
39+
*/
40+
abstract protected function doGet( string $key ): ?CacheItem;
41+
42+
/**
43+
* @inheritDoc
44+
* @since $ver$
45+
*/
46+
public function get( string $key, $fallback = null ) {
47+
$item = $this->doGet( $key );
48+
if ( ! $item || $item->is_expired( $this->clock->now() ) ) {
49+
$this->delete( $key );
50+
51+
return $fallback;
52+
}
53+
54+
return $item->value();
55+
}
56+
57+
/**
58+
* @inheritDoc
59+
* @since $ver$
60+
*/
61+
public function has( string $key ): bool {
62+
$item = $this->doGet( $key );
63+
64+
if ( ! $item || $item->is_expired( $this->clock->now() ) ) {
65+
$this->delete( $key );
66+
67+
return false;
68+
}
69+
70+
return true;
71+
}
72+
}

src/Cache/CacheItem.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace DataKit\DataViews\Cache;
44

5-
use DataKit\DataViews\Clock\Clock;
6-
use DateTime;
5+
use DateTimeImmutable;
76
use DateTimeInterface;
87

98
/**
@@ -119,16 +118,16 @@ public function tags(): array {
119118
/**
120119
* Returns whether the cache item is expired.
121120
*
122-
* @param Clock $clock The clock to test expiration.
121+
* @param DateTimeImmutable $now The date time to test against.
123122
*
124123
* @return bool Whether the cache item is expired.
125124
*/
126-
public function is_expired( Clock $clock ): bool {
125+
public function is_expired( DateTimeImmutable $now ): bool {
127126
if ( null === $this->expires_at ) {
128127
return false;
129128
}
130129

131-
return $clock->now() > $this->expires_at;
130+
return $now > $this->expires_at;
132131
}
133132

134133
/**

0 commit comments

Comments
 (0)