Reputation: 364
As I understand it right, Symfony 4 has its "own" cache instance. I would like to use it in my controllers and services, but fail to do so.
My /config/packages/framework.yaml
looks like:
framework:
secret: '%env(APP_SECRET)%'
#default_locale: en
#csrf_protection: ~
#http_method_override: true
# Enables session support. Note that the session will ONLY [...]
# Remove or comment this section to explicitly disable session support.
session:
handler_id: ~
#esi: ~
#fragments: ~
php_errors:
log: true
cache:
# Put the unique name of your app here: the prefix seed
# is used to compute stable namespaces for cache keys.
# prefix_seed: foobar
# The app cache caches to the filesystem by default.
# Other options include:
# Redis
#app: cache.adapter.redis
#default_redis_provider: redis://localhost
# APCu (not recommended with heavy random-write [...]
#app: cache.adapter.apcu
Got cache related packages in my composer.json:
"require": {
[...]
"sensio/framework-extra-bundle": "^5.1",
"symfony/apache-pack": "^1.0",
"symfony/asset": "^4.0.6",
"symfony/cache": "^4.0.6",
"symfony/console": "^4.0.6",
"symfony/dotenv": "^4.0.6",
"symfony/flex": "^1.0",
"symfony/framework-bundle": "^4.0.6",
"symfony/lts": "^4@dev",
"symfony/monolog-bundle": "^3.1",
"symfony/twig-bridge": "^4.0.6",
"symfony/twig-bundle": "^4.0.6",
"symfony/yaml": "^4.0.6",
"twig/twig": ">2.4"
},
My current workaround is, that I use a custom Cache instance (Filebased atm). To make it work, I determine the environment and build a path to the cache folder myself:
if (isset($_SERVER['env'])) {
$env = $_SERVER['env'];
} elseif (isset($_ENV['env'])) {
$env = $_ENV['env'];
} else {
throw new \Exception('Could not determine environment.');
}
$this->cache = new FilesystemCache('', 0, __DIR__ .'/../../var/cache/'. $env);
Upvotes: 2
Views: 2660
Reputation: 91
Look for cache services,
$ bin/console debug:container | grep cache
cache.app Symfony\Component\Cache\Adapter\TraceableAdapter
And inject it into any controller/service like any other service:
use Symfony\Component\Cache\Adapter\AdapterInterface;
public function __construct(AdapterInterface $cache)
....
(class name in use is little different than in the service list, it is hinted if you try to use Symfony\Component\Cache\Adapter\TraceableAdapter)
Upvotes: 3