Implementation of eloquent (without Laravel) caching

I use Eloquent without Laravel, and I wonder if there is a method that you can use (and not rely on Laravel components) to integrate the caching method, which then automatically caches all model requests (backend caching can be variable, APCu or memcache).

I think it should be possible to write a base model class that handles this, but I'm not quite sure how I would do it. Does anyone have any ideas in this direction?

+6
source share
1 answer

If you want to automatically cache your request, you must override the methods find (), findOrFail (), where () ...

Because of how Eloquent is created, you cannot just add the find () method to your own model class

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 } 

Then in your model, instead of the Eloquent \ Model extension, it now continues from your MyCacheModel. With a few settings, you can set how long query caching will take, and if the model should not be cached, just use Eloquent \ Model.

+5
source

All Articles