devcuongnguyen/laravel-caching-model

laravel eloquent model 的简单缓存库

1.0.0 2024-01-09 00:50 UTC

This package is auto-updated.

Last update: 2024-09-09 02:26:33 UTC


README

  • 用于缓存 laravel eloquent model 的简单包
  • 支持通过 id(或其他主键)检索缓存中存储的模型

使用方法

  1. 实现 Cacheable 接口,然后使用 HasCache 特性
use devcuongnguyen\CachingModel\Contracts\Cacheable;
use devcuongnguyen\CachingModel\HasCache;

class Setting extends Model implements Cacheable
{
    use HasCache;

    ...
}
  1. 使用 fromCache() 静态方法从缓存存储中检索模型数据
$cachedInstance = Setting::fromCache()->find($key);

可用方法

  1. public static function primaryCacheKey(): string;
  • 返回创建缓存键的主键
  • 默认:id
  1. public static function getCacheKey($id): string;
  • 返回具有主键 $id 的特定实例的缓存键
  1. public static function cacheTimeout(): int;
  • 返回缓存超时时间
  1. public function scopeCacheWithRelation($query);
  • 特定关系将一起缓存模型
public function scopeCacheWithRelation($query)
{
    return $query->with('relationship:id);
}