Skip to content

Commit

Permalink
Merge pull request #391 from terrestris/fasterinterceptor
Browse files Browse the repository at this point in the history
Speedup interceptor by using cached rules
  • Loading branch information
weskamm committed Aug 5, 2020
2 parents aef8127 + 95bc391 commit e554c45
Showing 1 changed file with 34 additions and 1 deletion.
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

0 comments on commit e554c45

Please sign in to comment.