mrsharko
mrsharko

Reputation: 295

how to use Memcache with symfony

I using symfony 2 and I want to use Memcache with it but the problem is I can't find any explain for Memcache I just found for memcached so are they the same setup steps ? I added this lines to install Memcache on symfony?

config.yml

framework:
  session:
    handler_id: session.handler.memcached

for parameters.yml

parameters:   
  memcached_host: 127.0.0.1
  memcached_port: 11211
  memcached_prefix: custom_key_
  memcached_expire: 14400

services.yml

services:
  session.handler.memcached:
    class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler
    arguments: [ "@session.memcached", { prefix: '%memcached_prefix%', expiretime: '%memcached_expire%' } ]


services:
  session.memcached:
    class: Memcached
    arguments:
      persistent_id: %memcached_prefix%
    calls:
      - [ addServer, [ %memcached_host%, %memcached_port% ]]



services:
  session.memcached:
    class: Madisoft\AppBundle\Utils\MemcachedWrapper
    arguments:
      persistent_id: '%memcached_prefix%'
    calls:
      - [ addServer, [ '%memcached_host%', '%memcached_port%' ] ]

Upvotes: 8

Views: 13551

Answers (2)

Strabek
Strabek

Reputation: 2511

I'm usinf Symfony 3.4 and here is my config.yml:

parameters:
    session_memcached_host: localhost
    session_memcached_port: 11211
    session_memcached_prefix: sess
    session_memcached_expire: 100 # this is in seconds

framework:
    session:
        handler_id: session.handler.memcached

and services.yml:

services:
    session.memcached:
        class: Memcached
        arguments:
            # Just remember to comment out below line or even remove whole arguments section
            # persistent_id: %session_memcached_prefix%
        calls:
            - [ addServer, [ %session_memcached_host%, %session_memcached_port% ]]

    session.handler.memcached:
        class:     Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler
        arguments: ["@session.memcached", { prefix: %session_memcached_prefix%, expiretime: %session_memcached_expire% }]

With above configuration I store sessions in Memcached server (to be precise AWS ElasticCache Memcached).

If you want to play with Memcached, here is what I use to store my custom data:

use Symfony\Component\Cache\Adapter\MemcachedAdapter;

$client = MemcachedAdapter::createConnection('memcached://localhost');
$cache  = new MemcachedAdapter($client, $namespace = '', $defaultLifetime = 0);
$item   = $cache->getItem('itemName');

if (!$item->isHit()) {
    // Item does not exist
    $item
        ->set($data)
        ->expiresAfter(10) // in seconds
    ;
    $cache->save($item);
} else {
    // Item exists
    $cachedItem = $item->get();
}

I hope this will help.

Upvotes: 0

Francesco Abeni
Francesco Abeni

Reputation: 4265

There is only one Memcached software, and it's the one available at https://memcached.org/.

There are two well-known PHP libraries for Memcached, called memcache (http://php.net/manual/en/book.memcache.php) and memcached (http://php.net/manual/en/book.memcached.php), so this is probably where your confusion comes from.

To use Memcached with Symfony 2 I suggest to use an external bundle by LeaseWeb which provides all the required documentation: https://github.com/LeaseWeb/LswMemcacheBundle.

Starting with Symfony 3.3 there will be a native Memcached adapter: see http://symfony.com/blog/new-in-symfony-3-3-memcached-cache-adapter.

Upvotes: 11

Related Questions