davealex/laravel-service-caching
Composer 安装命令:
composer require davealex/laravel-service-caching
包简介
A simple strategy for managing data services caching in Laravel applications
README 文档
README
Laravel Service Caching
A simple, fluent, and driver-agnostic caching layer for your Laravel service classes. This package provides an easy way to cache the results of your service methods, automatically handling cache key generation based on request parameters and user context.
It intelligently detects whether your configured cache driver supports tags and falls back to a manual key-tracking system if it doesn't, making it safe to use with drivers like file or database.
Installation
You can install the package via composer:
composer require davealex/laravel-service-caching
Configuration
Publish the configuration file with this command:
php artisan vendor:publish --provider="Davealex\LaravelServiceCaching\LaravelServiceCachingServiceProvider" --tag="config"
This will create a config/laravel-service-caching.php file in your application, allowing you to configure the default behavior of the package.
// config/laravel-service-caching.php
return [
// The default cache driver to use. 'null' will use Laravel's default.
'driver' => env('SERVICE_CACHE_DRIVER'),
// The default cache duration in seconds. (Default: 600 seconds / 10 minutes). Pass `duration` as **null or 0** in the options array to cache the result forever
'cache_duration_in_seconds' => 600,
// The attribute on the User model to use for unique user-based caching.
'user_identifier_key' => 'id',
];
Usage
1. Implement the Contract
First, your service class must implement the Davealex\LaravelServiceCaching\Contracts\CacheableServiceInterface. This is a simple marker interface that doesn't require you to implement any methods.
<?php
namespace App\Services;
use Davealex\LaravelServiceCaching\Contracts\CacheableServiceInterface;
class UserService implements CacheableServiceInterface
{
/**
* A method that retrieves data you want to cache.
*/
public function getActiveUsers(string $role = 'editor'): array
{
// This is a potentially slow database query or API call.
// It will only be executed if the data is not in the cache.
return User::where('status', 'active')
->where('role', $role)
->get()
->toArray();
}
}
2. Caching Service Data
Now, you can use the LaravelServiceCaching service (either through dependency injection or the Facade) to cache the results of your service methods.
The get() method is the primary method you will use.
use Davealex\LaravelServiceCaching\LaravelServiceCaching; // or use the Facade
use App\Services\UserService;
class UserController extends Controller
{
public function __construct(
private LaravelServiceCaching $cachingService,
private UserService $userService
) {}
public function index()
{
// The 'getActiveUsers' method will only be called the first time.
// Subsequent requests with the same URL query parameters will
// return the cached data.
$users = $this->cachingService->get(
$this->userService,
'getActiveUsers'
);
return response()->json($users);
}
}
3. Using Options
The third argument of the get() method is an $options array, which allows you to customize the caching behavior per call.
Caching Per User
To make the cache unique to the currently authenticated user, set unique_to_user to true.
$userData = $this->cachingService->get(
$this->reportService,
'generateUserReport',
[], // Method arguments
['unique_to_user' => true] // Options
);
Custom Duration
Set a custom cache duration (in seconds).
$data = $this->cachingService->get(
$this->someService,
'getInfrequentData',
[],
['duration' => 3600] // Cache for 1 hour
);
Additional Parameters
If your caching logic depends on factors outside the URL query string, you can add them to the cache key generation using the params key.
$data = $this->cachingService->get(
$this->productService,
'getProducts',
[],
['params' => ['region' => 'us-east-1']]
);
4. Clearing the Cache
You can clear all cached data associated with a specific service using the clear() method.
// Clear all cached results for UserService
$this->cachingService->clear($this->userService);
This will flush all cache entries for the service, whether you are using a taggable driver or not.
Testing
composer test
Security
If you discover any security related issues, please email daveabiola@gmail.com instead of using the issue tracker.
Credits
- David Olaleye
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
davealex/laravel-service-caching 适用场景与选型建议
davealex/laravel-service-caching 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「caching」 「service」 「laravel」 「davealex」 「laravel-service-caching」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 davealex/laravel-service-caching 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 davealex/laravel-service-caching 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 davealex/laravel-service-caching 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Query caching for Laravel 5
A fast and intuitive dependency injection container.
Registry service.
Simple PHP caching system that uses a tmp folder in a Linux environment.
Provides support for PSR-6 compatible cache for Yii1 application
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-21