如何将当前缓存实例传递到 service/controller?
How can I pass the current cache instance into a service/controller?
据我了解,Symfony 4 有其 "own" 缓存实例。我想在我的控制器和服务中使用它,但没有这样做。
我的 /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"
},
我目前的解决方法是,我使用自定义缓存实例(基于文件的 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);
寻找缓存服务,
$ bin/console debug:container | grep cache
cache.app Symfony\Component\Cache\Adapter\TraceableAdapter
并像任何其他服务一样将其注入任何 controller/service:
use Symfony\Component\Cache\Adapter\AdapterInterface;
public function __construct(AdapterInterface $cache)
....
(class 使用的名称与服务列表中的名称略有不同,如果您尝试使用 Symfony\Component\Cache\Adapter\TraceableAdapter)
据我了解,Symfony 4 有其 "own" 缓存实例。我想在我的控制器和服务中使用它,但没有这样做。
我的 /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"
},
我目前的解决方法是,我使用自定义缓存实例(基于文件的 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);
寻找缓存服务,
$ bin/console debug:container | grep cache
cache.app Symfony\Component\Cache\Adapter\TraceableAdapter
并像任何其他服务一样将其注入任何 controller/service:
use Symfony\Component\Cache\Adapter\AdapterInterface;
public function __construct(AdapterInterface $cache)
....
(class 使用的名称与服务列表中的名称略有不同,如果您尝试使用 Symfony\Component\Cache\Adapter\TraceableAdapter)