如何将 "memcache" 注入到 service.yml?
how to inject "memcache" to service.yml?
我安装了内存缓存库并将其添加到
framework:
session:
handler_id: session.handler.memcache
但是当我尝试使用它时出现此错误
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
You have requested a non-existent service "session.handler.memcache".
您想使用 memcache
还是 memcached
?
这是两个不同的扩展名,因此请注意这一点。
而且我建议用memcached
,memcache
死了。
服务 session.handler.memcache
未定义,因此您必须定义一个实现 SessionHandlerInterface
,在您的情况下 MemcacheSessionHandler
.
首先,我们需要将 memcache 实例定义为服务,这样我们就可以将其传递给 MemcacheSessionHandler
构造函数:
memcache:
class: \Memcache
calls:
- [ addServer, [ %host_parameter%, %port_parameter% ]]
然后,您的会话处理程序:
session.handler.memcache:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
arguments: [@memcache]
你也可以使用像cache/adapter-bundle
这样的bundle来注册PSR-6兼容服务(甚至是symfony缓存组件,在3.1中引入)并使用Psr6SessionHandler
.
如果你想使用memcached
,配置几乎相同。
Symfony 有自己的组件:https://symfony.com/doc/current/components/cache.html
您必须先在 /config/packages/framework.yaml
:
中配置它
framework:
cache:
pools:
memcached_service:
adapter: cache.adapter.memcached
public: true
provider: 'memcached://memcached:11211'
现在您可以在任何地方注入您的 Memcached 服务 (services.yaml
):
App\Service\SomeService:
arguments:
- "@memcached_service"
我安装了内存缓存库并将其添加到
framework:
session:
handler_id: session.handler.memcache
但是当我尝试使用它时出现此错误
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
You have requested a non-existent service "session.handler.memcache".
您想使用 memcache
还是 memcached
?
这是两个不同的扩展名,因此请注意这一点。
而且我建议用memcached
,memcache
死了。
服务 session.handler.memcache
未定义,因此您必须定义一个实现 SessionHandlerInterface
,在您的情况下 MemcacheSessionHandler
.
首先,我们需要将 memcache 实例定义为服务,这样我们就可以将其传递给 MemcacheSessionHandler
构造函数:
memcache:
class: \Memcache
calls:
- [ addServer, [ %host_parameter%, %port_parameter% ]]
然后,您的会话处理程序:
session.handler.memcache:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
arguments: [@memcache]
你也可以使用像cache/adapter-bundle
这样的bundle来注册PSR-6兼容服务(甚至是symfony缓存组件,在3.1中引入)并使用Psr6SessionHandler
.
如果你想使用memcached
,配置几乎相同。
Symfony 有自己的组件:https://symfony.com/doc/current/components/cache.html
您必须先在 /config/packages/framework.yaml
:
framework:
cache:
pools:
memcached_service:
adapter: cache.adapter.memcached
public: true
provider: 'memcached://memcached:11211'
现在您可以在任何地方注入您的 Memcached 服务 (services.yaml
):
App\Service\SomeService:
arguments:
- "@memcached_service"