在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文摘自:http://wqss.2008.blog.163.com/blog/static/912428082011823104218806/?suggestedreading
折腾了一天,简单了实现了把session存储到redis中,依托于Qeephp。 <?php class Helper_Session{ static private $connect = FALSE; private $redis = NULL; private $redis_host; private $redis_port; function __construct(){ $this->redis_host=Q::ini(“appini/redis_session/redis_host”); $this->redis_port=Q::ini(“appini/redis_session/redis_port”); $this->is_sso=Q::ini(“appini/redis_session/is_sso”); } function open($sess_path = ”, $sess_name = ”) { if ( class_exists(‘redis’) ){ $redis = new Redis(); $conn = $redis->connect( $this->redis_host , $this->redis_port ); } else { throw new QException(‘服务器没有安装PHP-Redis扩展!’); } if ( $conn ){ $this->redis = $redis ; } else { throw new QException(‘无法正常连接缓存服务器!’); } return true; } function close(){ return true; } function read($sess_id){ return $this->redis->get(‘session:’.$sess_id); } function write($sess_id, $data){ $sess_data=$this->redis->get(‘session:’.$sess_id); if(empty($sess_data)){ $this->redis->set(‘session:’.$sess_id,$data); } $life_time=get_cfg_var(“session.gc_maxlifetime”); $this->redis->expire(‘session:’.$sess_id,$life_time); return true; } function destroy($sess_id){ $this->redis->delete(‘session:’.$sess_id); return true; } function gc($sess_maxlifetime){ return true; } function init(){ session_set_save_handler(array($this,’open’),array($this,’close’),array($this,’read’),array($this,’write’),array($this,’destroy’),array($this,’gc’)); } } $redis_session = new Helper_Session(); $redis_session->init();
使用,先看一下qeephp的myapp.php里面。 // 导入类搜索路径 Q::import($app_config['APP_DIR']); Q::import($app_config['APP_DIR'] . ‘/model’); // 设置 session 服务 if (Q::ini(‘runtime_session_provider’)) { Q::loadClass(Q::ini(‘runtime_session_provider’)); } // 打开 session if (Q::ini(‘runtime_session_start’)) { session_start(); // #IFDEF DEBUG QLog::log(‘session_start()’, QLog::DEBUG); QLog::log(‘session_id: ‘ . session_id(), QLog::DEBUG); // #ENDIF } 从这段代码可以知道qeephp是留有接口的,方便我们扩展,这里以helper助手类的方式体现的,放到了 app文件夹的helper里面,所以将导入类搜索路径放到了设置 session服务的上面。 在environment.yaml加入 # 设置session服务 runtime_session_provider: helper_session 在app.yaml中加入 redis_session: redis_host: 127.0.0.1 redis_port: 6379 在有redis的情况下现在就可以利用redis存储session。 需要将次方法修改 function cleanCurrentUser() { session_destroy(); //unset($_SESSION[$this->_acl_session_key]); } 由于对redis还不是太熟悉,没有实现单点登录,慢慢会完善的。 sohu技术部实习
/** * 定义 RedisSessionHandler的基础类,并完成session的初始化 * * @copyright Copyright ©:2012 * @author Tian Mo <[email protected]> * @version 2012-07-16 14:52;00 * */ // **************************************************************************** // This class saves the PHP session data in redis. // **************************************************************************** class RedisSessionHandler { //the redis object private $_redis; // **************************************************************************** // class constructor // **************************************************************************** function RedisSessionHandler ($host = '127.0.0.1', $port = 8359, $db = 15) { if(!extension_loaded('redis')) throw new \Exception("Redis Extension needed!"); $_redis = new \Redis(); //connnect to the redis $_redis->connect($host, $port) or die("Can't connect to the Redis!"); $_redis->auth("TechIpd.Sohu"); $this->_redis = $_redis; //select the db $this->_redis->select($db); } // php_Session function open ($save_path, $session_name) // open the session. { // do nothing return TRUE; } // open function close () // close the session. { return $this->gc(); } // close function read ($session_id) // read any data for this session. { return $this->_redis->get($session_id); } // read function write ($session_id, $session_data) // write session data to redis. { $this->_redis->setnx($session_id, $session_data); //Be careful,we must be the life time all right. $this->_redis->expire($session_id, /*get_cfg_var("session.gc_maxlifetime")*/3600 * 24); return TRUE; } // write function destroy ($session_id) // destroy the specified session. { $this->_redis->delete($session_id); return TRUE; } // destroy function gc () // perform garbage collection. { return TRUE; } // gc function __destruct () // ensure session data is written out before classes are destroyed // (see http://bugs.php.net/bug.php?id=33772 for details) { @session_write_close(); } // __destruct //redis session start function init(&$sessionObject) { //set in my handler ini_set('session.save_handler', 'user'); session_set_save_handler(array(&$sessionObject, 'open'), array(&$sessionObject, 'close'), array(&$sessionObject, 'read'), array(&$sessionObject, 'write'), array(&$sessionObject, 'destroy'), array(&$sessionObject, 'gc')); // the following prevents unexpected effects when using objects as save handlers register_shutdown_function('session_write_close'); // proceed to set and retrieve values by key from $_SESSION session_start(); } } //@test redis #$session_class = new RedisSessionHandler(); #$session_class->init($session_class); #$_SESSION['nn'] = 'successful'; ?> |
2022-08-17
2022-11-06
2022-08-17
2022-07-29
2022-07-29
请发表评论