0

I have a spring project, and I need to use cache in my service class, like this:

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

@Service 
public class WebAuthnServer {  
      private final Cache<String, RegistrationRequest> registerRequestStorage = newCache(); 

      private static <K, V> Cache<K, V> newCache() {
        return CacheBuilder.newBuilder()
        .maximumSize(100)
        .expireAfterAccess(10, TimeUnit.MINUTES)
        .build();
      }

      public Either<String, RegistrationRequest> startRegistration(String username, String displayName, String credentialNickname, boolean requireResidentKey) { 
        if (userStorage.getRegistrationsByUsername(username).isEmpty()) { 
             RegistrationRequest request = new RegistrationRequest(...);            
             registerRequestStorage.put(request.getRequestId(), request);
         } else {
             return new Left("The username \"" + username + "\" is already registered.");
         }
      }
 }

I have registerRequestStorage cache, and I put some data in cache using the method startRegistration. But when I try to get this data in another method, the cache is empty.

 public Either<List<String>, SuccessfulRegistrationResult> finishRegistration(String responseJson) { 
    RegistrationResponse response = null;
    try {
        response = jsonMapper.readValue(responseJson, RegistrationResponse.class); 
    } catch (IOException e) { 
        return Left.apply(Arrays.asList("Registration failed!", "Failed to decode response object.", e.getMessage()));
    }

    RegistrationRequest request = registerRequestStorage.getIfPresent(response.getRequestId());
    registerRequestStorage.invalidate(response.getRequestId()); 

    if (request == null) {
        logger.info("fail finishRegistration responseJson: {}", responseJson);
        return Left.apply(Arrays.asList("Registration failed!", "No such registration in progress."));
    } else {
        Try<RegistrationResult> registrationTry = rp.finishRegistration(
            request.getPublicKeyCredentialCreationOptions(),
            response.getCredential(),
            Optional.empty()
        );
    }
}
  • Where's your code which gets the data? also you notice the expiration of 10 minutes? – Ori Marko Oct 07 '18 at 12:06
  • I added the code. Yes, but the time between request of the first and second method is less than 10 minutes – Gabriela Silva Oct 07 '18 at 12:10
  • See https://stackoverflow.com/questions/32093019/spring-using-google-guava-cache and https://stackoverflow.com/questions/44175085/why-spring-deprecate-guava-cache-in-official-document – Ori Marko Oct 07 '18 at 12:24
  • I tried use caffeine, but I get the same erro, my cache is empty https://gist.github.com/I-am-Gabi/da2dbef046ca393a3039da44e3262fb9 – Gabriela Silva Oct 07 '18 at 14:23
  • where is userStorage in your method? – Huy Nguyen Oct 07 '18 at 14:38
  • here https://gist.github.com/I-am-Gabi/da2dbef046ca393a3039da44e3262fb9#file-webauthnserver-java-L13 – Gabriela Silva Oct 07 '18 at 14:49
  • I can't find a simple way to cache `registerRequestStorage`, I just need keep this data in cache – Gabriela Silva Oct 07 '18 at 14:51
  • Actually, your code is quite long and complicated. So other developer is hard to read all of it. I recommend you should create a simple demo to check the caching firstly. – Huy Nguyen Oct 07 '18 at 15:14
  • Remark: Please don't post this on stackoverflow, since it is actually not a question, but you need debugging help with actual code. The question is also not related to Spring. To your code: I would remove the line `registerRequestStorage.invalidate(response.getRequestId());`, because it may lead to trouble if you get some call backs twice. The cache size is limited, so additional cleanup is not absolutely needed. Besides that its normal debugging: check that your request ids are really identical and don't contain any cruft. Maybe inspect the whole cache content. etc. – cruftex Oct 10 '18 at 13:45

0 Answers0