memcached session in symfony

方法1:

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:

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:

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.

Accessing the session

Session can be accessed from the Request object:

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

HttpFoundation component

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

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.

 

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

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

 

转载于:https://my.oschina.net/u/144160/blog/787107

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值