Static variables as cache possible issues

Viewed 82

I have a scenario with below use case:

  1. The function someFunctionToGetValue takes around 10 seconds to process.
  2. api is called ~300 requests per second.
  3. The data of this api is same for all users.
  4. The data changes rarely.

To handle the throughput I have used static variables as cache which refreshes every 10 minutes.

import javax.ws.rs.PATH;
import com.google.inject.Singleton;

@Path("/home")
@Singleton
class MyResource {

     private static String value;   //this is actually a complex huge pojo

     @Get
     public Response getConfigData(){

           boolean isMinsLapsed=Calender.getInstance().get(Calender.Minute)%10==0;

           if(value==null || isMinsLapsed){
               value=someFunctionToGetValue();
           }
           return value;
     }
}

This code needs to be sent to production and wondering on the below points.

  1. Drawbacks of using static variables as cache in above scenario?
  2. Any better ways of API caching with eviction in place?
1 Answers

There might be a race condition between two calls, the first call queries the value which is null, while performing the heavy someFunctionToGetValue the second call will still see value as null.

This might not be an issue for you, but if you want to make sure the value is only calculated once you can use memoization with expiration (like Guava's)

Supplier<String> memoizedSupplier = Suppliers.memoizeWithExpiration(
  CostlySupplier::generateBigNumber, 5, TimeUnit.SECONDS);

Additionally, since the resource is a singleton, the value argument does not need to be static because it is only instantiated once.

Related