In the current project I'm working on I see a lot of developers making the
mistake of caching date filtered queries.

Why shouldn't you cache date filtered queries?
1. If you're filtering by the current moment in time then the results are
irrelevant within a few moments of them being retrieved.  Some records may
be due to expire and others (that were filtered out) may be about to become
active.
2. Despite what I say in #1, there's a bug in the equals method of
EntityDateFilterCondition where all instances appear equal so the cached
result is never replaced until it is expired from the cache. So expired
records remain and future dated records never appear until the TTL is
reached.

The correct approach is to not have a date filter in the query, cache the
result and then filter the result in-memory using
EntityUtil.filterByDate(). An alternative approach is to only filter by
thruDate in the query since expired records will never become active, and
then filter by fromDate after you have the result.  We have no utility
tools in place for the latter approach though.

I'm writing all this because I've written a checker for the entity list and
object caches that inspect the query conditions for fromDate conditions and
if found, log a warning and refuse to cache.  I'd like to add this safety
feature to the OFBiz trunk but I'm wondering if anyone can think of a valid
reason to cache fromDate filtered records? I haven't been able to think of
any but that doesn't necessarily mean there aren't any use cases.

Thanks
Scott

Reply via email to