mouf/utils.session.optimistic-session-handler
Composer 安装命令:
composer require mouf/utils.session.optimistic-session-handler
包简介
Session handler that releases session lock quickly. Useful for multiple ajax calls on the same page.
README 文档
README
OptimisticSessionHandler
PHP file-based session handler that releases session lock quickly. Useful to speed up multiple Ajax calls on the same page.
Why do we need this session handler?
It improves performances in case of projects with multiple and long Ajax requests running. Several requests using the user session can be concurrently executed by the server since the session is not locked for a long time.
By default, PHP writes session files on the disk. When you execute session_start, the session file is opened, and a lock is
put on the file. If another process tries to perform a session_start, the process will wait until the lock on the session file is released.
This is a security feature of PHP (2 processes cannot modify the same session at the same time), but this is dreadful for performances,
as PHP requests sharing the same session must be run sequentially.
This package offers a way around this problem. It assumes that everything is going to be alright (hence the "optimistic" name), and let several processes access the session at the same time. If two processes modify the session at the same time, it will try to merge the 2 results. If it fails to do so, it will throw an exception.
How does it work?
This session handler modifies the default behaviour of PHP session handling.
Sessions are still written to disk (like PHP does by default).
The session is opened and read when you call session_start() to fill the global variable $_SESSION.
But the session is closed immediately after.
At the end of your PHP script, the $_SESSION is compared with the old session. If the session has been modified in your script,
the handler re-opens a session and compare the new session with your changes. The merged session is saved.
Note: if you use an alternative session handler (like APC or Memcache), do not use this session handler. It is designed to be used with file based sessions.
## Using the session handler It's extremely easy to use. Just declare a new instance :
$handler = new OptimisticSessionHandler();
And save it as your default session handler :
session_set_save_handler($handler, true);
Then you can start the session as usual
session_start(['read_and_close' => true]);
Then the $_SESSION array is accessible.
You can configure rules for managing conflicts. Just add element to the class parameter $conflictRules. The possible rules are:
- IGNORE => Don't use the current change.
- OVERWRITE => Use the current change.
- FAIL => Throw exception.
So you can just declare a new instance like this:
$handler = new OptimisticSessionHandler(array("key_to_override" => OptimisticSessionHandler::OVERRIDE));
Destroying the session
Warning: The session can't be destroyed by session_destroy() (It will throw an error). To destroy the session, you must empty the $_SESSION array.
$_SESSION = array();
If you want more information about this package you can go on OptimisticSessionHandler: A New Way To Think PHP Sessions
mouf/utils.session.optimistic-session-handler 适用场景与选型建议
mouf/utils.session.optimistic-session-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.27k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 08 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「session」 「handler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mouf/utils.session.optimistic-session-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mouf/utils.session.optimistic-session-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mouf/utils.session.optimistic-session-handler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lazy loading for middleware and request handlers
Secure session middleware for Slim 3 framework
Set Links with a specific language parameter
Non-I/O blocking PHP SessionHandler implementation using Nette/Caching with DI Extension for Nette framework
Bootstraps errors and handles them via reporters and renderers
PHP Error Handler module that captures and displays all throwable errors in a given format, making debugging easier and more efficient
统计信息
- 总下载量: 3.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-10