I'm trying to use Spring Cache to store data, generated by another method inside Service class.

This method marked with @Cacheable is a public method, the cache is being called in Controller layer.

When I do debugging, I inspect the object cacheManager, I found that it contains the map that I stored, but when calling the method cacheManager.getCache("cache") it return null.

Question is why that method return null while the object is holding the value?

This is the config, service and controller: Spring bean config:

@EnableCaching public class CachingConfig { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("optCache"); } } 

Service:

public void verify(Request request, String authorization) { String memberId = parseAuthToken(authorization).getMembershipID(); buildOtpCache(memberId, request.getTokenUUID(), 0) } @Cacheable("otpCache") public OTPCache buildOtpCache(String memberId, String uuid, int counter) { return OTPCache.builder() .memberId(memberId) .tokenUUID(uuid) .timestamp(LocalDateTime.now()) .counter(counter) .build(); } 

Controller:

@Override public void verifyOTP(MeOTPVerifyRequest verifyOTPRequest, String authorization) { String memberId = parseAuthToken(authorization).getMembershipID(); Collection<String> a = cacheManager.getCacheNames(); OTPCache OTPCache = cacheManager.getCache("cache").get(memberId, OTPCache.class); otpService.verify(verifyOTPRequest, authorization); } 

EDIT:

This is my new service class, remove @Cacheable annotation:

public void verify(Request request, String authorization) { String memberId = parseAuthToken(authorization).getMembershipID(); cacheManager.getCache("optCache").put(memberId, buildOtpCache(memberId, request.getTokenUUID(), 0)); } public OTPCache buildOtpCache(String memberId, String uuid, int counter) { return OTPCache.builder() .memberId(memberId) .tokenUUID(uuid) .timestamp(LocalDateTime.now()) .counter(counter) .build(); } 
3

Related questions 1 Spring + Ehcache : Not able to get the cache back from cacheManager without key 94 How do I tell Spring cache not to cache null value in @Cacheable annotation 1 Spring Cacheable does not work Related questions 1 Spring + Ehcache : Not able to get the cache back from cacheManager without key 94 How do I tell Spring cache not to cache null value in @Cacheable annotation 1 Spring Cacheable does not work 17 spring cache - Null key returned for cache operation 1 Spring Boot @CachePut value is null 4 Spring cache - "Null key returned for cache operation" 4 Spring Cache doesn't ignore Null key even I set the condition 0 @Cacheable() not returning proper cache 5 @Cacheable doesn't intercept the method, cache is always empty 0 Spring boot Cache not working expected, returning existing records Load 7 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.