Answer the question
In order to leave comments, you need to log in
The query caching time affects the business logic. What to do?
The problem is insanely simple, but I do not see an adequate solution.
I have the entity News.
The news, among other things, has a publication date property (date_publication) that determines whether the news can be shown. That is the date after which the news can be published on the site.
This is the main business rule/restriction.
Accordingly, when displaying a list of news, I have to compare the Date of publication with the current time, and that's where I run into a caching problem.
We all know that requests using the current time are not cached. For example, MySQL does not cache queries that use the NOW()
or function CURRENT_TIMESTAMP()
. Therefore, I need to explicitly pass the current date:date_publication <= "2017-04-10 13:50:00"
News is content that is usually not updated often, especially on resources where news is not the root context (Сore Сontext) .
I want to cache for 10 minutes a database query that returns a list of news. And in order for this to work, it's not enough for me to simply say - cache this request for 10 minutes. Here I run into a number of restrictions:
setResultCacheLifetime()
for caching query results , which automatically generates a cache ID based on the query parameters;setResultCacheId()
, but this leads to overcomplication and, in fact, the implementation of almost the same functionality as the default;$now = new \DateTime();
// округляем дату вниз чтоб в выборку не попали данные которых там быть не должно
$now->setTimestamp(floor($now->getTimestamp() / $cacheLifetime) * $cacheLifetime);
$result = $rep->match(
new AndX(
new NewsPublished(),
new Slice($slice_size, $slice_number)
),
new Cache(600)
);
$result = $rep->match(
new AndX(
new Equals('enabled', true),
new LessOrEqualThan('date_publication', new \DateTime()),
new Limit($slice_size),
new Offset(($slice_number - 1) * $slice_size)
),
new Cache(600)
);
match()
that parses the specifications and collects an SQL query based on them. To apply the specifications, I use a third-party library and its author disagrees with me . Answer the question
In order to leave comments, you need to log in
We all know that requests using the current time are not cached. For example, MySQL does not cache queries that use the NOW() or CURRENT_TIMESTAMP() function. Therefore, I need to explicitly pass the current date:
You're not caching there.
Relying on MySQL for caching is a stupid, inefficient strategy.
You are looking for a problem in the wrong place.
I asked this question to the guys from Doctrine and they made me understand that this is not their concern and this task should be solved at a different level.
published (bool)
, for example, and post pending news using the scheduler. SELECT FROM `posts` WHERE `published` = 1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question