I would like to cache getText
method and update cache on setText
. By now cache on getText
method works, but I can not update value. Code below is base on tutorial from Hazelcast.
@Service
public class SlowService {
String text = "Initial value";
@Cacheable("text")
public String getText() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return text;
}
@CachePut(value = "text", key = "#newText + 1")
public String setText(String newText) {
text = newText;
return text;
}
}
How to improve code above to make @CachePut
annotation work?
EDIT: Tried adding same keys:
@Cacheable(value = "text", key = "#someKey")
public String getText() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return text;
}
@CachePut(key = "#someKey")
public String setText(String newText) {
text = newText;
return text;
}
But getting:
java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public java.lang.String com.example.demo.SlowService.getText()] caches=[text] | key='#someKey' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'