@jpinto.ptn This doesn’t need a package. You just need an abstraction layer, such as a repository.
What's the most efficient way to cache models?
The main purpose of my Laravel projects is managing large amounts of data. Because a lot of it seldom changes, and to easing the load on the database, I implemented some automatic caching features (so a model is cached on update, and something like User::cached(1, 'name') works without any SQL queries). Since I use them on all my projects I was thinking of turning them into a package.
In doing so, I looked into what might be the best way to cache models. I first tried to compare storing the models themselves to storing their attributes only. Storing models had a minor speed advantage (< 4%) but a major memory usage penalty (3x as much, on Redis). Also, loading models was about 10% faster than loading attributes and creating a Model object from them.
To be precise, as "storing attributes" I mean storing the result of $model->getAttributes() and hydrating through (new static())->newFromBuilder($cachedAttributes) when loading.
Are there any ways to optimize this in a meaningful way? Or am I basically stuck between caching low-memory-usage, slightly-slower attributes or high-memory-usage, slightly-faster models? Caching casted attributes rather than their raw value might allow for some middle-ground, but it feels like it might add some added complexity as well.
Any suggestions on how to best approach this problem would be most welcome. Thanks in advance!
Please or to participate in this conversation.