Symfony - MemcachedAdapter::__construct() 必须是 Memcached 的实例,给定字符串

问题描述 投票:0回答:2

这很奇怪。我已经使用 Memcached 设置了缓存,如下所示:

框架.yaml

framework:
    cache:
        app: app.memcached_adapter

服务.yaml

app.memcached_client:
    class: Memcached
    factory: 'Symfony\Component\Cache\Adapter\MemcachedAdapter::createConnection'
    arguments: [['%app.memcached.dsn.1%', '%app.memcached.dsn.2%']]
    public: true

app.memcached_adapter:
    class: Symfony\Component\Cache\Adapter\MemcachedAdapter
    arguments:
        - '@app.memcached_client'
    public: true

我能够清除终端中的缓存,没有任何错误,但是当我加载网站时,我得到以下信息:

Type error: Argument 1 passed to Symfony\Component\Cache\Adapter\MemcachedAdapter::__construct() 
must be an instance of Memcached, string given, called in 
/users/me/project/please-dont-mention-the-company/sports/var/cache/lcl/ContainerXqbuh45/srcLclDebugProjectContainer.php 
on line 1029

在查看生成的容器类时,我发现了这个:

/**
 * Gets the public 'cache.app' shared service.
 *
 * @return \Symfony\Component\Cache\Adapter\TraceableAdapter
 */
protected function getCache_AppService()
{
    return $this->services['cache.app'] = new \Symfony\Component\Cache\Adapter\TraceableAdapter(new \Symfony\Component\Cache\Adapter\MemcachedAdapter('kRTmoLLARw'));
}

我不知道它是如何以该字符串作为参数生成的,它应该是memcached客户端!

有什么想法吗?我可以检查哪些内容而无需调试容器类生成?

php symfony caching memcached autowired
2个回答
0
投票

您是否尝试过从服务容器获取 CacheAdapter 而不是在此处创建新实例:


return $this->services['cache.app'] = new \Symfony\Component\Cache\Adapter\TraceableAdapter(
new \Symfony\Component\Cache\Adapter\MemcachedAdapter('kRTmoLLARw') // new instance?
);


0
投票

此问题仍然存在。下面的代码修复了它。

services:        
   app.my_custom_memcached_provider:
     class: Memcached
     factory:
       - Symfony\Component\Cache\Adapter\MemcachedAdapter
       - createConnection
     arguments:
        - 'memcached://127.0.0.1:11211'


framework:
    cache:
        pools:
            custom.cache_pool:
                adapter: cache.adapter.memcached
                provider: app.my_custom_memcached_provider
 
    
© www.soinside.com 2019 - 2024. All rights reserved.