tannhatcms / laravel-caching-model
为 Laravel Eloquent 模型提供简单缓存库
1.0.2
2023-02-20 18:20 UTC
Requires
- php: ^7.0|^8.0
- laravel/framework: ^6.0|^7.0|^8.0|^9.0|^10.0
Replaces
README
- 用于缓存 Laravel Eloquent 模型的简单包
- 支持通过 id(或其他主键)检索缓存存储中存储的模型
使用方法
- 实现 Cacheable 接口后使用 HasCache 特性
use Hacoidev\CachingModel\Contracts\Cacheable; use Hacoidev\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); }