通过session_set_save_handler()方法自定义Session写入Memcache

 <?php
class MemSession{
private static $handler = null;
private static $lifetime = null;
private static $time = null;
const MS = 'session'; private static function init($handler){
self::$handler = $handler;
self::$lifetime = ini_get('session.gc_maxlifetime');
self::$time = time();
} public static function start($memcache){
self::init($memcache);
//调用类中的方法要用数组,__CLASS__代表本类
session_set_save_handler(
array(__CLASS__,'open'),
array(__CLASS__,'close'),
array(__CLASS__,'read'),
array(__CLASS__,'write'),
array(__CLASS__,'destroy'),
array(__CLASS__,'gc')
);
session_start();
} public static function open($path,$name){ }
public static function close(){ } public static function read($PHPSESSID){
$val = self::$handler->get(self::session_key($PHPSESSID)); if($val===false || $val==null){
return false;
}
return $val;
}
public static function write($PHPSESSID,$data){
$method = $data? 'set':'replace';
return self::$handler->$method(self::session_key($PHPSESSID),$data,MEMCACHE_COMPRESSED,self::$lifetime);
} public static function destroy($PHPSESSID){
return self::$handle->delete(self::session_key($PHPSESSID));
}
//memcache本身就有限定时间,数据自动销毁,所以可不使用gc方法
public static function gc($lifetime){
return true;
} //给sessionID加前缀,避免key重复
private static function session_key($PHPSESSID){
$session_key = self::MS.$PHPSESSID;
return $session_key;
}
}
$mem = new Memcache;
$mem->connect("localhost",11211) or die("could not connect");
MemSession::start($mem);

将Session写入Memcache的更多相关文章

  1. session写入memcache

    1 <?php 2 class MemSession{ 3 private static $handler = null; 4 private static $lifetime = null; ...

  2. 将Session写入数据库

    使用session_set_save_handler()函数,将Session的内容写入数据库 <?php /* *@author Fahy *@link http://home.cnblogs ...

  3. session 存到memcache里

    web服务器的php session都给memcached ,这样你不管分发器把 ip连接分给哪个web服务器都不会有问题了,配置方法很简单,就在php的配置文件内 增加一条语句就可以了,不过前提你需 ...

  4. PHP session 写入数据库中的方法

    首先解释下为什么要把session 写到数据库中呢,session 一般默认是以文件的形式放在php.ini 配置的目录中的, 如果你的网站实现了多台服务器负载均衡,这样用户访问你的网站,可能进入的服 ...

  5. [Yii Framework] Share the session with memcache in Yii

    When developing distributed applications with Yii, naturally, we will face that we have to share the ...

  6. c# Session写入读取操作

    /// <summary> /// 写Session /// </summary> /// <typeparam name="T">Sessio ...

  7. session写入数据库

    <?php class session { private static $handle = null; private static $ip = null; private static $l ...

  8. session之memcache

    nginx服务器配置:192.168.200.111[root@nginx ~]# hostname nginx[root@nginx ~]# bash[root@nginx ~]# vim /usr ...

  9. session或memcache过期之后跳转到登陆页面并跳出iframe框架

    <!--在你想控制跳转的页面,比如login.html中的<head>与</head>之间加入以下代码:--> <script> if (window ...

随机推荐

  1. SecureCRT连接linux设置vim显示颜色

    只需要两个步骤: 1) 选项 --> 会话选项 --> 终端 --> 仿真 -->  勾选“ANSI 颜色”. 2)  在.bashrc中添加:export TERM=xter ...

  2. CSS--复习之旅(一)

    CSS概述 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与表现分 ...

  3. 数论 - n元线性同余方程的解法

    note:n元线性同余方程因其编程的特殊性,一般在acm中用的很少,这里只是出于兴趣学了一下 n元线性同余方程的概念: 形如:(a1*x1+a2*x2+....+an*xn)%m=b%m       ...

  4. C#创建自定义配置节

    在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...

  5. Python好用的网站收集

    第三方Pthon包查找:http://www.lfd.uci.edu/ http://www.cnblogs.com/lanxuezaipiao/p/3543658.html

  6. 根据Expander的IsExpanded属性值的变化动态设计Control的size

    简要说明: 当Expander 的IsExpanded属性为“True” 时给控件设个尺寸(此处为高度),当为“False”时给控件设另外一个值. 知识点:数据绑定.Style和Trigger < ...

  7. bootstrap - table

    http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/

  8. ASP.NET或WinFrom中获取汉子的拼音首字母

    1.获得一个字符串的每个字的拼音首字母构成所需的字符串 #region  获取首字母 /// <summary>         /// 这个办法是用来获得一个字符串的每个字的拼音首字母构 ...

  9. LeetCode2:Median of Two Sorted Arrays

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  10. ugui 获取Text的高度,动态改变高度

    项目中需要根据聊天内容的多少.显示外边框的高度.因为Text的内容是不固定的.但宽度是固定的.高度根据文字多少自增 可以通过Text的属性preferredHeight 获取文本框的高度