Eloquent(没有Laravel)缓存实现


Eloquent (without Laravel) caching implementation

我使用的是没有Laravel的Eloquent,我想知道是否有一种方法可以用来(不依赖于Laravel组件)集成缓存方法,然后自动缓存所有模型查询(缓存后端可以是可变的,比如APCu或memcache)。

我认为应该可以编写一个处理此问题的模型基类,但我不太确定如何实现它。有人对此有什么想法吗?

如果您想自动缓存查询,您必须重写find()、findOrFail(),where()。。。方法

由于Eloquent是如何构建的,您不能简单地在自定义模型类中添加方法find()

https://laracasts.com/discuss/channels/eloquent/override-find-method/replies/72028

class MyCacheModel extends 'Illuminate'Database'Eloquent'Model
{
// override methods as explained in previous link
// cache the result in redis for how long you want
}

然后在您的模型中,而不是扩展Eloquent''model,现在从MyCacheModel扩展。通过一些自定义,您可以设置缓存查询的时间,如果不应该缓存模型,则只需使用Eloquent''model。