在我的Symfony 5应用程序中,我想对不同的任务使用不同的缓存,当然,对于不同的环境也使用不同的缓存。
我的配置如下所示:
framework:
cache:
pools:
cache.auth:
adapter: cache.adapter.redis
provider: app.my_custom_redis_provider
cache.sap:
adapter: cache.adapter.redis
provider: app.my_custom_redis_provider
services:
app.my_custom_redis_provider:
arguments:
- '%env(REDIS_URL)%'
-
retry_interval: 2
timeout: '%env(REDIS_TIMEOUT)%'
class: Redis
factory:
- Symfony\Component\Cache\Adapter\RedisAdapter
- createConnection我在类中使用依赖注入。那么,我将如何定义特定类使用这两个缓存池中的哪个?
现在我像这样得到了我的缓存:
class SapListService implements ListServiceInterface
{
use LoggerTrait;
private CountryServiceInterface $countryService;
private CurrencyServiceInterface $currencyService;
public function __construct(
SapClientFactoryInterface $sapClient,
CacheItemPoolInterface $cache,
ParameterBagInterface $params
) {
$sapUser = $params->get('sap_user');
$sapPw = $params->get('sap_pw');
$urlStruktur = $params->get('sap_struktur');
$this->countryService = $sapClient->getCountryService($sapUser, $sapPw, $urlStruktur, $cache);
$this->currencyService = $sapClient->getCurrencyService($sapUser, $sapPw, $urlStruktur, $cache);
} 如何配置我的SapListService以使用cache.sap池?
发布于 2021-05-26 11:52:01
这在文档上得到了解释。
每个自定义池成为一个服务,其服务ID是池的名称(例如,custom_thing.cache)。还使用camel名称的camel版本为每个池创建了一个自动连接别名--例如,可以通过命名参数custom_thing.cache并键入--使用Symfony\Contracts\Cache\CacheInterface或Psr\Cache\CacheItemPoolInterface来自动注入它:
所以在你的情况下,你可以输入提示:
Psr\Cache\CacheItemPoolInterface $cacheSaphttps://stackoverflow.com/questions/67703845
复制相似问题