To call a method I use a hashmap with Lambda Expressions to find out which method should be called to lookup data. The problem is: if I call this method directly - the cache is working without any problem. But if I get this method from a map - no cache it all. Is there a possibility to fix it or Spring Cache can not be used for such scenario?
@Component
@Scope("singleton")
@SuppressWarnings("SpringJavaAutowiringInspection")
public class RulesZ
{
// Lookup is marked as @FunctionalInterface
Map<String,Lookup> lookups;
@Autowired
LookupZMapper lookzMapper;
@PostConstruct
public void init()
{
lookups = new LinkedHashMap<String,Lookup>();
lookups.put("shortform",(String kur, String type,String typeAddition)-> getKurForm(kur,type,typeAddition));
}
@Cacheable(value="lookupKur")
public String getKurForm(String kur, String type, String typeAddition)
{
LookupZtExample ex = new LookupZExample();
ex.createCriteria()...;
List res = lookzMapper.selectByExample(ex);
...
return "found value";
}
That means: if I call the method getKurForm directly - it will be cached, but if I call it using:
RulesZ.getLookups().get("shortform").lookup("val1","DF","DS"));
Than it will search in a DB for every method call.