据我所知,symfony 4有自己的“缓存”实例。我想在我的控制器和服务中使用它,但没有这样做。
我的/config/packages/framework.yaml
看起来像是:
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
我的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"
},
我目前的解决方法是,我使用自定义缓存实例(Filebased atm)。为了使它工作,我确定环境并自己构建缓存文件夹的路径:
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);
提前感谢您的帮助!
答案 0 :(得分:0)
寻找缓存服务,
$ bin/console debug:container | grep cache
cache.app Symfony\Component\Cache\Adapter\TraceableAdapter
将其注入任何其他服务的控制器/服务中:
use Symfony\Component\Cache\Adapter\AdapterInterface;
public function __construct(AdapterInterface $cache)
....
(使用的类名与服务列表中的名称略有不同,如果您尝试使用Symfony \ Component \ Cache \ Adapter \ TraceableAdapter,则会暗示
)