весна - используя кеш - PullRequest
       6

весна - используя кеш

0 голосов
/ 07 октября 2018

У меня есть проект Spring, и мне нужно использовать кеш в своем классе обслуживания, например:

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.");
         }
      }
 }

У меня есть registerRequestStorage кеш, и я помещаю некоторые данные в кеш, используя метод startRegistration.Но когда я пытаюсь получить эти данные другим способом, кеш становится пустым.

 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()
        );
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...