Cache not refreshing when being called from a asynchrounous function in Spring - caching

I am calling a function which has CacheEvict annotation on it. This is being called from a function that is itself executed asynchronously.
It seems that the cache is not being evicted after the function has been executed.
Here is sample code
#Async("executor1")
public void function1()
{
// do something
anotherFunction("name", 123, 12);
// do something more
}
#CacheEvict(cacheNames = {"cache1", "cache2", "cache3"}, key = "#testId")
public List<Integer> anotherFunction(String name, int testId, int packageId)
{
// some code here
}
What I want is that entries corresponding to testId should be cleared from all the caches.
However, in another call, I can see old entries of cache1. function1 is being called from the controller. Both these functions are present inside the service. Now, Is this configuration correct? If yes, What may be the possible reasons that cache is not being cleared?
Any help appreciated. Thanks in advance.

I think your problem is that Spring proxies are not reentrant. To implement Async and CacheEvict, Spring creates a proxy. So, in your example, the call stack will be:
A -> B$$proxy.function1() -> B.function1() -> B.anotherFunction()
B$$proxy contains the logic for async and eviction. Which won't apply when calling directly anotherFunction. In fact, even if you remove the #Async, it will still don't work.
A trick you can use is to inject the proxied bean into the class. To delegate to the proxy of the class instead this.
public class MyClass {
private MyClass meWithAProxy;
#Autowired
ApplicationContext applicationContext;
#PostConstruct
public void init() {
meWithAProxy = applicationContext.getBean(MyClass.class);
}
#Async("executor1")
public void function1() {
meWithAProxy.anotherFunction("name", 123, 12);
}
#CacheEvict(cacheNames = "cache1", key = "#testId")
public List<Integer> anotherFunction(String name, int testId, int packageId) {
return Collections.emptyList();
}
}
It works. But there's a catch. If you now call anotherFunction directly, it won't work. I consider this to be a Spring bug and will file it as is.

Related

How to mock a particular method of a spring bean

