Simple configurable trait to add caching per-method.
class Controller
{
use SeanJA\Cache\CacheableTrait;
public function __construct(CacheItemPoolInterface $cache)
{
$this->setCache($cache);
}
}
By default, this value will be remembered for 1 hour
public function cacheableMethod( $cacheable_parameters )
{
$data = $this->remember(function(){
return 'Cacheable data';
});
return response($data);
}
Add a protected method called getTTL
to your class that returns a custom date interval based on the input
protected function getTTL(string $method, array $args): DateInterval
{
return match ($method) {
'method1' => DateInterval::createFromDateString('1 day'),
'method2' => DateInterval::createFromDateString('10 seconds'),
'method3' => DateInterval::createFromDateString('10 seconds'),
default => DateInterval::createFromDateString('6 minutes'),
};
}
You can change the way the key is generated, it should be unique for each place you use remember otherwise you will end up overwriting things in weird ways
protected function generateCacheKey(string $class, string $method, array $args): string
{
return 'key';
}
Add a custom value to the cache, can be used to bust the cache when you do a deploy, or you could set it manually to bust the cache at any point.
protected function getCacheId(): string
{
return $_ENV['RELEASE_VERSION'];
}
Can be used to avoid caching certain method calls
protected function shouldCache(string $method, array $args): bool
{
return $method === 'maybeCache', $args[0] === 'plz cache';
}
This will disable caching for the class until you restore it
public function shouldDisableCaching(): void
{
$this->disableCache();
}
If you really want to
class CachedClass{
use \SeanJA\Cache\CacheableTrait;
public function cachedTime(){
return $this->remember(function(){
return time();
});
}
public function uncachedTime(): void
{
$this->disableCache();
$data = $this->cachedTime();
$this->restoreCache();
return $data;
}
}