top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Caching Error that we missed!..


@Configuration
public class CacheConfig implements UserCache{

	private static final Logger logger = LoggerFactory.getLogger(CacheConfig.class);
	
	CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration;
	
	/* Cache Provider */
	CachingProvider cachingProvider ;
	
	CacheManager cacheManager;
	
	public CacheConfig() {
		
		/*Cache event listener*/
		cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
				.newEventListenerConfiguration(new CustomCacheEventLogger(), EventType.EVICTED, EventType.EXPIRED,
						EventType.CREATED, EventType.UPDATED, EventType.REMOVED)
				.unordered().asynchronous();
		
		cachingProvider = Caching.getCachingProvider();
	}


	@Bean
	public CacheManager ehcacheManagerForUserLogin() { 	
		logger.info("Userlogin Cache being configures");

		/* Cache Configuration less heap larger TTL */
		@SuppressWarnings("deprecation")
		CacheConfiguration<String, UserLogin> cachecUserLogin = CacheConfigurationBuilder
				.newCacheConfigurationBuilder(String.class, UserLogin.class,
						ResourcePoolsBuilder.newResourcePoolsBuilder().heap(250, EntryUnit.ENTRIES))
				.add(cacheEventListenerConfiguration)
				.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(1000))).build();
		
			/* Cache CacheManager */
		CacheManager cacheUserLogin = cachingProvider.getCacheManager();

		javax.cache.configuration.Configuration<String, UserLogin> configurationUD = Eh107Configuration
				.fromEhcacheCacheConfiguration(cachecUserLogin);

		cacheUserLogin.createCache("UserloginCache", configurationUD);
		return cacheUserLogin;
	}

	@Override
	public UserDetails getUserFromCache(String username) {
		// TODO Auto-generated method stubcacheManger
		return cacheManager.getCache("UserloginCache").unwrap(UserLogin.class);
	}

	@Override
	public void putUserInCache(UserDetails user) {
		cacheManager.getCache("UserloginCache").put(user.getUsername(), user);
	}

	@Override
	public void removeUserFromCache(String username) {
		cacheManager.getCache("UserloginCache").remove(username);
	}
	
	

}

If you check the getUserFromCache() method, the developer is not returning the object for the passed key.

I fixed it like below.


	@Override
	public UserDetails getUserFromCache(String username) {
		Cache<String, UserDetailsImpl> cache = cacheManager.getCache("UserDetailsImplCache", String.class, UserDetailsImpl.class);
		return cache.get(username);
	}

But then it was throwing null pointer exception at cacheManager ( It was null ).

When you look at the code, you will see that this is a configuration class, you are returning CacheManager Bean here. In the same class, if you are trying to access CacheManager bean in another method, it has to be injected using one of bean injection methodology. simply declaring it and using will not work.

And it has to be lazzy loaded injection.


I tried @Autowired

@Lazy And it worked .


@Configuration
public class CacheConfig implements UserCache{

	private static final Logger logger = LoggerFactory.getLogger(CacheConfig.class);
	
	CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration;
	
	/* Cache Provider */
	CachingProvider cachingProvider ;

	@Autowired
	@Lazy
	CacheManager cacheManager;
	
	public CacheConfig() {
		/*Cache event listener*/
		cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
				.newEventListenerConfiguration(new CustomCacheEventLogger(), EventType.EVICTED, EventType.EXPIRED,
						EventType.CREATED, EventType.UPDATED, EventType.REMOVED)
				.unordered().asynchronous();
		
		cachingProvider = Caching.getCachingProvider();
	}


	@Bean
	public CacheManager ehcacheManagerForUserDetailsImpl() {
		logger.info("UserDetailsImpl Cache being configured");
		
		/* Cache CacheManager */
		CacheManager cacheMgrUserDetailsImpl = cachingProvider.getCacheManager();

		/* Cache Configuration less heap larger TTL */
		@SuppressWarnings("deprecation")
		CacheConfiguration<String, UserDetailsImpl> cacheConfigUserDetImpl = CacheConfigurationBuilder
				.newCacheConfigurationBuilder(String.class, UserDetailsImpl.class,
						ResourcePoolsBuilder.newResourcePoolsBuilder().heap(300, EntryUnit.ENTRIES))
				.add(cacheEventListenerConfiguration)
				.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(1000))).build(); // 1000 sec = 16 mins

		javax.cache.configuration.Configuration<String, UserDetailsImpl> configurationUD = Eh107Configuration
				.fromEhcacheCacheConfiguration(cacheConfigUserDetImpl);

		cacheMgrUserDetailsImpl.createCache("UserDetailsImplCache", configurationUD);
		return cacheMgrUserDetailsImpl;
	}

	@Override
	public UserDetails getUserFromCache(String username) {
		Cache<String, UserDetailsImpl> cache = cacheManager.getCache("UserDetailsImplCache", String.class, UserDetailsImpl.class);
		return cache.get(username);
	}

	@Override
	public void putUserInCache(UserDetails user) {
		cacheManager.getCache("UserDetailsImplCache").put(user.getUsername(), user);
	}

	@Override
	public void removeUserFromCache(String username) {
		cacheManager.getCache("UserDetailsImplCache").remove(username);
	}

}

Thank you for reading.. Have fun..

29 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page