Skip to content

Commit

Permalink
disable cache at query level
Browse files Browse the repository at this point in the history
  • Loading branch information
antleb committed Feb 13, 2024
1 parent 5a167d1 commit 0aef108
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ public class Query {
private int limit;
private String orderBy;

boolean useCache = true;

public Query() {}

public Query(String name, List<Object> parameters, List<FilterGroup> filters, List<Select> select, String entity, String profile, int limit, String orderBy) {
public Query(String name, List<Object> parameters, List<FilterGroup> filters, List<Select> select, String entity, String profile, int limit, String orderBy, boolean useCache) {
this.name = name;
this.parameters = parameters;
this.filters = filters;
Expand All @@ -23,6 +25,7 @@ public Query(String name, List<Object> parameters, List<FilterGroup> filters, Li
this.profile = profile;
this.limit = limit;
this.orderBy = orderBy;
this.useCache = useCache;
}

public List<FilterGroup> getFilters() {
Expand Down Expand Up @@ -89,6 +92,14 @@ public void setParameters(List<Object> parameters) {
this.parameters = parameters;
}

public boolean isUseCache() {
return useCache;
}

public void setUseCache(boolean useCache) {
this.useCache = useCache;
}

@Override
public String toString() {
return "Query{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,22 @@ public List<Result> query(List<Query> queryList, String orderBy) throws StatsSer
throw new StatsServiceException("query " + queryName + " not found!");
}

cacheKey = StatsCache.getCacheKey(querySql, parameters, profile);

if (statsCache.exists(cacheKey)) {
result = statsCache.get(cacheKey);

log.debug("Key " + cacheKey + " in cache! Returning: " + result);
if (query.isUseCache) {
cacheKey = StatsCache.getCacheKey(querySql, parameters, profile);

if (statsCache.exists(cacheKey)) {
result = statsCache.get(cacheKey);

log.debug("Key " + cacheKey + " in cache! Returning: " + result);
} else {
log.debug("result for key " + cacheKey + " not in cache. Querying db!");
result = statsRepository.executeQuery(querySql, parameters, profile);
log.debug("result: " + result);
statsCache.save(new QueryWithParameters(querySql, parameters, profile), result);
}
} else {
log.debug("result for key " + cacheKey + " not in cache. Querying db!");
log.debug("Cache disabled for query.");
result = statsRepository.executeQuery(querySql, parameters, profile);
log.debug("result: " + result);
statsCache.save(new QueryWithParameters(querySql, parameters, profile), result);
}

results.add(result);
Expand Down

0 comments on commit 0aef108

Please sign in to comment.