Skip to content

Commit e39059a

Browse files
author
Stephan Wentz
committed
fixup! fix: Add redis storage support, add bundle and deprecate brainbits/blocking-bundle, require psr/clock, rework expiration to ttl mechanism implemented by storage
1 parent d72306c commit e39059a

5 files changed

Lines changed: 11 additions & 13 deletions

File tree

src/Blocker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function tryBlock(BlockIdentity $identifier, int|null $ttl = null): Block
4949
return null;
5050
}
5151

52-
$this->storage->touch($block);
52+
$this->storage->touch($block, $ttl ?? $this->defaultTtl);
5353

5454
return $block;
5555
}

src/Storage/FilesystemStorage.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function write(Block $block, int $ttl): bool
7878
return true;
7979
}
8080

81-
public function touch(Block $block): bool
81+
public function touch(Block $block, int $ttl): bool
8282
{
8383
$identity = $block->getIdentity();
8484

@@ -89,13 +89,10 @@ public function touch(Block $block): bool
8989
$filename = $this->getFilename($block->getIdentity());
9090
$metaFilename = $filename . '.meta';
9191

92-
$metaContent = file_get_contents($metaFilename);
93-
assert(is_string($metaContent));
94-
assert($metaContent !== '');
95-
$metaData = json_decode($metaContent, true);
96-
assert(is_array($metaData));
97-
$metaData['updatedAt'] = $this->clock->now()->format('c');
98-
$metaContent = json_encode($metaData);
92+
$metaContent = json_encode([
93+
'ttl' => $ttl,
94+
'updatedAt' => $this->clock->now()->format('c'),
95+
]);
9996

10097
if (file_put_contents($metaFilename, $metaContent) === false) {
10198
throw IOException::writeFailed($metaFilename);

src/Storage/InMemoryStorage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ public function write(Block $block, int $ttl): bool
5252
return true;
5353
}
5454

55-
public function touch(Block $block): bool
55+
public function touch(Block $block, int $ttl): bool
5656
{
5757
if (!$this->exists($block->getIdentity())) {
5858
return false;
5959
}
6060

61+
$this->blocks[(string) $block->getIdentity()]['ttl'] = $ttl;
6162
$this->blocks[(string) $block->getIdentity()]['updatedAt'] = $this->clock->now();
6263

6364
return true;

src/Storage/PredisStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function write(Block $block, int $ttl): bool
5555
return true;
5656
}
5757

58-
public function touch(Block $block): bool
58+
public function touch(Block $block, int $ttl): bool
5959
{
6060
$identity = $block->getIdentity();
6161

@@ -64,7 +64,7 @@ public function touch(Block $block): bool
6464
}
6565

6666
try {
67-
$this->client->touch($this->createKey($identity));
67+
$this->client->expire($this->createKey($identity), $ttl);
6868
} catch (PredisException) {
6969
throw IOException::touchFailed((string) $identity);
7070
}

src/Storage/StorageInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface StorageInterface
2323
{
2424
public function write(Block $block, int $ttl): bool;
2525

26-
public function touch(Block $block): bool;
26+
public function touch(Block $block, int $ttl): bool;
2727

2828
public function remove(Block $block): bool;
2929

0 commit comments

Comments
 (0)