最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年05月12日 16:40:15   作者:嘉興ing  
這篇文章主要介紹了PHP使用Redis實(shí)現(xiàn)Session共享的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前言

小型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. 通過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=密碼"

測試代碼

<?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)簡單, 無需修改php代碼
  • 缺點(diǎn): 配置不支持多樣化, 只能應(yīng)用于簡單場景

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

通過 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 的(來自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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Yii框架安裝簡明教程

    Yii框架安裝簡明教程

    這篇文章主要介紹了Yii框架安裝方法,總結(jié)分析了Yii框架安裝的基本步驟、命令與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • php實(shí)現(xiàn)簡易計(jì)算器

    php實(shí)現(xiàn)簡易計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)簡易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Yii框架中memcache用法實(shí)例

    Yii框架中memcache用法實(shí)例

    這篇文章主要介紹了Yii框架中memcache用法,以實(shí)例形式分析了在Yii框架中加入memcache的具體操作方法,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • android上傳圖片到PHP的過程詳解

    android上傳圖片到PHP的過程詳解

    這篇文章主要介紹了android上傳圖片到PHP的過程詳解,需要的朋友可以參考下
    2015-08-08
  • jQuery向下滾動(dòng)即時(shí)加載內(nèi)容實(shí)現(xiàn)的瀑布流效果

    jQuery向下滾動(dòng)即時(shí)加載內(nèi)容實(shí)現(xiàn)的瀑布流效果

    下拉滾動(dòng)條或鼠標(biāo)滾輪滾動(dòng)到頁面底部時(shí), 動(dòng)態(tài)即時(shí)加載新內(nèi)容,通過本文給大家介紹jQuery向下滾動(dòng)即時(shí)加載內(nèi)容實(shí)現(xiàn)的瀑布流效果,感興趣的朋友參考下
    2016-01-01
  • 通過代碼實(shí)例解析PHP session工作原理

    通過代碼實(shí)例解析PHP session工作原理

    這篇文章主要介紹了通過代碼實(shí)例解析PHP session工作原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 基于ThinkPHP實(shí)現(xiàn)的日歷功能實(shí)例詳解

    基于ThinkPHP實(shí)現(xiàn)的日歷功能實(shí)例詳解

    這篇文章主要介紹了基于ThinkPHP實(shí)現(xiàn)的日歷功能,結(jié)合實(shí)例形式詳細(xì)分析了基于thinkPHP實(shí)現(xiàn)日歷功能的相關(guān)界面布局、數(shù)據(jù)庫操作與日期時(shí)間運(yùn)算相關(guān)技巧,需要的朋友可以參考下
    2017-04-04
  • php 解決舊系統(tǒng) 查出所有數(shù)據(jù)分頁的類

    php 解決舊系統(tǒng) 查出所有數(shù)據(jù)分頁的類

    不同之處在于 沒有實(shí)現(xiàn)分頁的系統(tǒng), 默認(rèn)全部查出來 現(xiàn)在就要不能動(dòng)后臺(tái)的基礎(chǔ)上進(jìn)行操作 可以采用 相應(yīng)的 如下 代碼
    2012-08-08
  • php 使用mpdf實(shí)現(xiàn)指定字段配置字體樣式的方法

    php 使用mpdf實(shí)現(xiàn)指定字段配置字體樣式的方法

    前兩天在做一個(gè)pdf導(dǎo)出功能,使用的插件是kartik-v/yii2-mpdf,此插件使用的是mpdf。接下來通過本文給大家介紹php 使用mpdf實(shí)現(xiàn)指定字段配置字體樣式的方法,需要的朋友可以參考下
    2019-07-07
  • PHP寫微信公眾號(hào)文章頁采集方法

    PHP寫微信公眾號(hào)文章頁采集方法

    給大家分析一下如何用PHP寫出采集微信公眾號(hào)文章的方法以及代碼詳細(xì)講解,需要的朋友學(xué)習(xí)一下。
    2017-12-12

最新評(píng)論

临泽县| 卢氏县| 榆中县| 吴桥县| 盐山县| 榆林市| 新宾| 佛冈县| 平度市| 北碚区| 永安市| 乐亭县| 浪卡子县| 格尔木市| 霞浦县| 乐清市| 宁晋县| 报价| 卫辉市| 石林| 方正县| 炎陵县| 银川市| 芒康县| 海兴县| 碌曲县| 营口市| 沙田区| 乌拉特中旗| 姚安县| 晋州市| 岫岩| 巴中市| 田东县| 丹棱县| 密山市| 海宁市| 游戏| 九龙县| 平南县| 高安市|