Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions src/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,29 +867,7 @@ public function set(string $key, mixed $value, mixed $ttl = null): bool
$this->validateKey($key);
$ttlSeconds = $this->normalizeTtl($ttl);

$result = false;
if (method_exists($this->adapter, 'set')) {
try {
$result = $this->adapter->set($key, $value, $ttlSeconds);
} catch (Psr6InvalidArgumentException $e) {
throw new CacheInvalidArgumentException($e->getMessage(), 0, $e);
}
} else {
// Fall back to PSR-6 approach
try {
$item = $this->adapter->getItem($key)->set($value)->expiresAfter($ttlSeconds);
$result = $this->save($item);
} catch (Psr6InvalidArgumentException $e) {
throw new CacheInvalidArgumentException($e->getMessage(), 0, $e);
}
}

if ($result) {
$this->clearTagMeta($key);
$this->metric('set');
}

return (bool) $result;
return $this->store($key, $value, $ttlSeconds);
}

public function setLockProvider(LockProviderInterface $lockProvider): self
Expand Down Expand Up @@ -984,13 +962,13 @@ public function setNamespaceAndDirectory(string $namespace, ?string $dir = null)
public function setTagged(string $key, mixed $value, array $tags, mixed $ttl = null): bool
{
$normalizedTags = $this->normalizeTagList($tags);
$ok = $this->set($key, $value, $ttl);
$this->validateKey($key);
$ttlSeconds = $this->normalizeTtl($ttl);
$ok = $this->store($key, $value, $ttlSeconds);
if (!$ok) {
return false;
}

$ttlSeconds = $this->normalizeTtl($ttl);

return $this->writeTagMeta($key, $normalizedTags, $ttlSeconds);
}

Expand Down Expand Up @@ -1056,8 +1034,9 @@ private function currentTagVersion(string $normalizedTag): int
{
$key = $this->tagVersionKey($normalizedTag);
$item = $this->adapter->getItem($key);
if ($item->isHit() && is_int($item->get()) && $item->get() > 0) {
return $item->get();
$version = $item->isHit() ? $item->get() : null;
if (is_int($version) && $version > 0) {
return $version;
}

$item->set(1)->expiresAfter(null);
Expand Down Expand Up @@ -1206,6 +1185,33 @@ private function stampedeLockKey(string $key): string
return '__im_lock_' . hash('xxh128', $key);
}

private function store(string $key, mixed $value, ?int $ttlSeconds): bool
{
$result = false;
if (method_exists($this->adapter, 'set')) {
try {
$result = $this->adapter->set($key, $value, $ttlSeconds);
} catch (Psr6InvalidArgumentException $e) {
throw new CacheInvalidArgumentException($e->getMessage(), 0, $e);
}
} else {
// Fall back to PSR-6 approach
try {
$item = $this->adapter->getItem($key)->set($value)->expiresAfter($ttlSeconds);
$result = $this->save($item);
} catch (Psr6InvalidArgumentException $e) {
throw new CacheInvalidArgumentException($e->getMessage(), 0, $e);
}
}

if ($result) {
$this->clearTagMeta($key);
$this->metric('set');
}

return (bool) $result;
}

private function tagMetaKey(string $key): string
{
return self::TAG_META_PREFIX . hash('sha256', $key);
Expand Down Expand Up @@ -1246,9 +1252,8 @@ private function writeTagMeta(string $key, array $tags, ?int $ttl): bool
}

$versions = [];
foreach (array_values(array_unique($tags)) as $tag) {
$normalized = $this->normalizeTag((string) $tag);
$versions[$normalized] = $this->currentTagVersion($normalized);
foreach ($tags as $tag) {
$versions[$tag] = $this->currentTagVersion($tag);
}

$metaItem = $this->adapter->getItem($this->tagMetaKey($key));
Expand Down
7 changes: 3 additions & 4 deletions src/Cache/CacheReadRememberTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ public function remember(
mixed $ttl = null,
array $tags = [],
): mixed {
$this->validateKey($key);
$normalizedTtl = $this->normalizeTtl($ttl);
$normalizedTags = $this->normalizeTagList($tags);

try {
$item = $this->getItem($key);
} catch (Psr6InvalidArgumentException $e) {
Expand All @@ -173,6 +169,9 @@ public function remember(
return $lockedItem->get();
}

$normalizedTtl = $this->normalizeTtl($ttl);
$normalizedTags = $this->normalizeTagList($tags);

if ($normalizedTtl !== null) {
$lockedItem->expiresAfter($normalizedTtl);
}
Expand Down