定制 joby/smol-session 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

joby/smol-session

Composer 安装命令:

composer require joby/smol-session

包简介

An opinionated PHP session management library, designed to expose a simple API but provide smart performance optimizations and minimize session creation and locking.

README 文档

README

An opinionated PHP session management library designed to expose a simple API while providing smart performance optimizations and minimizing session creation and locking.

Features

  • Lazy session creation - Sessions are only started when data is actually written, avoiding unnecessary cookie traffic
  • Minimized locking - Sessions are only locked once on first read, and again if necessary during commits, not for the entire response lifecycle
  • Atomic updates - Operations like increments that apply atomically on commit are possible
  • Simple static API - Clean, straightforward interface for common session operations

Installation

composer require joby/smol-session

Usage

use Joby\Smol\Session\Session;

// Set values (queued, doesn't lock the session)
Session::set('user_id', 123);
Session::set('username', 'john_doe');

// Increment a counter (queued, doesn't lock the session, will apply to actual value upon commit to avoid race conditions)
Session::increment('page_views');
Session::increment('score', 10);

// Unset values (queued, doesn't lock the session)
Session::unset('temp_data');

// Read values (applies queued updates to cached values for convenience)
$userId = Session::get('user_id');
$views = Session::get('page_views');

// Commit all changes at once atomically, does not reopen session if no changes are queued
Session::commit();

// Can rotate session IDs
Session::rotate();

// Can also destroy the session, deleting all data and unsetting the cookie
Session::destroy();

Typed Getters

smolSession provides type-safe getter methods that automatically convert session values to the expected type:

// Type-safe access with automatic conversion
$userId = Session::getInt('user_id');       // Converts "123" → 123
$price = Session::getFloat('cart_total');   // Converts "19.99" → 19.99
$enabled = Session::getBool('dark_mode');   // Converts "yes" → true
$username = Session::getString('username'); // Converts 123 → "123"

// With defaults for missing values
$limit = Session::getInt('page_limit', 20);
$theme = Session::getString('theme', 'default');
$debug = Session::getBool('debug', false);

// Require values (throw TypeCastException if null/missing)
$requiredId = Session::requireInt('user_id');
$requiredEmail = Session::requireString('email');

Type Conversion Rules

Values are converted pretty permissively using (https://github.com/joby-lol/smol-cast)[smolCast], see its readme for detailed rules about what can be converted and how it works.

How It Works

  1. Reading - Session::get() reads from a cached copy of the session and applies any queued updates
  2. Writing - Session::set(), Session::increment(), and Session::unset() queue changes without opening the session
  3. Committing - Session::commit() opens the session, applies all updates atomically, and closes it

This approach minimizes session file locking and reduces the window where concurrent requests might conflict.

Advanced: Custom Atomic Updates

You can create custom atomic update operations by implementing the SessionUpdate interface:

use Joby\Smol\Session\SessionUpdate;

class AppendToArray implements SessionUpdate
{
    public function __construct(public mixed $value) {}

    public function apply(mixed $current_value): array
    {
        $array = is_array($current_value) ? $current_value : [];
        $array[] = $this->value;
        return $array;
    }

    public function isAbsolute(): bool
    {
        return false; // depends on current value
    }
}

// Use with Session::update()
Session::update('items', new AppendToArray('new_item'));

The isAbsolute() method indicates whether the update replaces the value entirely (like SetValue) or depends on the current value (like IncrementValue). Absolute updates enable performance optimizations by discarding previous queued updates for the same key.

Requirements

Fully tested on PHP 8.3+, static analysis for PHP 8.1.

License

MIT License - See LICENSE file for details.

joby/smol-session 适用场景与选型建议

joby/smol-session 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 23
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-15