Skip to content

Commit

Permalink
Merge pull request #10 from RaubJo/master
Browse files Browse the repository at this point in the history
Update to allow setting of cache key and subkey
  • Loading branch information
dalabarge authored Jan 13, 2025
2 parents 1d89854 + 612f721 commit e6c71ee
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,10 +995,10 @@ class MostPopularPosts extends Query implements Cacheable
}
```

You can also dynamically call `->ttl($seconds)` on the query builder to customize
the TTL of the cache results when querying. You can customize `public $key` property
to set a custom key for the query but by default the value will be generated based
on a hash of the query itself. This makes unique queries cacheable under separate
You can also dynamically call `->ttl($seconds)` on the query builder to customize the TTL of the
cache results when querying. You can customize `public $key` property or call `->key($key)` and
optionally `->subKey($subKey)` to set a custom key for the query but by default the value will be
generated based on a hash of the query itself. This makes unique queries cacheable under separate
auto-generated keys.

### How to Bust a Cached Query
Expand Down
44 changes: 42 additions & 2 deletions src/Buses/Cached.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,46 @@ public function ttl(int $ttl = null)
return $this;
}

/**
* Get or set the cache key.
*
* @param string|null $key
*
* @return string|self
*/
public function key(string $key = null)
{
$runnable = $this->toBase();

if (is_null($key)) {
return (string) $runnable->key;
}

$runnable->key = $key;

return $this;
}

/**
* Get or set the cache sub key.
*
* @param string|null $subkey
*
* @return string|self
*/
public function subkey(string $subkey = null)
{
$runnable = $this->toBase();

if (is_null($subkey)) {
return (string) $runnable->subkey;
}

$runnable->subkey = $subkey;

return $this;
}

/**
* Run the query outside the cache.
*
Expand Down Expand Up @@ -298,7 +338,7 @@ protected function tags(): array
*
* @return string
*/
protected function key(): string
protected function getKey(): string
{
$runnable = $this->toBase();

Expand All @@ -314,7 +354,7 @@ protected function key(): string
*
* @return string
*/
protected function subkey(): string
protected function getSubkey(): string
{
$runnable = $this->toBase();

Expand Down

0 comments on commit e6c71ee

Please sign in to comment.