方法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

imports:

# ....

- { resource: services/session.yml }

framework:

# ....

session:

handler_id: session.handler.memcached

aptitude install memcached php5-memcached

parameters:

# ...

session_memcached_host: localhost

session_memcached_port: 11211

session_memcached_prefix: sess

session_memcached_expire: 3600

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 <?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

*

*/

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 <?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);

}

}

} 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] ] ] ]

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.

php session m.jb51.net,memcached session in symfony相关推荐

  1. tomcat + memcached session manager共享session

    网上有很多关于通过MSM(memcached session manager)实现memcached共享session的文章,但是很多都是东拼西凑,误导别人.正巧最近有一个地方用到,特此总结一下. M ...

  2. session存入redis或memcached

    2019独角兽企业重金招聘Python工程师标准>>> session 存入 redis 的 php 配置 安装 redis 安装 phpredis 扩展 编辑 php.ini 配置 ...

  3. php session缓存,扫盲:php session缓存至memcached中的方法

    memcached是一套分布式的快取系统,当初是DangaInteractive为了LiveJournal所发展的,但被许多软件(如MediaWiki)所使用.这是一套开放源代码软件,以BSDlice ...

  4. session机制详解以及session的相关应用

    2019独角兽企业重金招聘Python工程师标准>>> session是web开发里一个重要的概念,在大多数web应用里session都是被当做现成的东西,拿来就直接用,但是一些复杂 ...

  5. cookie session token区别_cookie、session与token的真正区别

    发展史 1.很久很久以前,Web 基本上就是文档的浏览而已, 既然是浏览,作为服务器, 不需要记录谁在某一段时间里都浏览了什么文档,每次请求都是一个新的HTTP协议, 就是请求加响应, 尤其是我不用记 ...

  6. PHP session锁:如何避免session阻塞PHP请求

    来源:https://log.zvz.im/2016/02/27/PHP-session/ https://ma.ttias.be/php-session-locking-prevent-sessio ...

  7. 170222、使用Spring Session和Redis解决分布式Session跨域共享问题

    使用Spring Session和Redis解决分布式Session跨域共享问题 原创 2017-02-27 徐刘根 Java后端技术 前言 对于分布式使用Nginx+Tomcat实现负载均衡,最常用 ...

  8. session传递参数_分布式 Session 之 Spring Session 架构与设计

    作者 | 李增光 杏仁后端工程师.「只有变秃,才能变强!」 ​前言 开始进行 Web 开发时,我们可能会遇到这样的情况,当服务器重启之后,之前的登录状态会失效需要重新登录.又或者你的应用程序部署了不止 ...

  9. 你的项目真的需要Session吗? redis保存session性能怎么样?

    在web开发中,Session这个东西一直都很重要,至少伴随我10年之久, 前一段时间发生一个性能问题,因为Redis session 问题,后来想想 其实我的项目session 是不需要的. 先看看 ...

最新文章

  1. 美团爱问Kafka?太真实了!
  2. 深入理解Fabric环境搭建的详细过程
  3. 2017年10月05日普及组 负进制
  4. linux下静默安装oracle10g,CentOS下远程静默安装Oracle10g笔记
  5. Oracle DBVERIFY 工具介绍
  6. 全国python一级考试_全国青少年软件编程(Python)等级考试试卷(一级) 1-1
  7. centos7下载elasticsearch7版本(超详细)
  8. 大数据之-Hadoop之HDFS_合并FsImage和Edits文件_之CheckPoint时间设置---大数据之hadoop工作笔记0072
  9. Prototype 学习——Function对象
  10. tfs 2013 access deny
  11. xcode 配置wechat_友盟微信、QQ等分享提示未验证应用配置
  12. 输入上标的html标签是,HTML上标sup与下标注sub标签元素
  13. LA 2218 Triathlon (Geometry, Half Plane Intersection)
  14. 【Verilog设计—数字传输系统】ASK调制与FSK调制
  15. github微信小程序服务器,GitHub - cp871202/nideshop: NideShop 开源微信小程序商城服务端(Node.js + ThinkJS)...
  16. Unity实战篇 |制作一个跟随鼠标转向的 简易箭头指示标,包括 UI指向 和 3D指向标
  17. 算法(Java实现)-算法的时间复杂度和空间复杂度
  18. SSM校园好货APP的设计与实现毕业设计源码121619
  19. Apache Camel中的recipientList和routingSlip的区别?
  20. PreScan快速入门到精通第二十八讲PreScan中常用传感器之TIS传感器

热门文章

  1. 华为鸿蒙系统避免外国人,【图片】华为鸿蒙系统的厉害之处在于 你可能非用不可 !【手机吧】_百度贴吧...
  2. 比特安详细解析Fantasm Finance事件:注重细节,才能历久弥新
  3. WebServer代码实现第四版
  4. selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate elemen
  5. 密码极客波卡生态系列分享会第一期:如何基于波卡开发区块链项目
  6. VMware 全屏快捷键
  7. Node.js能做什么
  8. 华为高管:苹果是个软件公司,华为也开始走这条路
  9. Android 长时间 卡顿,安卓手机用的久了会卡顿?这几点才最有用!
  10. 云服务器哪家服务好、性价比高、比较稳定?