欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP使用Redis实现Session共享的实现示例

程序员文章站 2023-02-17 14:43:34
前言 小型web服务, session数据基本是保存在本地(更多是本地磁盘文件), 但是当部署多台服务, 且需要共享session, 确保每个服务都能共享到同一份se...

前言

小型web服务, session数据基本是保存在本地(更多是本地磁盘文件), 但是当部署多台服务, 且需要共享session, 确保每个服务都能共享到同一份session数据.

redis 数据存储在内存中, 性能好, 配合持久化可确保数据完整.

设计方案

1. 通过php自身session配置实现

# 使用 redis 作为存储方案
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
# 若设置了连接密码, 则使用如下
session.save_path = "tcp://127.0.0.1:6379?auth=密码"

测试代码

<?php
ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://127.0.0.1:6379");

session_start();
echo "<pre>";
$_session['usertest'.rand(1,5)]=1;
var_dump($_session);

echo "</pre>";

输出 ↓

array(2) {
  ["usertest1"]=>
  int(88)
  ["usertest3"]=>
  int(1)
}
usertest1|i:1;usertest3|i:1;

评价

  • 优点: 实现简单, 无需修改php代码
  • 缺点: 配置不支持多样化, 只能应用于简单场景

2. 设置用户自定义会话存储函数

通过 函数设置用户自定义会话函数.

session_set_save_handler ( callable $open , callable $close , callable $read , callable $write , callable $destroy , callable $gc [, callable $create_sid [, callable $validate_sid [, callable $update_timestamp ]]] ) : bool
  
# >= php5.4
session_set_save_handler ( object $sessionhandler [, bool $register_shutdown = true ] ) : bool

在配置完会话存储函数后, 再执行 session_start() 即可.

具体代码略, 以下提供一份 memcached 的(来自symfony框架代码):

<?php

/*
 * this file is part of the symfony package.
 *
 * (c) fabien potencier <fabien@symfony.com>
 *
 * for the full copyright and license information, please view the license
 * file that was distributed with this source code.
 */

namespace symfony\component\httpfoundation\session\storage\handler;

/**
 * memcachesessionhandler.
 *
 * @author drak <drak@zikula.org>
 */
class memcachesessionhandler implements \sessionhandlerinterface
{
  /**
   * @var \memcache memcache driver.
   */
  private $memcache;

  /**
   * @var int time to live in seconds
   */
  private $ttl;

  /**
   * @var string key prefix for shared environments.
   */
  private $prefix;

  /**
   * constructor.
   *
   * list of available options:
   * * prefix: the prefix to use for the memcache keys in order to avoid collision
   * * expiretime: the time to live in seconds
   *
   * @param \memcache $memcache a \memcache instance
   * @param array   $options an associative array of memcache options
   *
   * @throws \invalidargumentexception when unsupported options are passed
   */
  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"', implode(', ', $diff)
      ));
    }

    $this->memcache = $memcache;
    $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
    $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s';
  }

  /**
   * {@inheritdoc}
   */
  public function open($savepath, $sessionname)
  {
    return true;
  }

  /**
   * {@inheritdoc}
   */
  public function close()
  {
    return $this->memcache->close();
  }

  /**
   * {@inheritdoc}
   */
  public function read($sessionid)
  {
    return $this->memcache->get($this->prefix.$sessionid) ?: '';
  }

  /**
   * {@inheritdoc}
   */
  public function write($sessionid, $data)
  {
    return $this->memcache->set($this->prefix.$sessionid, $data, 0, time() + $this->ttl);
  }

  /**
   * {@inheritdoc}
   */
  public function destroy($sessionid)
  {
    return $this->memcache->delete($this->prefix.$sessionid);
  }

  /**
   * {@inheritdoc}
   */
  public function gc($maxlifetime)
  {
    // not required here because memcache will auto expire the records anyhow.
    return true;
  }

  /**
   * return a memcache instance
   *
   * @return \memcache
   */
  protected function getmemcache()
  {
    return $this->memcache;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。