定制 athwari/laravel-method-cache 二次开发

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

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

athwari/laravel-method-cache

最新稳定版本:v1.0.0

Composer 安装命令:

composer require athwari/laravel-method-cache

包简介

Attribute-based method caching for Laravel using method overriding proxy system

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Attribute-based method caching for Laravel applications using method overriding proxy system.

This package allows you to cache method results by annotating them with attributes, automatically intercepting calls to store and retrieve cached results.

Installation

Install the package with Composer:

composer require athwari/laravel-method-cache

Laravel package auto-discovery is supported, so the service provider and facade are registered automatically.

Configuration

Publish the configuration file:

php artisan vendor:publish --tag=method-cache-config

The published config file is located at config/method-cache.php.

Default configuration

return [
    'default_ttl' => 3600, // Default TTL in seconds
    'cache_store' => null, // Cache store to use, null for default
    'key_prefix' => 'method_cache',
    'enable_logging' => false,
    'log_channel' => 'default',
    'auto_flush_events' => [], // Eloquent events to auto-flush, e.g., ['saved', 'deleted']
];

Usage

Annotate methods with the #[Cacheable] attribute to enable caching.

Basic caching

use Athwari\MethodCache\Attributes\Cacheable;

class UserService
{
    #[Cacheable(ttl: 300)] // Cache for 5 minutes
    public function getUser(int $id): User
    {
        // Expensive operation
        return User::find($id);
    }
}

Custom key and tags

#[Cacheable(key: 'user_profile_{id}', tags: ['users'])]
public function getUserProfile(int $id): array
{
    return [
        'user' => User::find($id),
        'posts' => Post::where('user_id', $id)->get(),
    ];
}

Conditional caching

#[Cacheable(condition: 'fn($args) => $args[0] > 0')]
public function expensiveCalculation(int $value): int
{
    // Only cache if value > 0
}

Cache invalidation

Use the facade to flush caches:

use Athwari\MethodCache\Facades\MethodCache;

// Flush all
MethodCache::flushAll();

// Flush by tags
MethodCache::flushTags(['users']);

// Flush by class
MethodCache::flushClass(UserService::class);

Or use the console command:

php artisan method-cache:flush --tags=users
php artisan method-cache:flush --class=App\\Services\\UserService

Attributes

  • #[Cacheable]: Main caching attribute
  • #[NoCache]: Skip caching for specific methods
  • #[CacheableUntil]: Cache until a specific time

Exceptions

The package may throw exceptions related to caching or method overriding.

Testing

Run the test suite with Pest:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The package is open-source software licensed under the MIT License. Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固