D
D
D3lphi2017-03-28 17:13:41
Laravel
D3lphi, 2017-03-28 17:13:41

Caching: repository or model?

I have wondered more than once, read the answers on the toaster about this, but have never seen a clear answer. So, the question is: Where is it more correct, from the point of view of architecture (In a Laravel application), to store the caching logic (For example, if there is data in the cache, pull it out, otherwise give the data from the database) in the repository or in the model? It seems like the model is responsible for working with the data, although on the other hand, I heard the opinion that the model is a simple POPO.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Falseclock, 2017-03-28
@Falseclock

I wrote my driver 10 years ago and I don't know any problems.

$sth = $db->prepare("SELECT * FROM foo");
$sth->cache("Foo:*");
$sth->execute();

the execute method checks whether the value of the cache key was and if it is, pulls the data from the cache. If it is empty, it makes a request to the database. I wrote once and forgot for many years.
Without specifying a cache, data will always be pulled from the database
$sth = $db->prepare("SELECT * FROM foo");
$sth->execute();

D
Dmitry Evgrafovich, 2017-03-28
@Tantacula

above the repository. I did the following: I write a repository, its interface, then through the container service I bind the interface to the implementation. Later, another code-code-code, then after a while I already write the cache layer and all the caching logic. The caching class inherits the same interface and, when written, is substituted into the service container instead of the current implementation. You can put the caching layer even higher, everything depends on your architecture (therefore, there will be no unequivocal answer either), but I personally don’t see the point below, maybe I just didn’t come across examples when it was required. And the architecture within Laravel is not clearly defined. There are some tools described in the documentation, but you can build the architecture in the way that seems most correct to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question