memcached session in symfony

方法1:php

If you use the full stack Symfony framework, than configuring memcached as a session handler is as simple as specifying it in your php.ini:git

session.save_handler=memcached
session.save_path=localhost:11211

Make sure that the handler_id is set to null (~) in your app/config/config.yml. This way Symfony will use the native php handler:github

framework:
    session:
        # handler_id set to null will use default session handler from php.ini
        handler_id:  ~

Now you can start using your session. It is going to be stored in memcached.session

Accessing the sessionapp

Session can be accessed from the Request object:ide

$request->getSession()->set('name', 'Kuba');

HttpFoundation componentmemcached

The same can be set up outside of a full stack framework with the HttpFoundation component alone:this

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;

$storage = new NativeSessionStorage(array());
$session = new Session($storage, new NamespacedAttributeBag());

$request = Request::createFromGlobals();
$request->setSession($session);

The above snippet sets the session up just like the full stack framework does (by default). This way, the script you'll put this code in, will share the session with a full Symfony application.spa

 

方法2:https://gist.github.com/K-Phoen/4327229.net

Storing Symfony2 sessions in memcached

Raw

 config.yml

  imports:
  # ....
  - { resource: services/session.yml }
   
   
  framework:
  # ....
  session:
  handler_id: session.handler.memcached

Raw

 gistfile1.txt

  aptitude install memcached php5-memcached

Raw

 parameters.yml

  parameters:
  # ...
   
  session_memcached_host: localhost
  session_memcached_port: 11211
  session_memcached_prefix: sess
  session_memcached_expire: 3600

Raw

 session_services.yml

  services:
  session.memcached:
  class: Memcached
  arguments:
  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% }]

 

方法3:https://gist.github.com/benr77/258e42642b4632d5a826#file-memcachedwrapper-php

Memcached wrapper for persistent connections

Raw

 MemcachedWrapper.php

  <?php
   
  namespace ChaletOps\BaseBundle\Utils;
   
  /**
  * Class MemcachedWrapper
  */
  class MemcachedWrapper extends \Memcached
  {
   
  /**
  * @param string $persistentId
  */
  public function __construct($persistentId)
  {
  parent::__construct($persistentId);
  }
   
  /**
  * Prevent adding of new servers as duplicates. We're persistent!
  *
  * @param array $servers
  *
  * @return bool
  */
  public function addServers(array $servers)
  {
  if (0 == count($this->getServerList()))
  {
  return parent::addServers($servers);
  }
   
  return false;
  }
   
  /**
  * Prevent adding of new server as duplicate. We're persistent!
  *
  * @param string $host
  * @param int $port
  * @param int $weight
  *
  * @return bool
  */
  public function addServer($host, $port, $weight = 0)
  {
  foreach ($this->getServerList() as $server)
  {
  if ($server['host'] == $host && $server['port'] == $port)
  {
  return false;
  }
  }
   
  return parent::addServer($host, $port, $weight);
  }
   
  }

 

 

方法4:https://gist.github.com/elmariachi111/5637669a028c29e3973a

Symfony2 Memcached settings for persistent connections

Raw

 AcmeMemcached.php

  <?php
   
  namespace Acme\CoreBundle\Classes\Cache;
   
   
  class AcmeMemcached extends \Memcached {
   
   
  function __construct($persistent_id)
  {
  parent::__construct($persistent_id);
  }
   
  /**
  * prevent adding of new servers. We're persistent!
  */
  public function addServers(array $servers)
  {
  if (0 == count($this->getServerList())) {
  parent::addServers($servers);
  }
  }
   
   
  }

Raw

 config(prod).yml

  services:
  memcache.servers:
  class: Acme\CoreBundle\Classes\Cache\AcmeMemcached
  arguments: [ "acme_mc" ] #persistent id
  calls:
  - [ addServers, [ [ ["10.0.0.1",11211, 50 ], ["10.0.0.2",11211,25], ["10.0.0.3",11211,25] ] ] ]

@adamquaile

adamquaile commented on 8 Jul 2015

I know this is old, but for anyone stumbling across this. Just be aware there is also a singular addServer method you may want to override too. Otherwise it's still possible to end up with multiple open connections.

@benr77

benr77 commented on 29 Jul 2015

I've added the method for addServer as well - see my Gist based on this one. https://gist.github.com/benr77/258e42642b4632d5a826#file-memcachedwrapper-php

相關文章
相關標籤/搜索