前言
小型web服务, ssion数据基本是保存在本地(更多是本地磁盘文件), 但是当部署多台服务, 且需要共享ssion, 确保每个服务都能共享到同一份ssion数据.
redis 数据存储在内存中, 性能好, 配合持久化可确保数据完整.
设计方案
1. 通过php自身ssion配置实现
# 使用 redis 作为存储方案ssion.save_handler = redisssion.save_path = "tcp://127.0.0.1:6379"# 若设置了连接密码, 则使用如下ssion.save_path = "tcp://127.0.0.1:6379?auth=密码"
测试代码
<?phpini_t("ssion.save_handler", "redis");ini_t("ssion.save_path", "tcp://127.0.0.1:6379");ssion_start();echo "<pre>";$_ssion['urtest'.rand(1,5)]=1;var_dump($_ssion);echo "</pre>";
输出 ↓
array(2) {
[“urtest1”]=>
int(88)
[“urtest3”]=>
int(1)
}
urtest1|i:1;urtest3|i:1;
评价
优点: 实现简单, 无需修改php代码缺点: 配置不支持多样化, 只能应用于简单场景2. 设置用户自定义会话存储函数
通过 函数设置用户自定义会话函数.
ssion_t_save_handler ( callable $open , callable $clo , callable $read , callable $write , callable $destroy , callable $gc [, callable $create_sid [, callable $validate_sid [, callable $update_timestamp ]]] ) : bool # >= php5.4ssion_t_save_handler ( object $ssionhandler [, bool $register_shutdown = true ] ) : bool
在配置完会话存储函数后, 再执行 ssion_start() 即可.
具体代码略, 以下提供一份 memcached 的(来自symfony框架代码):
<?php/* * this file is part of the symfony package. * * (c) fabien potencier <fabien@symfony.com> * * for the full copyright and licen information, plea view the licen * file that was distributed with this source code. */namespace symfony\手抄报清明节component\httpfoundation\ssion\storage\handler;/** * memcachessionhandler. * * @author drak <drak@zikula.org> */class memcachessionhandler implements \ssionhandlerinterface{ /** * @var \memcache memcache driver. */ private $memcache; /** * @var int time to live in conds */ private $ttl; /** * @var string key prefix for shared environments. */ private $prefix; /** * constructor. * * list of available options: * * prefix: the prefix to u for the memcache keys in order to avoid collision * * expiretime: the time to live in conds * * @param \memcache $memcache a \memcache instance * @param array $options an associative array of memcache options * * @throws \invalidargumentexception when unsupported options are pasd */ public function __construct(\memcache $memcache, array $options = array()) { if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) { throw new \invalidargumentexception(sprintf( 'the following options are not supported "%s"', i贝加尔湖畔 好声音mplode(', ', $diff) )); } $this->memcache = $memcache; $this->ttl = ist($options['ee过去分词xpiretime']) ? (int) $options['expiretime'] : 86400; $this->prefix = ist($options['prefix']) ? $options['prefix'] : 'sf2s'; } /** * {@inheritdoc} */ public function open($savepath, $ssionname) { return true; } /** * {@inheritdoc} */ public funct宸宫 沐非ion clo() { return $this->memcache->clo(); } /** * {@inheritdoc} */ public function read($ssionid) { return $this->memcache->get($this->prefix.$ssionid) ?: ''; } /** * {@inheritdoc} */ public function write($ssionid, $data) { return $this->memcache->t($this->prefix.$ssionid, $data, 0, time() + $this->ttl); } /** * {@inheritdoc} */ public function destroy($ssionid) { return $this->me励志书籍排行榜mcache->delete($this->prefix.$ssionid); } /** * {@inheritdoc} */ public function gc($maxlifetime) { // not required here becau memcache will auto expire the records anyhow. return true; } /** * return a memcache instance * * @return \memcache */ protected function getmemcache() { return $this->memcache; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-07 09:54:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/6f7b096ffc365afbf28bb7c4a7fdd848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP使用Redis实现Session共享的实现示例.doc
本文 PDF 下载地址:PHP使用Redis实现Session共享的实现示例.pdf
留言与评论(共有 0 条评论) |