chilltvpack / laravel-caching-model
为 Laravel Eloquent 模型提供简单缓存库
dev-main
2023-11-28 12:01 UTC
Requires
- php: ^7.0|^8.0
- laravel/framework: ^6.0|^7.0|^8.0
This package is not auto-updated.
Last update: 2024-10-02 14:20:03 UTC
README
- 用于缓存 Laravel Eloquent 模型的简单包
- 支持通过 ID(或其他主键)从缓存存储中检索模型
用法
- 实现 Cacheable 接口后使用 HasCache 特性
use chilltvpack\CachingModel\Contracts\Cacheable; use chilltvpack\CachingModel\HasCache; class Setting extends Model implements Cacheable { use HasCache; ... }
- 使用
fromCache()
静态方法从缓存存储中检索模型数据
$cachedInstance = Setting::fromCache()->find($key);
可用方法
- public static function primaryCacheKey(): string;
- 返回创建缓存键的主键
- 默认: id
- public static function getCacheKey($id): string;
- 返回具有主键 $id 的特定实例的缓存键
- public static function cacheTimeout(): int;
- 返回缓存超时时间
- public function scopeCacheWithRelation($query);
- 特定关系将一起缓存模型
public function scopeCacheWithRelation($query) { return $query->with('relationship:id); }