成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

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

Jiavan / 2867人閱讀

摘要:年月日前言小型服務(wù)數(shù)據(jù)基本是保存在本地更多是本地磁盤文件但是當(dāng)部署多臺(tái)服務(wù)且需要共享確保每個(gè)服務(wù)都能共享到同一份數(shù)據(jù)數(shù)據(jù)存儲(chǔ)在內(nèi)存中性能好配合持久化可確保數(shù)據(jù)完整設(shè)計(jì)方案通過(guò)自身配置實(shí)現(xiàn)使用作為存儲(chǔ)方案若設(shè)置了連接密碼則使用如下密碼測(cè)試代

Last-Modified: 2019年5月10日16:06:36

前言

小型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è)試代碼

";
$_SESSION["usertest".rand(1,5)]=1;
var_dump($_SESSION);

echo "
";

輸出 ↓

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框架代碼):


 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentHttpFoundationSessionStorageHandler;

/**
 * MemcacheSessionHandler.
 *
 * @author Drak 
 */
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;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/31440.html

相關(guān)文章

  • 負(fù)載均衡中使用Redis實(shí)現(xiàn)共享Session

    摘要:最近在研究架構(gòu)方面的知識(shí),包括數(shù)據(jù)庫(kù)讀寫(xiě)分離,緩存和隊(duì)列,集群,以及負(fù)載均衡,今天就來(lái)先學(xué)習(xí)下我在負(fù)載均衡中遇到的問(wèn)題,那就是共享的問(wèn)題。一負(fù)載均衡負(fù)載均衡把眾多的訪問(wèn)量分擔(dān)到其他的服務(wù)器上,讓每個(gè)服務(wù)器的壓力減少。 最近在研究Web架構(gòu)方面的知識(shí),包括數(shù)據(jù)庫(kù)讀寫(xiě)分離,Redis緩存和隊(duì)列,集群,以及負(fù)載均衡(LVS),今天就來(lái)先學(xué)習(xí)下我在負(fù)載均衡中遇到的問(wèn)題,那就是session共享...

    tainzhi 評(píng)論0 收藏0
  • Sessions共享技術(shù)設(shè)計(jì)

    摘要:方法銷毀大于給定的所有數(shù)據(jù),對(duì)本身?yè)碛羞^(guò)期機(jī)制的系統(tǒng)如和而言,該方法可以留空。注意事項(xiàng)瀏覽器標(biāo)簽?zāi)_本執(zhí)行過(guò)程中,打開(kāi)標(biāo)簽訪問(wèn)同一個(gè)腳本,會(huì)被,直到執(zhí)行完畢。 概述 分布式session是實(shí)現(xiàn)分布式部署的前提, 當(dāng)前項(xiàng)目由于歷史原因未實(shí)現(xiàn)分布式session, 但是由于在kubernets中部署多個(gè)pod時(shí), 負(fù)載均衡的調(diào)用鏈太長(zhǎng), 導(dǎo)致會(huì)話不能保持, 所以迫切需要分布式session....

    RdouTyping 評(píng)論0 收藏0
  • web應(yīng)用集群入門-利用docker在單機(jī)搭建web應(yīng)用集群實(shí)現(xiàn)session共享

    摘要:環(huán)境要求安裝了的主機(jī)本文示例環(huán)境為準(zhǔn)備鏡像首先把所有需要用到的鏡像拉取下來(lái)容器編排是容器進(jìn)行編排的工具,定義和運(yùn)行多容器的應(yīng)用,可以一條命令啟動(dòng)多個(gè)容器。 環(huán)境要求:安裝了docker的主機(jī) (本文示例環(huán)境為centos7.4) 準(zhǔn)備鏡像 首先把所有需要用到的鏡像拉取下來(lái) # nginx $ docker pull nginx # php & php-fpm $ docker pul...

    ls0609 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<