Redis替代文件存储session

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_25551295/article/details/50709425

php默认使用文件存储session,如果并发量大,效率会非常低。而redis对高并发的支持非常好,可以利用redis替换文件来存储session。

php存数session主要用到了session_set_save_handler.

在使用session_set_save_handler之前,先把php.ini配置文件的session.save_hadler选项设置为user

<?php
class SessionManager{
private $redis;
private $sessionSavePath;
private $sessionName;
private $sessionExpireTime=30;

public function __construct(){
    $this->redis = new Redis();
    $this->redis->connect('127.0.0.1',6379);
    $retval = session_set_save_handler(
        array($this,"open"),
        array($this,"close"),
        array($this,"read"),
        array($this,"write"),
        array($this,"destroy"),
        array($this,"gc")
    );
    session_start();
}

public function open($path,$name){
    return true;
}

public function close(){
    return true;
}

public function read($id){
    $value = $this->redis->get($id);
    if($value){
        return $value;
    }else{
        return '';
    }
}

public function write($id,$data){
    if($this->redis->set($id,$data)){
        $this->redis->expire($id,$this->sessionExpireTime);
        return true;
    }
    return false;
}

public function destroy($id){
    if($this->redis->delete($id)){
        return true;
    }
    return false;
}

public function gc($maxlifetime){
    return true;
}

public function __destruct(){
    session_write_close();
}

}
?>

sessionManager构造函数主要用来连接redis服务器,使用session_set_slave_handler函数并设置session回调函数,并调用session_start函数开启session功能。使用时,只需要包含sessionManager类,然后实例化一个sessionManager对象。下面是一个管理session的实例。session_set.php代码如下:

<?php
    include('SessionManager.php');
    new SessionManager();
    $_SESSION['username'] = 'captain';
?>

下面是读取session的代码.session_get.php代码如下:

<?php
    include('SessionManager.php');
    new SessionManager();
    echo $_SESSION['username'];
?>

分别访问session_set.php和session_get.php页面,我们会发现,已经可以成功的设置和获取session了

展开阅读全文

tomcat redis 存放session

08-12

tomcat7.055、redis2.8、jedis.jar、common-pool2.2.jar、tomcat-redis-session-manager-1.2-tomcat-7.jarrnrn将上面的3个jar包放入tomcat下的lib目录rnrn修改content.xmlrn添加:[code=html]rn [/code]rn启动tomcat时,报错,无法成功启动tomcat。rn以下是错误日志:[code=text]八月 12, 2014 12:43:16 下午 org.apache.tomcat.util.digester.Digester startElementrn严重: Begin event threw errorrnjava.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool$Configrn at java.lang.Class.getDeclaredConstructors0(Native Method)rn at java.lang.Class.privateGetDeclaredConstructors(Class.java:2532)rn at java.lang.Class.getConstructor0(Class.java:2842)rn at java.lang.Class.newInstance(Class.java:345)rn at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:145)rn at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1288)rn at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)rn at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1343)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)rn at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)rn at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)rn at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)rn at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)rn at org.apache.catalina.startup.ContextConfig.processContextConfig(ContextConfig.java:637)rn at org.apache.catalina.startup.ContextConfig.contextConfig(ContextConfig.java:580)rn at org.apache.catalina.startup.ContextConfig.init(ContextConfig.java:837)rn at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:385)rn at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)rn at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)rn at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)rn at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:110)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)rn at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)rn at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)rn at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)rn at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)rn at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1898)rn at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)rn at java.util.concurrent.FutureTask.run(FutureTask.java:262)rn at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)rn at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)rn at java.lang.Thread.run(Thread.java:745)rnCaused by: java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool$Configrn at java.net.URLClassLoader$1.run(URLClassLoader.java:366)rn at java.net.URLClassLoader$1.run(URLClassLoader.java:355)rn at java.security.AccessController.doPrivileged(Native Method)rn at java.net.URLClassLoader.findClass(URLClassLoader.java:354)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:425)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:358)rn ... 37 morernrn八月 12, 2014 12:43:16 下午 org.apache.catalina.startup.HostConfig deployDirectoryrn严重: Error deploying web application directory C:\Apache Software Foundation\tomcat7\tomcat3\apache-tomcat-7.0.55\webapps\docsrnjava.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool$Configrn at java.lang.Class.getDeclaredConstructors0(Native Method)rn at java.lang.Class.privateGetDeclaredConstructors(Class.java:2532)rn at java.lang.Class.getConstructor0(Class.java:2842)rn at java.lang.Class.newInstance(Class.java:345)rn at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:145)rn at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1288)rn at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)rn at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1343)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)rn at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)rn at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)rn at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)rn at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)rn at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)rn at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)rn at org.apache.catalina.startup.ContextConfig.processContextConfig(ContextConfig.java:637)rn at org.apache.catalina.startup.ContextConfig.contextConfig(ContextConfig.java:580)rn at org.apache.catalina.startup.ContextConfig.init(ContextConfig.java:837)rn at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:385)rn at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)rn at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)rn at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)rn at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:110)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)rn at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)rn at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)rn at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)rn at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)rn at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1898)rn at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)rn at java.util.concurrent.FutureTask.run(FutureTask.java:262)rn at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)rn at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)rn at java.lang.Thread.run(Thread.java:745)rnCaused by: java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool$Configrn at java.net.URLClassLoader$1.run(URLClassLoader.java:366)rn at java.net.URLClassLoader$1.run(URLClassLoader.java:355)rn at java.security.AccessController.doPrivileged(Native Method)rn at java.net.URLClassLoader.findClass(URLClassLoader.java:354)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:425)rn at java.lang.ClassLoader.loadClass(ClassLoader.java:358)rn ... 37 more[/code]rn在新加入的3个jar包中,均没有需要用到org.apache.commons.pool.impl.GenericObjectPool$Config的地方,redis引用的是rnorg.apache.commons.pool2.impl.GenericObjectPool$Config,不知道怎么会出现这个错。rnrn在tomcat-redis-session-manager-1.2-tomcat-7.jar的源码中有一个类RedisSessionManager在Eclipse中发现有问题。rn这个类中有处代码for (Valve valve : getContext().getPipeline().getValves()) 这里的getContext() 方法报错,提示没有该方法,并且在该类的父类ManagerBase中也没有找到该方法。 论坛

没有更多推荐了,返回首页