I have a Spring boot project with a custom CacheResolver , since I need to determine the runtime that I want to use the cache for, I have no compilation errors, but when I do some tests and CacheResolver gap on my custom CacheResolver it never enters into it .
This is my cache class configuration:
@Configuration @EnableCaching(proxyTargetClass = true) @PropertySource(CacheConfig.CLASSPATH_DEPLOY_CACHE_PROPERTIES_PROPERTIES) public class CacheConfig extends CachingConfigurerSupport{ public static final String CLASSPATH_DEPLOY_CACHE_PROPERTIES_PROPERTIES = "classpath:/deploy/cache-properties.properties"; public static final String CACHEABLE_DOCUMENTS_PROPERTY = "cacheable.documents"; public static final String TTL_CACHEABLE_DOCUMENTS_PROPERTY = "ttl.cacheable.documents"; public static final String SIZED_CACHEABLE_DOCUMENTS_PROPERTY = "sized.cacheable.documents"; public static final String CACHE_NAME = "permanentCache"; public static final String TTL_CACHE = "ttlCache"; public static final String SIZED_CACHE = "sizedCache"; public static final String CACHEABLE_DOCUMENTS = "cacheableDocuments"; public static final String SIZED_CACHEABLE_DOCUMENTS = "sizedCacheableDocuments"; public static final int WEIGHT = 1000000; public static final int TO_KBYTES = 1000; @Inject protected Environment environment;
Here is my CacheResolver
public class WgstCacheResolver extends AbstractCacheResolver { private final List<String> cacheableDocuments; private final List<String> sizedCacheableDocuments; public WgstCacheResolver(final CacheManager cacheManager,final List<String> cacheableDocuments, final List<String> sizedCacheableDocuments) { super(cacheManager); this.cacheableDocuments = cacheableDocuments; this.sizedCacheableDocuments = sizedCacheableDocuments; } @Override protected Collection<String> getCacheNames(final CacheOperationInvocationContext<?> context) { final Collection<String> cacheNames = new ArrayList<>(); final AbstractDao dao = (AbstractDao)context.getTarget(); final String documentType = dao.getDocumentType().toString(); if (cacheableDocuments.contains(documentType)){ cacheNames.add("permanentCache"); } if (sizedCacheableDocuments.contains(documentType)){ cacheNames.add("sizedCache"); } return cacheNames; } }
And here is my DAO, where I use the cache:
@Component @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.DEFAULT) @CacheConfig(cacheResolver = "wgstCacheResolver") public class CacheableDao<T extends Storable> extends AbstractDao<T> { private final Logger logger = LoggerFactory.getLogger(CacheableDao.class); public CacheableDao(final Bucket bucket, final Class<T> typeParameterClass, final DocumentType documentType) { super(bucket, typeParameterClass, documentType); } @Cacheable(key = "{#root.methodName, #root.target.generateFullKey(#key)}") public T get(final String key) throws DatastoreAccessException, ObjectMappingException {
I tried to implement CacheResolver instead of the AbstractCacheResolver extension, but that didn't make any difference.
Thanks.
source share