Skip to content

SSMCacheManager set/add inconsistency #84

@nowaklukasz

Description

@nowaklukasz

I recently stumbled upon following issue.

Behaviour of the setCaches in SSMCacheManager differs based on the way we create the manager. If it is annotated as @Bean - everything works as a charm, but if it is simple factory method - we need to use setter to setup the collection and then add each cache separately.

As the code serves as thousands of words, this works:

@Bean
public CacheManager cacheManager() {
            return new CompositeCacheManager(
                    inMemoryCache(),
                    ssmCacheManager());
}

@Bean
public SSMCacheManager ssmCacheManager() {
            SSMCache cache = createCache(CACHE_NAME);
            SSMCacheManager ssmCM = new SSMCacheManager();
            ssmCM.setCaches(ImmutableList.of(cache))
}

this does not:

@Bean
public CacheManager cacheManager() {
            return new CompositeCacheManager(
                    inMemoryCache(),
                    ssmCacheManager());
}

private SSMCacheManager ssmCacheManager() {
            SSMCache cache = createCache(CACHE_NAME);
            SSMCacheManager ssmCM = new SSMCacheManager();
            ssmCM.setCaches(ImmutableList.of(cache))
}

the only way to make it working is to call afterPropertiesSet manually or setCaches with new collection and call addCache for each cache separately.

Solution:

  1. At least a documentation so that one does not have to go through the code
  2. If the SSMCacheManager is not created as a bean, maybe some fallback for getCache? so that if cacheMap is empty or if cacheMap.get(name) == null it would check caches collection for the name?
  3. setter could register caches

Background:
We decided to introduce in-memory cache in front of memcache (aws elasticache to be specific) to cut delays that are caused by network. As a result, we started returning CompositeCacheManager as a CacheManager bean, and method that created SSMCacheManager became a private method (we removed the Bean annotation). We found the hard way, that cache stopped working - it did not find proper cache names. Simple change to addCache also did not help - there was null pointer exception.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions