Skip to content

Commit

Permalink
fixup! fix: Add redis storage support, add bundle and deprecate brain…
Browse files Browse the repository at this point in the history
…bits/blocking-bundle, require psr/clock, rework expiration to ttl mechanism implemented by storage
  • Loading branch information
Stephan Wentz committed Jul 21, 2023
1 parent d72306c commit e39059a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Blocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function tryBlock(BlockIdentity $identifier, int|null $ttl = null): Block
return null;
}

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

return $block;
}
Expand Down
13 changes: 5 additions & 8 deletions src/Storage/FilesystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function write(Block $block, int $ttl): bool
return true;
}

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

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

$metaContent = file_get_contents($metaFilename);
assert(is_string($metaContent));
assert($metaContent !== '');
$metaData = json_decode($metaContent, true);
assert(is_array($metaData));
$metaData['updatedAt'] = $this->clock->now()->format('c');
$metaContent = json_encode($metaData);
$metaContent = json_encode([
'ttl' => $ttl,
'updatedAt' => $this->clock->now()->format('c'),
]);

if (file_put_contents($metaFilename, $metaContent) === false) {
throw IOException::writeFailed($metaFilename);
Expand Down
3 changes: 2 additions & 1 deletion src/Storage/InMemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ public function write(Block $block, int $ttl): bool
return true;
}

public function touch(Block $block): bool
public function touch(Block $block, int $ttl): bool
{
if (!$this->exists($block->getIdentity())) {
return false;
}

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

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Storage/PredisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function write(Block $block, int $ttl): bool
return true;
}

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

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

try {
$this->client->touch($this->createKey($identity));
$this->client->expire($this->createKey($identity), $ttl);
} catch (PredisException) {
throw IOException::touchFailed((string) $identity);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface StorageInterface
{
public function write(Block $block, int $ttl): bool;

public function touch(Block $block): bool;
public function touch(Block $block, int $ttl): bool;

public function remove(Block $block): bool;

Expand Down

0 comments on commit e39059a

Please sign in to comment.