Skip to content

Commit

Permalink
Cache resource index queries (see #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Jan 27, 2017
1 parent e72b34a commit 09e3916
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/controllers/resources/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.fasterxml.jackson.databind.JsonNode;

import play.Logger;
import play.cache.Cache;
import play.libs.Json;

/**
Expand Down Expand Up @@ -77,7 +78,14 @@ public Index queryResources(String q) {
*/
public Index queryResources(String q, int from, int size, String sort,
String owner) {
return withClient((Client client) -> {
String cacheId =
String.format("index-%s.%s.%s.%s.%s", q, from, size, sort, owner);
Index index = (Index) Cache.get(cacheId);
if (index != null) {
return index;
}
Logger.debug("Not cached: " + cacheId);
Index resultIndex = withClient((Client client) -> {
QueryBuilder query = owner.isEmpty() ? QueryBuilders.queryStringQuery(q)
: ownerQuery(q, owner);
Logger.trace("queryResources: q={}, from={}, size={}, sort={}, query={}",
Expand All @@ -102,7 +110,8 @@ public Index queryResources(String q, int from, int size, String sort,
result = Json.toJson(results);
total = hits.getTotalHits();
});

Cache.set(cacheId, resultIndex, Application.ONE_HOUR);
return resultIndex;
}

private static QueryBuilder ownerQuery(String q, String owner) {
Expand Down

0 comments on commit 09e3916

Please sign in to comment.