altmetric/mongo-session-handler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

altmetric/mongo-session-handler

最新稳定版本:v2.1.0

Composer 安装命令:

composer require altmetric/mongo-session-handler

包简介

A PHP session handler backed by MongoDB.

README 文档

README

mongo-session-handler is not maintained anymore.

Mongo Session Handler Build Status

A PHP session handler backed by MongoDB.

Current version: 2.1.0
Supported PHP versions: 5.4, 5.5, 5.6, 7

Note: This package depends on the MongoDB PHP driver extension (mongodb) and its companion PHP library. If you need to use the older, legacy driver (mongo), please see version 1.0.

Installation

$ composer require altmetric/mongo-session-handler:^2.1

Usage

<?php
use Altmetric\MongoSessionHandler;

$sessions = $mongoClient->someDB->sessions;
$handler = new MongoSessionHandler($sessions);

session_set_save_handler($handler);
session_set_cookie_params(0, '/', '.example.com', false, true);
session_name('my_session_name');
session_start();

API Documentation

public MongoSessionHandler::__construct(MongoDB\Collection $collection[, Psr\Log\LoggerInterface $logger])

$handler = new \Altmetric\MongoSessionHandler($client->db->sessions);
session_set_save_handler($handler);
session_start();

$handler = new \Altmetric\MongoSessionHandler($client->db->sessions, $logger);

Instantiate a new MongoDB session handler with the following arguments:

  • $collection: a MongoDB\Collection collection to use for session storage;
  • $logger: an optional PSR-3-compliant logger.

The given $collection will be populated with documents using the following schema:

This handler implements the SessionHandlerInterface meaning that it can be registered as a session handler with session_set_save_handler.

Expiring sessions

If you wish to clean up expired sessions using SessionHandlerInterface::gc, ensure that your session.gc_divisor, session.gc_probability and session.gc_maxlifetime settings are populated accordingly, e.g. the following settings in your php.ini mean that sessions that haven't been updated in over an hour have a 1% chance of being cleaned whenever someone starts a new session:

session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 3600

In order to keep session cleaning fast, you should add an index on the last_accessed field of your session $collection, e.g.

db.sessions.createIndex({last_accessed: 1});

Concurrency

As MongoDB prior to 3.0 does not support document level locking, this session handler operates on a principle of Last Write Wins.

If a user of a session causes two simultaneous writes then you may end up with the following situation:

  1. Window A reads session value of ['foo' => 'bar'];
  2. Window B reads session value of ['foo' => 'bar'];
  3. Window B writes session value of ['foo' => 'baz'];
  4. Window A writes session value of ['foo' => 'quux'].

The session will now contain ['foo' => 'quux'] as it was the last successful write. This may be surprising if you're trying to increment some value in a session as it is not locked during reads and writes:

  1. Window A reads session value of ['count' => 0];
  2. Window B reads session value of ['count' => 0];
  3. Window B writes session value of ['count' => 1];
  4. Window A writes session value of ['count' => 1].

Acknowledgements

License

Copyright © 2015-2017 Altmetric LLP

Distributed under the MIT License.

altmetric/mongo-session-handler 适用场景与选型建议

altmetric/mongo-session-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59.05k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 altmetric/mongo-session-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 altmetric/mongo-session-handler 我们能提供哪些服务?
定制开发 / 二次开发

基于 altmetric/mongo-session-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 59.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 4
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-15