Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup interceptor by using cached rules #391

Merged
merged 1 commit into from
Aug 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package de.terrestris.shoguncore.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.terrestris.shoguncore.dao.InterceptorRuleDao;
import de.terrestris.shoguncore.model.interceptor.InterceptorRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
Expand All @@ -20,6 +24,11 @@
public class InterceptorRuleService<E extends InterceptorRule, D extends InterceptorRuleDao<E>>
extends PermissionAwareCrudService<E, D> {

/**
* The cached rules for faster access
*/
HashMap<String, List<E>> cachedRules = new HashMap();

/**
* Default constructor, which calls the type-constructor
*/
Expand All @@ -43,7 +52,31 @@ protected InterceptorRuleService(Class<E> entityClass) {
*/
@Transactional(readOnly = true)
public List<E> findAllRulesForServiceAndEvent(String service, String event) {
return this.dao.findAllRulesForServiceAndEvent(service, event);
if (this.cachedRules.containsKey(service + "," + event)) {
return this.cachedRules.get(service + "," + event);
} else {
List<E> rules = this.dao.findAllRulesForServiceAndEvent(service, event);
this.cachedRules.put(service + "," + event, rules);
return rules;
}
}

@Override
public void saveOrUpdate(E e) {
this.cachedRules.clear();
super.saveOrUpdate(e);
}

@Override
public E updatePartialWithJsonNode(E entity, JsonNode jsonObject, ObjectMapper objectMapper) throws IOException {
this.cachedRules.clear();
return super.updatePartialWithJsonNode(entity, jsonObject, objectMapper);
}

@Override
public void delete(E e) {
this.cachedRules.clear();
super.delete(e);
}

/**
Expand Down