Skip to content

Instantly share code, notes, and snippets.

@danilopinotti
Created September 28, 2022 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilopinotti/571ece51ef113865e2deb7bc148f91ec to your computer and use it in GitHub Desktop.
Save danilopinotti/571ece51ef113865e2deb7bc148f91ec to your computer and use it in GitHub Desktop.
Cache Manager to easily converts TTL and enable/disable custom cache
<?php
namespace App\Support;
use Illuminate\Contracts\Cache\Repository;
class CacheManager
{
private ?string $driver = null;
public function __construct(?string $driver = null)
{
$this->driver = $driver;
}
/**
* Determine the cache driver
*
* @param string $driver
* @return $this
*/
public function driver(string $driver): self
{
return new self($driver);
}
/**
* Returns cache with array driver
*
* @return $this
*/
public function arrayDriver(): self
{
return new self('array');
}
/**
* Returns the cached value by key
*
* @param string|callable $key Cache Key
* @param array $tags Custom tags
* @return mixed
*/
public function get(string|callable $key, array $tags = [])
{
$key = (string) value($key);
return $this->getCache($tags)
->get($key);
}
/**
* Returns the cached value by key and remember the result for a determined TTL
*
* @param callable|string $key Cache Key
* @param string $ttl Time to keep this value in cache e.g. '1 hour'
* @param callable $value Remembered value
* @param array $tags Custom tags
* @return mixed
*
* @see http://php.net/manual/pt_BR/datetime.formats.relative.php
*/
public function remember(
callable|string $key,
string $ttl,
callable $value,
array $tags = []
): mixed {
$applicationCacheIsEnabled = config('cache.enable_application_cache');
if (! $applicationCacheIsEnabled) {
return value($value);
}
$key = (string) value($key);
$ttl = $this->resolveTtl($ttl);
return $this->getCache($tags)
->remember($key, $ttl, $value);
}
/**
* Put the value in cache based on key
*
* @param string|callable $key Cache Key
* @param string $ttl Time to keep this value in cache e.g. '1 hour'
* @param mixed $value Value to be stored
* @param array $tags Custom tags
* @return mixed
*
* @see http://php.net/manual/pt_BR/datetime.formats.relative.php
*/
public function put(
string|callable $key,
string $ttl,
mixed $value,
array $tags = []
) {
$key = (string) value($key);
$ttl = $this->resolveTtl($ttl);
return $this->getCache($tags)
->put($key, value($value), $ttl);
}
/**
* Converts TTL from string to Date Interval
*
* @param string $ttl Time to keep this value in cache e.g. '1 hour'
* @return \DateInterval TTL as DateInterval object
*
* @see http://php.net/manual/pt_BR/datetime.formats.relative.php
*/
private function resolveTtl(string $ttl): \DateInterval
{
return \DateInterval::createFromDateString($ttl);
}
/**
* Returns Laravel native cache already initialized with the correct parameters
*
* @param array $tags
* @return \Illuminate\Contracts\Cache\Repository
*/
private function getCache(array $tags = []): Repository
{
$cache = app('cache')
->driver($this->driver);
return ($tags)
? $cache->tags($tags)
: $cache;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment