Skip to main content

Query Memoization

Utilities

Efficiently cache the results of any query in memory for an indicated period of time.

Installation

1
api('com.psddev.component-lib:query-memoization-util')

Installing this plugin automatically adds the MemoizingDatabase to the database stack. This can be disabled by setting dari/isQueryMemoizationEnabled to false or per request with the query parameter ?_cache=false

Note that queries are not cached by default; each query must be opted in explicitly.

Usage

1
2
class Test {
3
void main() {
4
5
int memoizationSeconds = 60;
6
Query query = Query.from(Article.class).where("x = y");
7
8
// Enable memoization on this query
9
MemoizingDatabase.setMemoizationSeconds(query, memoizationSeconds);
10
11
// The results of this query are efficiently cached in memory for 60
12
// seconds across all requests.
13
PaginatedResult<Article> results = query.select(0, 10);
14
}
15
}