spiritix/lada-cache
Composer 安装命令:
composer require spiritix/lada-cache
包简介
A Redis based, automated and scalable database caching layer for Laravel
README 文档
README
A Redis-based, fully automated, and scalable query cache layer for Laravel.
Lada Cache 6.x - a focused rewrite for Laravel 12 / 13 and PHP 8.3, addressing many long-standing issues and adding new features.
Table of Contents
- Features
- Version Compatibility
- Architecture
- Performance
- Why?
- Why only Redis?
- Installation
- Configuration
- Usage
- Console Commands
- Known Issues and Limitations
- Contributing
- License
Features
- 🚀 Fully automated query caching - no code changes required after setup
- 🧩 Granular invalidation - automatically invalidates only affected rows or tables
- 🧠 Transparent integration with Eloquent and Query Builder
- ⚡ Redis-backed - in-memory speed, cluster-ready, horizontally scalable
- 🧰 Laravel Debugbar integration - visualize cache hits, misses, and invalidations
- 🎛️ Fine-grained control - include or exclude tables from caching
- 🧱 Connection integration - DB queries pass through a Lada-aware connection subclass transparently
Version Compatibility
| Laravel | PHP | Lada Cache |
|---|---|---|
| 5.1-5.6 | 5.6.4+ | 2.x |
| 5.7-5.8 | 7.1+ | 3.x |
| 6.x | 7.2+ | 4.x |
| 7.x | 7.2+ | 5.x |
| 8.x | 7.3+ | 5.x |
| 9.x-11.x | 8.0+ | 5.x |
| 12.x | 8.3+ | 6.x |
| 13.x | 8.3+ | 6.x |
Architecture
Lada Cache integrates with Laravel’s database layer by registering a Lada-aware connection, which returns a custom query builder to intercept and cache SQL operations automatically.
Query lifecycle:
- Intercept query → Reflector analyzes SQL, bindings, and affected tables.
- Compute cache key → Based on SQL + parameters + database.
- Lookup in Redis → If cached, return immediately.
- Execute + store → If not cached, execute query and store result.
- Auto-invalidate → On any insert, update, delete, or truncate.
The result is automatic, consistent caching across all database operations.
Performance
Real-world gains range from 5% to 95%, depending on how many and how complex your queries are. Typical Laravel apps see ~10–30% faster responses and a significant drop in DB load.
- Large payloads still cost to move/encode; e.g. a query returning ~500MB won’t get faster from caching alone.
- The more redundant and complex the queries per request, the bigger the benefit.
- Reduced database traffic can translate to lower infra cost and easier horizontal scaling.
Why?
- Database-heavy apps (especially with Eloquent) often repeat the same queries and not all are efficient.
- RDBMS internal caches (e.g., MySQL Query Cache) have hard limits:
- Do not cache multi-table queries (joins)
- Coarse invalidation (row change can evict a whole table)
- Not distributed across DB servers
- Poor scalability under load
- Laravel manual caching requires manual invalidation or time-based expiry.
Lada Cache provides automated, granular, distributed caching with transparent invalidation and scale-out via Redis.
Why only Redis?
- Requires in-memory storage for latency and throughput.
- Must be easily scalable and distributed.
- Needs tagging support for granular invalidation (Laravel Cache tags exist but are slow for this use case).
Therefore, Lada Cache builds directly on top of Laravel Redis. If you need another backend, contributions are welcome.
Installation
Install via Composer:
composer require spiritix/lada-cache
Lada Cache registers itself automatically via Laravel Package Discovery.
Then, publish the config file:
php artisan vendor:publish --provider="Spiritix\LadaCache\LadaCacheServiceProvider"
Finally, ensure all your Eloquent models include the trait:
use Spiritix\LadaCache\Database\LadaCacheTrait; class Car extends Model { use LadaCacheTrait; }
💡 It’s best to add the trait in a shared
BaseModelclass that all models extend.
Configuration
After publishing the configuration file (see Installation), you will find a comprehensive config file at config/lada-cache.php.
This file allows you to fine-tune cache behavior, such as:
- Enabling or disabling Lada Cache globally
- Setting key prefixes and expiration times
- Defining which tables to include or exclude from caching
- Enabling Debugbar integration for cache inspection
The default configuration is already optimized for most Laravel applications, so you typically won’t need to modify it unless you want more granular control.
Usage
After installation, Lada Cache works automatically.
No code changes or caching calls are required - all database queries are transparently cached and invalidated.
You can control global behavior via .env:
LADA_CACHE_ACTIVE=true LADA_CACHE_DEBUGBAR=true
Bypassing the cache per query
Use withoutCache() to opt a single query out of caching. The query skips both read caching and write invalidation, then continues through Laravel as usual:
// Eloquent $user = User::where('id', $id)->withoutCache()->first(); // Query Builder DB::table('users')->where('id', $id)->withoutCache()->update(['name' => 'New']);
This is useful for freshness-critical reads, diagnostics, or ad-hoc cache busting without touching the configuration.
Console Commands
# Flush all cached entries php artisan lada-cache:flush # Temporarily disable cache php artisan lada-cache:disable # Re-enable cache php artisan lada-cache:enable
Known Issues and Limitations
- Multiple connections (
DB::connection('foo')) are only supported when using Lada’s connection integration. Models defining$connectionwork automatically. - Third-party packages with custom query builders may bypass caching.
- Complex SQL constructs such as
UNION/INTERSECT/advanced expressions may not be fully reflected for row-level tagging; invalidation falls back to table-level tags. - Raw SQL executed directly via the connection (e.g.,
DB::select(),DB::statement()) is not cached by design. - Row-level tagging relies on standard single-column primary keys. Composite or unconventional primary keys fall back to table-level invalidation.
Contributing
Pull requests and issue reports are welcome!
- Follow PSR-12 coding style
- Add tests for all new features
- Submit via feature branches (no direct PRs from
master)
License
Lada Cache is open-source software licensed under the MIT License.
spiritix/lada-cache 适用场景与选型建议
spiritix/lada-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 455.71k 次下载、GitHub Stars 达 591, 最近一次更新时间为 2015 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「database」 「php」 「cache」 「caching」 「model」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spiritix/lada-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spiritix/lada-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spiritix/lada-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Microservice RPC through message queues.
Store your language lines in the database, yaml or other sources
The CodeIgniter Redis package
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
贝嘟分布式缓存扩展
统计信息
- 总下载量: 455.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 592
- 点击次数: 18
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-12