好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁(yè) > 知識(shí)庫(kù) > PHP使用Redis實(shí)現(xiàn)Session共享的實(shí)現(xiàn)示例

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

熱門標(biāo)簽:電銷機(jī)器人問(wèn)門薩維品牌my 余姚電話機(jī)器人 外呼系統(tǒng)能給企業(yè)帶來(lái)哪些好處 開發(fā)地圖標(biāo)注類網(wǎng)站 廣東廣州在怎么申請(qǐng)400電話 百度地圖怎樣標(biāo)注圖標(biāo) 400電話蘭州申請(qǐng)請(qǐng) 百度地圖標(biāo)注偏差 咸寧銷售電銷機(jī)器人系統(tǒng)

前言

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

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

設(shè)計(jì)方案

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

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

測(cè)試代碼

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

評(píng)價(jià)

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

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

通過(guò) session_set_save_handler() 函數(shù)設(shè)置用戶自定義會(huì)話函數(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.4
session_set_save_handler ( object $sessionhandler [, bool $register_shutdown = TRUE ] ) : bool

在配置完會(huì)話存儲(chǔ)函數(shù)后, 再執(zhí)行 session_start() 即可.

具體代碼略, 以下提供一份 Memcached 的(來(lái)自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;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP實(shí)現(xiàn)cookie跨域session共享的方法分析
  • PHP實(shí)現(xiàn)負(fù)載均衡session共享redis緩存操作示例
  • PHP簡(jiǎn)單實(shí)現(xiàn)HTTP和HTTPS跨域共享session解決辦法
  • php實(shí)現(xiàn)session共享的實(shí)例方法

標(biāo)簽:衡陽(yáng) 巴彥淖爾 重慶 銅陵 臨沂 十堰 麗江 鷹潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP使用Redis實(shí)現(xiàn)Session共享的實(shí)現(xiàn)示例》,本文關(guān)鍵詞  PHP,使用,Redis,實(shí)現(xiàn),Session,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP使用Redis實(shí)現(xiàn)Session共享的實(shí)現(xiàn)示例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于PHP使用Redis實(shí)現(xiàn)Session共享的實(shí)現(xiàn)示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 3d动漫精品啪啪一区二区中文| 蜜桃视频一区二区三区| 欧美一级婬片A片久久精品色达人 一色欧美AV一噜噜噜 | 德国美女丰满BBwBBw| 黃色A片三級三級三級免费看? | 中国一级毛片在线观看| 一级毛片视频免费| 日本videoshd| 辣妞范10000部啪视频试看| japanhdxxxxhd| 国产91人妻精品一区二区| 学生16女人毛A级毛片国外电影| cao死我吧视频| 开荤粗肉v文| 国产偷自拍视频| 性开放天体浴场xxxxx| 国产精品久久久久精品一区二区| 日本无码免费Av在线观看司葵| 国产精品mm| **毛片免费拍拍拍aa视频| 久久综合伊人77777蜜臀| 色欲AV秘?无码一区二区三区| 无码人妻aⅴ一区二区三区湄公河| 韩国色三级伦不卡高清在线观看| ?国产精品嫩草影院?春菇 | 男女视频免费| 超爽肉文| 波多野结衣乳巨码在线播放| 女人扒开给男人爽| 日本调教无遮挡免费视频| 成人做受120秒试看试看视频| 老师也疯狂韩剧在线观看| 护士饱满奶水小说| 扒开?狂揉?脱脱内内真人| 天堂在线天堂新版www| 纯爱肉文| 男人揉女人胸视频| 免费看裸体美女??????动漫| 翁熄系列乱吃奶小玲| Jlzz日本人妻熟妇无码APP| 中国特黄特色大片免费视频老年人|