Skip to content

Commit

Permalink
Prefix keys
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Dec 21, 2024
1 parent 25a2d2b commit f85630f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/Illuminate/Cache/MemoizedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,26 @@ public function get($key)
*/
public function many(array $keys)
{
$results = [];
$memoized = [];
$retrieved = [];
$missing = [];

foreach ($keys as $key) {
if (array_key_exists($key, $this->cache)) {
$results[$key] = $this->cache[$key];
if (array_key_exists($this->prefix($key), $this->cache)) {
$memoized[$key] = $this->cache[$this->prefix($key)];
} else {
$missing[] = $key;
}
}

if (count($missing) > 0) {
$results = [...$results, ...$this->repository->many($missing)];
$retrieved = tap($this->repository->many($missing), $this->memoize(...));
}

$this->cache = [...$this->cache, ...$results];

return $results;
return [
...$memoized,
...$retrieved,
];
}

/**
Expand All @@ -89,7 +91,7 @@ public function putMany(array $values, $seconds)
{
return tap($this->repository->putMany($values, $seconds), function ($result) use ($values) {
if ($result) {
$this->cache = [...$this->cache, ...$values];
$this->memoize($values);
}
});
}
Expand All @@ -105,7 +107,7 @@ public function increment($key, $value = 1)
{
return tap($this->repository->increment($key, $value), function ($result) use ($key) {
if (is_int($result)) {
$this->cache[$key] = (string) $result;
$this->memoize([$key => $result]);
}
});
}
Expand All @@ -121,7 +123,7 @@ public function decrement($key, $value = 1)
{
return tap($this->repository->decrement($key, $value), function ($result) use ($key) {
if (is_int($result)) {
$this->cache[$key] = (string) $result;
$this->memoize([$key => $result]);
}
});
}
Expand All @@ -137,7 +139,7 @@ public function forever($key, $value)
{
return tap($this->repository->forever($key, $value), function ($result) use ($key, $value) {
if ($result) {
$this->cache[$key] = $value;
$this->memoize([$key => $value]);
}
});
}
Expand All @@ -152,7 +154,7 @@ public function forget($key)
{
return tap($this->repository->forget($key), function ($result) use ($key) {
if ($result) {
unset($this->cache[$key]);
unset($this->cache[$this->prefix($key)]);
}
});
}
Expand All @@ -178,10 +180,33 @@ public function flush()
*/
public function getPrefix()
{
// TODO do we need to do anything with this?
// I suppose we should prefix all keys with this. Someone could
// dynamically change the prefix midway through the request and then we
// would clash with the previous keys.
return $this->repository->getPrefix();
}

/**
* Prefix the given key.
*
* @param string $key
* @return string
*/
protected function prefix($key)
{
return $this->getPrefix().$key;
}

/**
* Memoized the given values.
*
* @param array $values
* @return void
*/
protected function memoize($values)
{
$this->cache = [
...$this->cache,
...collect($values)->mapWithKeys(fn ($value, $key) => [
$this->prefix($key) => is_null($value) ? $value : (string) $value,
]),
];
}
}
18 changes: 18 additions & 0 deletions tests/Integration/Cache/RedisStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,24 @@ public function testMemoizedDriverGetsPrefix()
$this->assertSame('foo', Cache::memo('redis')->getPrefix());
}

public function testMemoizedKeysArePrefixed()
{
$redis = Cache::store('redis');

$redis->setPrefix('aaaa');
$redis->put('name', 'Tim');
$redis->setPrefix('zzzz');
$redis->put('name', 'Taylor');

$redis->setPrefix('aaaa');
$value = Cache::memo('redis')->get('name');
$this->assertSame('Tim', $value);

$redis->setPrefix('zzzz');
$value = Cache::memo('redis')->get('name');
$this->assertSame('Taylor', $value);
}

public function testItDoesNotDispatchEvents()
{
// TODO
Expand Down

0 comments on commit f85630f

Please sign in to comment.