I have a spring bean with multiple APIs. Mocking the bean doesn't serve my purpose as I would like to verify fetchFromDb() called only once on multiple calls to getCachedData() with the same input. This is to make sure the result is cached.
Is it possible to mock fetchFromDb() on bean 'market' while calling getCachedData()?
Sample Class
#Configuration("market")
public class AllMarket {
#Autowired
private CacheManager cachedData;
public boolean getCachedData(LocalDate giveDate) {
//check if it exists in cache
if(Objects.nonNull(checkCache(giveDate)) {
return checkCache(giveDate);
}
//fetch from database
boolean bool = fetchFromDb(givenDate);
cacheData(giveDate, bool);
return bool;
}
public boolean checkCache(LocalDate giveDate) {
return cacheManager.getData(givenDate);
}
public boolean fetchFromDb(LocalDate givenDate) {
//return the data from database
}
public void cacheData(LocalDate givenDate, boolean bool) {
cacheManager.addToCache(givenDate, bool);
}
}
You can use Mockito.spy() for this kind of test. In this case you should spy your AllMarket instance and stub fetchFromDb. At the end you can Mockito.verify that fetchFromDb was called exactly once. It will look something like this:
AllMarket spy = spy(allMarket);
when(spy.fetchFromDb(givenDate)).thenReturn(true); //you have boolean as a return type
...
verify(spy, times(1)).fetchFromDb(givenDate);
For more information, you can see Official Mockito doc
Maybe mockito argument captor could asist you. It lets you to capture method input and how many times method was called, also may other functions. Please check https://www.baeldung.com/mockito-annotations.

Spring cache does not cache anything

I'm using spring-boot-starter-parent version 2.0.1
these are the application.properties
spring.cache.type=redis
spring.cache.cache-names=edges
spring.cache.redis.cache-null-values=false
spring.cache.redis.time-to-live=60000000
spring.cache.redis.key-prefix=true
spring.redis.host=localhost
spring.redis.port=6379
This is the main class.
#SpringBootApplication
#EnableAsync
#EnableCaching
public class JanusApplication {
public static void main(String[] args) {
SpringApplication.run(JanusApplication.class, args);
}
}
This's the java method that I want to cache result of it.
#Service
public class GremlinService {
#Cacheable(value = "edges")
public String getEdgeId(long fromId, long toId, String label) {
// basically finds an edge in graph database
}
public Edge createEdge(Vertex from, Vertex to, String label){
String edgeId = getEdgeId((Long) from.id(), (Long) to.id(), label);
if (!Util.isEmpty(edgeId)) {
// if edge created before, use its id to query it again
return getEdgeById(edgeId);
} else {
return createNewEdge((Long) from.id(), (Long) to.id(), label);
}
}
}
I don't have any other configuration for redis or cache. Although it does not throw any error, it does not cache anything. I check with redis-cli.
In order for the caching to work, the function to be cached must be called from an external class.
That's because Spring creates a proxy for your bean and resolves the caching when the method call passes through that proxy.
If the function call is done internally, it doesn't pass the proxy and thus the caching is not applied.
Here's another answer that adresses this question: Spring cache #Cacheable method ignored when called from within the same class

Spring Cache Abstraction: How to Deal With java.util.Optional<T>

We have a lot of code in our code base that's similar to the following interface:
public interface SomethingService {
#Cacheable(value = "singleSomething")
Optional<Something> fetchSingle(int somethingId);
// more methods...
}
This works fine as long we're only using local caches. But as soon as we're using a distributed cache like Hazelcast, things start to break because java.util.Optional<T> is not serializable and thus cannot be cached.
With what I've come up so far to solve this problem:
Removing java.util.Optional<T> from the method definitions and instead checking for the trusty null.
Unwrapping java.util.Optional<T> before caching the actual value.
I want to avoid (1) because it would involve a lot of refactoring. And I have no idea how to accomplish (2) without implementing my own org.springframework.cache.Cache.
What other options do I have? I would prefer a generic (Spring) solution that would work with most distributed caches (Hazelcast, Infinispan, ...) but I would accept a Hazelcast-only option too.
A potential solution would be to register a serializer for the Optional type. Hazelcast has a flexibile serialization API and you can register a serializer for any type.
For more information see the following example:
https://github.com/hazelcast/hazelcast-code-samples/tree/master/serialization/stream-serializer
So something like this:
public class OptionalSerializer implements StreamSerializer<Optional> {
#Override
public void write(ObjectDataOutput out, Optional object) throws IOException {
if(object.isPresent()){
out.writeObject(object.get());
}else{
out.writeObject(null);
}
}
#Override
public Optional read(ObjectDataInput in) throws IOException {
Object result = in.readObject();
return result == null?Optional.empty():Optional.of(result);
}
#Override
public int getTypeId() {
return 0;//todo:
}
#Override
public void destroy() {
}
}
However the solution isn't perfect because this Optional thing will be part of the actual storage. So internally the Optional wrapper is also stored and this can lead to problems with e.g. queries.

CacheOutput Attribute Ignoring Azure-hosted Redis Cache

I have just implemented output caching of my Asp.Net Web API Controllers using StrathWeb's library connecting to the StackExchange.Redis library connecting through to an Azure-hosted Redis Cache.
I have written a custom class that implements the StrathWeb IApiOutputCache interface and calls the equivalent StackExchange methods. This is registered as the cache output provder in Global.asax.cs.
Here's an example of usage:
public class MyApiController : ApiController
{
private const int FIFTEEN_MINUTES_IN_SECONDS = 900;
[CacheOutput(ClientTimeSpan = FIFTEEN_MINUTES_IN_SECONDS, ServerTimeSpan = FIFTEEN_MINUTES_IN_SECONDS)]
async public Task<Data> GetAsync(int param1, string param2)
{
return await GetExpensiveData();
}
[Serializable]
public class Data
{
// Members omitted for brevity
}
}
When a call is made to the api endpoint I can see that the framework correctly calls all the required methods on my IApiOutputCache class: Contains, Set and Get. However, even when a cached copy is found and returned, the GetExpensiveData() method is always run and the 'fresh' data returned.
No errors are thrown. The cache seems to be working. Yet, my expensive code is always called.
Thanks for your help :).
Problem solved. I was incorrectly calling into Redis from my IApiOutputCache class.
Before...
public class AzureRedisApiOutputCache : IApiOutputCache
{
public object Get(string key)
{
return AzureRedisCache.Instance.GetDatabase().StringGet(key);
}
}
After...
public class AzureRedisApiOutputCache : IApiOutputCache
{
public object Get(string key)
{
// Call the extension method that also performs deserialization...
return AzureRedisCache.Instance.GetDatabase().Get(key);
}
}
public static class RedisDatabaseExtensions
{
public static object Get(this IDatabase cache, string key)
{
return Deserialize<object>(cache.StringGet(key));
}
}
This confused me for some time as the CacheOutput framework never reported an error. It just silently failed and fell back to the controller method.

Using #SubscribeMapping annotated method for RPC-like behavior when return value is deferred

I really like #SubscribeMapping approach to implement RPC-like semantic with STOMP-over-Websocket.
Unfortunately its "magic" requires that annotated method returns a value. But what if return value is not readily available? I want to avoid blocking inside the method waiting for it. Instead I'd like to pass a callback that will publish a value when it's ready. I thought I could use messaging template's convertAndSendToUser() inside a callback to do that. Turns out #SubscribeMapping handling is quite special and is not possible with instance of SimpMessageSendingOperations.
I was able to achieve my goal by calling handleReturnValue() on a SubscriptionMethodReturnValueHandler, but the overall mechanics of this is very tedious if not hackish (like providing dummy instance of MethodParameter to handleReturnValue()):
public class MessageController {
private final SubscriptionMethodReturnValueHandler subscriptionMethodReturnValueHandler;
#Autowired
public MessageController(SimpAnnotationMethodMessageHandler annotationMethodMessageHandler) {
SubscriptionMethodReturnValueHandler subscriptionMethodReturnValueHandler = null;
for (HandlerMethodReturnValueHandler returnValueHandler : annotationMethodMessageHandler.getReturnValueHandlers()) {
if (returnValueHandler instanceof SubscriptionMethodReturnValueHandler) {
subscriptionMethodReturnValueHandler = (SubscriptionMethodReturnValueHandler) returnValueHandler;
break;
}
}
this.subscriptionMethodReturnValueHandler = subscriptionMethodReturnValueHandler;
}
#SubscribeMapping("/greeting/{name}")
public void greet(#DestinationVariable String name, Message<?> message) throws Exception {
subscriptionMethodReturnValueHandler.handleReturnValue("Hello " + name, new MethodParameter(Object.class.getMethods()[0], -1), message);
}
}
So my question is simple: Is there a better way?

Resources