定制 playbase-oss/laravel-redis-session-enhanced 二次开发

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

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

playbase-oss/laravel-redis-session-enhanced

Composer 安装命令:

composer require playbase-oss/laravel-redis-session-enhanced

包简介

Enhanced redis driver for sessions in Laravel

README 文档

README

Total Downloads Latest Stable Version License Status

A Redis session driver for Laravel that stores rich session metadata — user_id, ip_address, user_agent, and last_activity — alongside the session payload. This gives you the same session management capabilities as Laravel's database driver, but backed by Redis.

Use this if you want to:

  • Track active sessions per user
  • Implement "logout from other devices"
  • Force-logout users (individually or all at once)
  • Prevent concurrent sessions

Requirements

  • PHP ^8.2
  • Laravel ^11.0 | ^12.0
  • Redis configured in your Laravel application

Installation

composer require playbase-oss/laravel-redis-session-enhanced

The service provider is auto-discovered via Laravel's package discovery.

Configuration

1. Add a dedicated Redis connection for sessions in config/database.php:

'redis' => [
    // ... existing connections

    'session' => [
        'host'     => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port'     => env('REDIS_PORT', 6379),
        'database' => env('REDIS_SESSION_DB', 2),
    ],
],

Using a separate database index keeps session keys isolated from cache and queue data, making it trivial to flush all sessions without affecting other Redis data.

2. Update your .env:

SESSION_DRIVER=redis-session
SESSION_CONNECTION=session

If you have a cached config, clear it:

php artisan config:clear

Session Data Structure

Each session is stored as a JSON object with the following shape:

{
    "user_id": 1,
    "ip_address": "127.0.0.1",
    "user_agent": "Mozilla/5.0 ...",
    "last_activity": 1711234567,
    "payload": "<base64-encoded session data>"
}

Usage

SessionHelper

SessionHelper abstracts over both the redis-session and database drivers, so you can switch between them without changing application code.

use PlayBaseOss\LaravelRedisSessionEnhanced\Support\SessionHelper;

// Get all sessions for a user
SessionHelper::getForUser($userId);

// Get only active sessions (within SESSION_LIFETIME)
SessionHelper::getForUser($userId, only_active: true);

// Delete all sessions of a user except the current one
SessionHelper::deleteForUserExceptSession($userId, request()->session()->id());

// Delete all sessions of a user
SessionHelper::deleteForUserExceptSession($userId);

// Delete all sessions (every user)
SessionHelper::deleteAll();

// Check if the application is using a supported driver
SessionHelper::isUsingValidDriver(); // true for database or redis-session

Direct Handler Access

You can resolve the underlying RedisSessionEnhancerHandler directly from the session facade:

use PlayBaseOss\LaravelRedisSessionEnhanced\Session\RedisSessionEnhancerHandler;

/** @var RedisSessionEnhancerHandler $handler */
$handler = Session::getHandler();

// Get all sessions as a Collection of SessionData objects
$sessions = $handler->readAll();

// Flush all session data from Redis
$handler->destroyAll();

SessionData

readAll() returns a Collection of SessionData objects:

use PlayBaseOss\LaravelRedisSessionEnhanced\Session\SessionData;

$session->id;            // string  — session ID
$session->user_id;       // mixed   — authenticated user ID, or null
$session->ip_address;    // string  — client IP address
$session->user_agent;    // string  — client User-Agent (truncated to 500 chars)
$session->last_activity; // int     — Unix timestamp of last activity
$session->payload;       // string  — base64-encoded session payload

Migrating from v1

The package has been transferred to PlayBase-OSS. Update your composer.json:

composer remove craftsys/laravel-redis-session-enhanced
composer require playbase-oss/laravel-redis-session-enhanced

Update any direct class references in your application:

v1 v2
Craftsys\LaravelRedisSessionEnhanced\SessionHelper PlayBaseOss\LaravelRedisSessionEnhanced\Support\SessionHelper
Craftsys\LaravelRedisSessionEnhanced\RedisSessionEnhancerHandler PlayBaseOss\LaravelRedisSessionEnhanced\Session\RedisSessionEnhancerHandler

Credits

License

MIT

playbase-oss/laravel-redis-session-enhanced 适用场景与选型建议

playbase-oss/laravel-redis-session-enhanced 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 139 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「php」 「session」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 playbase-oss/laravel-redis-session-enhanced 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-22