bigdevwhale/cachetastic

Laravel 缓存方法结果的包

1.1.0 2023-10-28 19:32 UTC

This package is auto-updated.

Last update: 2024-09-28 21:33:10 UTC


README

frankenstein

Cachetastic - 为您的 Laravel 缓存提供动力!

目录

介绍

欢迎来到 Cachetastic,它是优化 Laravel 应用程序缓存的秘密武器。此包增强了您的 Laravel 缓存功能,使缓存方法结果和提高应用程序性能变得轻而易举。告别冗余的 API 调用和数据库查询,欢迎快速的响应!

关键特性

  • 方法级缓存:轻松缓存任何方法的结果,无论是常规的还是静态的。
  • 灵活的缓存管理:按需强制刷新和更新缓存的新值。
  • 自动缓存键生成:缓存键基于方法名称和参数生成。
  • 自定义参数键:用户可以指定用于缓存键生成的参数键。
  • Laravel 集成:与 Laravel 的缓存系统无缝集成。
  • 干净且优雅的代码:结构良好且高效的代码库,易于使用和贡献。
  • PHPUnit 和 Mockery 支持:包括 PHPUnit 和 Mockery 的单元测试支持。

使用场景

Cachetastic 是 Laravel 项目的完美补充,适用于各种使用场景。

  • API 响应:缓存外部 API 的响应以减少延迟并最小化速率限制。
  • 数据库查询:缓存复杂数据库查询以提升查询性能。
  • 计算密集型方法:缓存计算密集型方法的成果以实现更快的执行。
  • 动态内容:缓存动态内容以减少服务器负载并改善用户体验。
  • 速率限制的 API:通过缓存 API 响应和提供缓存数据来避免超出速率限制。

Cachetastic 是您优化 Laravel 应用程序性能的必备工具!

入门指南

开始使用 Cachetastic 简单易行。按照以下简单步骤将其集成到您的 Laravel 项目中

  1. 使用 Composer 安装 Cachetastic

    composer require bigdevwhale/cachetastic
  2. 在您的 Laravel 应用程序中配置默认的 缓存驱动器

  3. 使用 CacheService 类开始缓存方法结果。

     // Import the CacheService class
     use Cachetastic\Cachetastic;
    
     // Create an instance of CacheService
     $cacheService = new Cachetastic(
         new YourApiService(), // The service or object to call the method on.
         'fetchData',          // The name of the method to call on the service.
         [1, 2]               // An array of parameters to pass to the method.
     );
    
     // Customize the cache duration (optional)
     $cacheService->setCacheDuration(60);
    
     // Cache the result of your API call
     $result = $cacheService->retrieveOrCache();
    
      // Force a clear of the cached data if needed
     $cacheService->forceClear();
    
     // You can also use the optional $regenerate parameter to control if data should be regenerated
     $result = $cacheService->forceRefresh(false)
  4. 根据您的需求自定义缓存、强制刷新和错误处理。

就是这样!Cachetastic 以最小的努力无缝增强了您的缓存功能。

构造函数参数

CacheService 构造函数需要以下参数

  • service:要调用方法的服务或对象。
  • method:要在服务上调用的方法的名称。
  • params:传递给方法的一组参数。

缓存自定义方法

Cachetastic 提供以下方法来自定义缓存

  • setCacheDuration(int $cacheDuration):设置缓存持续时间(分钟)。
  • setCacheKeyParams(array $cacheKeyParams):设置用于缓存键生成的参数键。
  • setCustomCacheKey(string $customCacheKey):设置一个自定义缓存键,代替自动生成一个。

默认情况下,缓存持续时间设置为 60 分钟。

您可以使用这些方法进一步自定义您的缓存体验。

示例

以下是在 Laravel 应用程序中缓存方法结果的示例,无论是普通方法还是静态方法。

use Cachetastic\Cachetastic;
use YourApiService;

// Create an instance of Cachetastic to cache the result of a regular method
$cacheService = new Cachetastic(
    new YourApiService(), // The service or object to call the method on.
    'fetchData',          // The name of the method to call on the service.
    [1, 2]               // An array of parameters to pass to the method.
);

// Customize the cache duration (optional)
$cacheService->setCacheDuration(60);

// Cache the result of your API call, whether it's a regular method
$result = $cacheService->retrieveOrCache();

// Create an instance of Cachetastic to cache the result of a static method
$cacheServiceStatic = new Cachetastic(
    YourApiService::class, // The class with the static method.
    'fetchDataStatic',    // The name of the static method to call.
    [1, 2]                // An array of parameters to pass to the static method.
);

// Cache the result of your API call, whether it's a static method
$resultStatic = $cacheServiceStatic->retrieveOrCache();

重写缓存键

请注意,如果两个方法在同一个类中执行,并且只有数组参数,由于缓存键生成只使用标量参数,它们将覆盖彼此的缓存值。在这种情况下,请考虑使用上面描述的 setCustomCacheKey 方法。

贡献

我们欢迎开源社区的贡献。请随意在我们的 GitHub 仓库提交错误报告、功能请求或拉取请求。

许可

Cachetastic 是在 MIT 许可证下许可的开源软件。

使用 Cachetastic 为您的 Laravel 应用程序添加动力,为您的缓存游戏加速!试一试,亲身体验其好处。祝您缓存愉快!