All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soento.redis.support.GenericCacheResolver Maven / Gradle / Ivy

package com.soento.redis.support;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.util.Assert;

import java.util.*;

/**
 * @author soento
 */
@Slf4j
public class GenericCacheResolver implements CacheResolver, InitializingBean {
    private List cacheManagerList;

    public GenericCacheResolver(CacheManager... cacheManager) {
        this.cacheManagerList = Arrays.asList(cacheManager);
    }

    public GenericCacheResolver(List cacheManagerList) {
        this.cacheManagerList = cacheManagerList;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(this.cacheManagerList, "CacheManager is required");
    }

    @Override
    public Collection resolveCaches(CacheOperationInvocationContext context) {
        Collection cacheNames = getCacheNames(context);
        if (cacheNames == null) {
            return Collections.emptyList();
        }
        Collection result = new ArrayList<>();
        for (CacheManager cacheManager : getCacheManagerList()) {
            for (String cacheName : cacheNames) {
                Cache cache = cacheManager.getCache(cacheName);
                if (cache == null) {
                    throw new IllegalArgumentException("Cannot find cache named '" +
                            cacheName + "' for " + context.getOperation());
                }
                result.add(cache);
            }
        }
        return result;
    }

    private Collection getCacheNames(CacheOperationInvocationContext context) {
        return context.getOperation().getCacheNames();
    }

    public List getCacheManagerList() {
        return cacheManagerList;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy