Skip to content

Commit cac6ce2

Browse files
committed
WIP
1 parent 558434b commit cac6ce2

3 files changed

Lines changed: 119 additions & 5 deletions

File tree

src/Federation/EntityCollection/CacheEntityCollectionStore.php

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
class CacheEntityCollectionStore implements EntityCollectionStoreInterface
1313
{
14-
protected const PREFIX = 'federation_entities';
14+
protected const KEY_FEDERATED_ENTITIES = 'federation_entities';
15+
16+
protected const KEY_LAST_UPDATED = 'last_updated';
1517

1618

1719
public function __construct(
@@ -22,13 +24,16 @@ public function __construct(
2224
}
2325

2426

27+
/**
28+
* @inheritDoc
29+
*/
2530
public function store(string $trustAnchorId, array $entities, int $ttl): void
2631
{
2732
try {
2833
$this->cacheDecorator->set(
2934
$this->helpers->json()->encode($entities),
3035
$ttl,
31-
self::PREFIX,
36+
self::KEY_FEDERATED_ENTITIES,
3237
$trustAnchorId,
3338
);
3439
} catch (Throwable $throwable) {
@@ -41,11 +46,14 @@ public function store(string $trustAnchorId, array $entities, int $ttl): void
4146
}
4247

4348

49+
/**
50+
* @inheritDoc
51+
*/
4452
public function get(string $trustAnchorId): ?array
4553
{
4654
try {
4755
/** @var ?string $cached */
48-
$cached = $this->cacheDecorator->get(null, self::PREFIX, $trustAnchorId);
56+
$cached = $this->cacheDecorator->get(null, self::KEY_FEDERATED_ENTITIES, $trustAnchorId);
4957

5058
if (is_null($cached)) {
5159
return null;
@@ -69,15 +77,79 @@ public function get(string $trustAnchorId): ?array
6977
}
7078

7179

80+
/**
81+
* @inheritDoc
82+
*/
7283
public function clear(string $trustAnchorId): void
7384
{
7485
try {
75-
$this->cacheDecorator->delete(self::PREFIX, $trustAnchorId);
86+
$this->cacheDecorator->delete(self::KEY_FEDERATED_ENTITIES, $trustAnchorId);
7687
} catch (Throwable $throwable) {
7788
$this->logger?->error('Unable to clear entities from cache.', [
7889
'trustAnchorId' => $trustAnchorId,
7990
'exception_message' => $throwable->getMessage(),
8091
]);
8192
}
8293
}
94+
95+
96+
/**
97+
* @inheritDoc
98+
*/
99+
public function storeLastUpdated(string $trustAnchorId, int $timestamp, int $ttl): void
100+
{
101+
try {
102+
$this->cacheDecorator->set(
103+
(string)$timestamp,
104+
$ttl,
105+
self::KEY_LAST_UPDATED,
106+
$trustAnchorId,
107+
);
108+
} catch (Throwable $throwable) {
109+
$this->logger?->error('Unable to store last updated timestamp in cache.', [
110+
'trustAnchorId' => $trustAnchorId,
111+
'timestamp' => $timestamp,
112+
'exception_message' => $throwable->getMessage(),
113+
]);
114+
}
115+
}
116+
117+
118+
/**
119+
* @inheritDoc
120+
*/
121+
public function getLastUpdated(string $trustAnchorId): ?int
122+
{
123+
try {
124+
$lastUpdated = $this->cacheDecorator->get(null, self::KEY_LAST_UPDATED, $trustAnchorId);
125+
} catch (Throwable $throwable) {
126+
$this->logger?->error('Unable to retrieve last updated timestamp from cache.', [
127+
'trustAnchorId' => $trustAnchorId,
128+
'exception_message' => $throwable->getMessage(),
129+
]);
130+
return null;
131+
}
132+
133+
if (is_int($lastUpdated)) {
134+
return $lastUpdated;
135+
}
136+
137+
return null;
138+
}
139+
140+
141+
/**
142+
* @inheritDoc
143+
*/
144+
public function clearLastUpdated(string $trustAnchorId): void
145+
{
146+
try {
147+
$this->cacheDecorator->delete(self::KEY_LAST_UPDATED, $trustAnchorId);
148+
} catch (Throwable $throwable) {
149+
$this->logger?->error('Unable to clear last updated timestamp from cache.', [
150+
'trustAnchorId' => $trustAnchorId,
151+
'exception_message' => $throwable->getMessage(),
152+
]);
153+
}
154+
}
83155
}

src/Federation/EntityCollection/EntityCollectionStoreInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,25 @@ public function get(string $trustAnchorId): ?array;
3030
* @param non-empty-string $trustAnchorId
3131
*/
3232
public function clear(string $trustAnchorId): void;
33+
34+
35+
/**
36+
* Set the last update timestamp for a given trust anchor.
37+
*
38+
* @param non-empty-string $trustAnchorId
39+
*/
40+
public function storeLastUpdated(string $trustAnchorId, int $timestamp, int $ttl): void;
41+
42+
43+
/**
44+
* Get the last update timestamp for a given trust anchor.
45+
* @param non-empty-string $trustAnchorId
46+
*/
47+
public function getLastUpdated(string $trustAnchorId): ?int;
48+
49+
50+
/**
51+
* Clear the last update timestamp for a given trust anchor.
52+
*/
53+
public function clearLastUpdated(string $trustAnchorId): void;
3354
}

src/Federation/EntityCollection/InMemoryEntityCollectionStore.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
class InMemoryEntityCollectionStore implements EntityCollectionStoreInterface
88
{
99
/** @var array<string, array{entities: array<string, array<string, mixed>>, expires: int}> */
10-
private array $store = [];
10+
protected array $store = [];
11+
12+
/** @var array<string, int> */
13+
protected array $lastUpdatedStore = [];
1114

1215

1316
public function store(string $trustAnchorId, array $entities, int $ttl): void
@@ -38,4 +41,22 @@ public function clear(string $trustAnchorId): void
3841
{
3942
unset($this->store[$trustAnchorId]);
4043
}
44+
45+
46+
public function storeLastUpdated(string $trustAnchorId, int $timestamp, int $ttl): void
47+
{
48+
$this->lastUpdatedStore[$trustAnchorId] = $timestamp;
49+
}
50+
51+
52+
public function getLastUpdated(string $trustAnchorId): ?int
53+
{
54+
return $this->lastUpdatedStore[$trustAnchorId] ?? null;
55+
}
56+
57+
58+
public function clearLastUpdated(string $trustAnchorId): void
59+
{
60+
unset($this->lastUpdatedStore[$trustAnchorId]);
61+
}
4162
}

0 commit comments

Comments
 (0)