Skip to content

Commit

Permalink
misc: use xxh128 hash for cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Sep 18, 2024
1 parent 0420306 commit 6d43673
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
46 changes: 27 additions & 19 deletions src/Cache/KeySanitizerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Traversable;

use function hash;
use function strstr;

/**
* @internal
Expand All @@ -21,21 +22,28 @@ final class KeySanitizerCache implements WarmupCache
{
private static string $version;

/** @var Closure(string): string */
private Closure $sanitize;

public function __construct(
/** @var CacheInterface<EntryType> */
private CacheInterface $delegate
) {
// Two things:
// 1. We append the current version of the package to the cache key in
// order to avoid collisions between entries from different versions
// of the library.
// 2. The key is hashed so that it does not contain illegal characters.
// @see https://www.php-fig.org/psr/psr-16/#12-definitions
// @infection-ignore-all
$this->sanitize = static fn (string $key) => $key . hash('sha1', self::$version ??= PHP_VERSION . '/' . Package::version());
) {}

/**
* Two things:
* 1. We append the current version of the package to the cache key in order
* to avoid collisions between entries from different versions of the
* library.
* 2. The key is hashed so that it does not contain illegal characters.
* @see https://www.php-fig.org/psr/psr-16/#12-definitions
*
* @infection-ignore-all
*/
private function sanitize(string $key): string
{
self::$version ??= PHP_VERSION . '/' . Package::version();

$firstPart = strstr($key, "\0", before_needle: true);

return $firstPart . hash('xxh128', $key . self::$version);
}

public function warmup(): void
Expand All @@ -47,17 +55,17 @@ public function warmup(): void

public function get($key, $default = null): mixed
{
return $this->delegate->get(($this->sanitize)($key), $default);
return $this->delegate->get($this->sanitize($key), $default);
}

public function set($key, $value, $ttl = null): bool
{
return $this->delegate->set(($this->sanitize)($key), $value, $ttl);
return $this->delegate->set($this->sanitize($key), $value, $ttl);
}

public function delete($key): bool
{
return $this->delegate->delete(($this->sanitize)($key));
return $this->delegate->delete($this->sanitize($key));
}

public function clear(): bool
Expand All @@ -67,7 +75,7 @@ public function clear(): bool

public function has($key): bool
{
return $this->delegate->has(($this->sanitize)($key));
return $this->delegate->has($this->sanitize($key));
}

/**
Expand All @@ -76,7 +84,7 @@ public function has($key): bool
public function getMultiple($keys, $default = null): Traversable
{
foreach ($keys as $key) {
yield $key => $this->delegate->get(($this->sanitize)($key), $default);
yield $key => $this->delegate->get($this->sanitize($key), $default);
}
}

Expand All @@ -85,7 +93,7 @@ public function setMultiple($values, $ttl = null): bool
$versionedValues = [];

foreach ($values as $key => $value) {
$versionedValues[($this->sanitize)($key)] = $value;
$versionedValues[$this->sanitize($key)] = $value;
}

return $this->delegate->setMultiple($versionedValues, $ttl);
Expand All @@ -96,7 +104,7 @@ public function deleteMultiple($keys): bool
$transformedKeys = [];

foreach ($keys as $key) {
$transformedKeys[] = ($this->sanitize)($key);
$transformedKeys[] = $this->sanitize($key);
}

return $this->delegate->deleteMultiple($transformedKeys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use CuyZ\Valinor\Type\ObjectType;
use Psr\SimpleCache\CacheInterface;

use function hash;

/** @internal */
final class CacheClassDefinitionRepository implements ClassDefinitionRepository
{
Expand All @@ -23,7 +21,7 @@ public function __construct(
public function for(ObjectType $type): ClassDefinition
{
// @infection-ignore-all
$key = 'class-definition' . hash('sha1', $type->toString());
$key = "class-definition-\0" . $type->toString();

$entry = $this->cache->get($key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use CuyZ\Valinor\Utility\Reflection\Reflection;
use Psr\SimpleCache\CacheInterface;

use function hash;

/** @internal */
final class CacheFunctionDefinitionRepository implements FunctionDefinitionRepository
{
Expand All @@ -25,7 +23,7 @@ public function for(callable $function): FunctionDefinition
$reflection = Reflection::function($function);

// @infection-ignore-all
$key = 'function-definition-' . hash('sha1', $reflection->getFileName() . $reflection->getStartLine() . $reflection->getEndLine());
$key = "function-definition-\0" . $reflection->getFileName() . ':' . $reflection->getStartLine() . '-' . $reflection->getEndLine();

$entry = $this->cache->get($key);

Expand Down

0 comments on commit 6d43673

Please sign in to comment.