国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP使用Redis實現(xiàn)Session共享的實現(xiàn)示例

2024-05-05 00:09:01
字體:
供稿:網(wǎng)友

前言

小型web服務(wù), session數(shù)據(jù)基本是保存在本地(更多是本地磁盤文件), 但是當(dāng)部署多臺服務(wù), 且需要共享session, 確保每個服務(wù)都能共享到同一份session數(shù)據(jù).

redis 數(shù)據(jù)存儲在內(nèi)存中, 性能好, 配合持久化可確保數(shù)據(jù)完整.

設(shè)計方案

1. 通過php自身session配置實現(xiàn)

# 使用 redis 作為存儲方案session.save_handler = redissession.save_path = "tcp://127.0.0.1:6379"# 若設(shè)置了連接密碼, 則使用如下session.save_path = "tcp://127.0.0.1:6379?auth=密碼"

測試代碼

<?phpini_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;

評價

  • 優(yōu)點: 實現(xiàn)簡單, 無需修改php代碼
  • 缺點: 配置不支持多樣化, 只能應(yīng)用于簡單場景

2. 設(shè)置用戶自定義會話存儲函數(shù)

通過 session_set_save_handler() 函數(shù)設(shè)置用戶自定義會話函數(shù).

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.4session_set_save_handler ( object $sessionhandler [, bool $register_shutdown = TRUE ] ) : bool

在配置完會話存儲函數(shù)后, 再執(zhí)行 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;  }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 商都县| 泊头市| 福泉市| 项城市| 泸定县| 延庆县| 阳西县| 云阳县| 文山县| 隆子县| 永登县| 南充市| 双桥区| 庐江县| 黎平县| 定兴县| 剑川县| 沽源县| 滦南县| 菏泽市| 湘西| 四川省| 台湾省| 连云港市| 布拖县| 叙永县| 察隅县| 余庆县| 安顺市| 建水县| 鸡东县| 竹溪县| 西青区| 双流县| 绥中县| 永德县| 泸定县| 乐业县| 登封市| 青河县| 喜德县|