Caching
Tempest comes with a simple wrapper around PSR-6, meaning you can use all PSR-6 compliant cache adapters. Tempest uses Symfony's Cache Component as a default implementation.
Configuration#
Tempest will use file caching by default. You can however configure another adapter via CacheConfig
:
// Config/cache.php use Tempest\Cache\CacheConfig; use Symfony\Component\Cache\Adapter\FilesystemAdapter; return new CacheConfig( pool: new FilesystemAdapter( namespace: '', defaultLifetime: 0, directory: __DIR__ . '/../../../../.cache', ), );
Note that CacheConfig
is used for the default cache. You can create your own caches with different adapters like so:
use Tempest\Cache\Cache; use Tempest\Cache\IsCache; use Symfony\Component\Cache\Adapter\RedisAdapter; #[Singleton] final readonly class RedisCache implements Cache { use IsCache; protected function getCachePool(): CacheItemPoolInterface { return new RedisAdapter(/* … */) } }
Caching stuff#
In order to cache stuff, you only need to inject the Cache
interface (or your custom implementations), and you're ready to go:
final readonly class RssController { public function __construct( private Cache $cache ) {} public function __invoke(): Response { $rss = $this->cache->resolve( key: 'rss', cache: function () { return file_get_contents('https://stitcher.io/rss') }, expiresAt: (new DateTimeImmutable())->add(new DateInterval('P1D')) ) } }
If you need more fine-grained control, the Cache
interface has the following methods:
resolve(string $key, Closure $cache, ?DateTimeInterface $expiresAt = null): mixed
— to retrieve an item from cache, it'll be cached automatically if it wasn't yet.put(string $key, mixed $value, ?DateTimeInterface $expiresAt = null): CacheItemInterface
— to get an item from cache. Note that this method will always returnCacheItemInterface
, even if there wasn't a hit (thanks PSR-6!). Use$item->isHit()
to know whether it's valid or not.get(string $key): mixed
— get an item from cache, returnsnull
if there wasn't a hit.remove(string $key): void
— removes an item from cache.clear(): void
— clears the cache in full.
Clearing caches#
Tempest comes with a cache:clear
command built-in which allows you to pick which caches you want to clear:
./tempest cache:clear Which caches do you want to clear? > [ ] Tempest\Cache\GenericCache > [ ] …
If you want to clear all caches, you can use the --all
flag:
./tempest cache:clear --all Tempest\Cache\GenericCache cleared successfully … cleared successfully Done