I browsed the internet and didn’t find much information on how to use any caching library with Slim framework 3.
Can anyone help me with this issue?
I browsed the internet and didn’t find much information on how to use any caching library with Slim framework 3.
Can anyone help me with this issue?
2
Answers
I use
symfony/cache
with Slim 3. You can use any other cache library, but I give an example setup for this specific library. And I should mention, this is actually independent of Slim or any other framework.First you need to include this library in your project, I recommend using composer. I also will iinclude
predis/predis
to be able to use Redis adapter:composer require symfony/cache predis/predis
Then I’ll use Dependency Injection Container to setup cache pool to make it available to other objects which need to use caching features:
Now you have a cache item pool in
$container['cache']
which has methods defined in PSR-6.Here is a sample code using it:
Now when you want to create new instance of SampleClass you should pass this cache item pool from the DIC, for example in a route callback: