mrsharko
mrsharko

Reputation: 295

how to inject "memcache" to service.yml?

I installed memcache lib and added it to

framework:  
    session:
        handler_id: session.handler.memcache

but when I trying to use it I get this error

  [Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]  
  You have requested a non-existent service "session.handler.memcache".       

Upvotes: 2

Views: 1862

Answers (2)

Nikita Leshchev
Nikita Leshchev

Reputation: 1844

Symfony has it's own component: https://symfony.com/doc/current/components/cache.html

You have to configure it first in your /config/packages/framework.yaml:

framework:
  cache:
    pools:
      memcached_service:
        adapter: cache.adapter.memcached
        public: true
        provider: 'memcached://memcached:11211'

Now you can inject your Memcached service wherever you want (services.yaml):

App\Service\SomeService:
  arguments:
    - "@memcached_service"

Upvotes: 1

Mateusz Sip
Mateusz Sip

Reputation: 1280

You want to use memcache or memcached? These are two different extensions, so be aware of that. And I suggest to use memcached, memcache is dead.

Serivce session.handler.memcache is not defined, so you have to define one implementing SessionHandlerInterface, in your case MemcacheSessionHandler.

First, we need to define memcache instance as a service, so we can pass it to MemcacheSessionHandler constructor:

memcache:
    class: \Memcache
calls:
    - [ addServer, [ %host_parameter%, %port_parameter% ]]

Then, your session handler:

session.handler.memcache:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
        arguments: [@memcache]

You can also use a bundle like cache/adapter-bundle to register a PSR-6 compatible service (or even a symfony cache component, introduced in 3.1) and use Psr6SessionHandler.

If you want to use memcached, it's almost the same configuration-wise.

Upvotes: 1

Related Questions