Skip to content

Commit

Permalink
Add local cache functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
krazzer committed Oct 2, 2024
1 parent 24b4440 commit 3c933d9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Services/CacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
class CacheService extends Injectable
{
/** @var array */
private array $localCache = [];

/**
* @param string $prefix
*/
Expand Down Expand Up @@ -117,6 +120,22 @@ public function cache(string $cacheKey, callable $function, float|int $ttl = Cac
return $result;
}

/**
* Cache function just to remember locally, use if you call the same function multiple times in one request
*
* @param string $cacheKey
* @param callable $function
* @return mixed
*/
public function cacheLocal(string $cacheKey, callable $function): mixed
{
if( ! array_key_exists($cacheKey, $this->localCache)){
$this->localCache[$cacheKey] = $function();
}

return $this->localCache[$cacheKey];
}

/**
* @param array $args
* @return string
Expand Down

0 comments on commit 3c933d9

Please sign in to comment.