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

com.rustknife.jknife.cache.AppCacheManager Maven / Gradle / Ivy

The newest version!
package com.rustknife.jknife.cache;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.cache.RedisCacheManager;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 自定义缓存管理,支持 ehcache + redis
 * yoke 2018-05-20
 */
public class AppCacheManager implements CacheManager {
    @Resource @Lazy
    private volatile RedisCacheManager  redisCacheManager;
    @Resource @Lazy
    private volatile AppEhCacheCacheManager ehCacheCacheManager;

    private final static String redisPrefix = "redis";

    /**
     * get the cache by spring annotation cache name
     * @param name spring annotation cache name
     * @return Cache
     */
    @Override
    public Cache getCache(String name) {
        // determinate by the cache name prefix for use redis or ehCache
        if (name.startsWith(redisPrefix))
            return redisCacheManager == null ? null : redisCacheManager.getCache(name);
        else
            return ehCacheCacheManager == null ? null : ehCacheCacheManager.getCache(name);
    }

    @Override
    public Collection getCacheNames() {
        Collection cacheNames = new ArrayList<>();
        if (redisCacheManager != null) 
            cacheNames.addAll(redisCacheManager.getCacheNames());
        if (ehCacheCacheManager != null)
            cacheNames.addAll(ehCacheCacheManager.getCacheNames());
        return cacheNames;
    }

    public static String getRedisPrefix(){
        return redisPrefix;
    }

    public CacheManager getRedisCacheManager() {
        return redisCacheManager;
    }

    public void setRedisCacheManager(RedisCacheManager  redisCacheManager) {
        this.redisCacheManager = redisCacheManager;
    }

    public CacheManager getEhCacheCacheManager() {
        return ehCacheCacheManager;
    }

    public void setEhCacheCacheManager(AppEhCacheCacheManager ehcacheCacheManager) {
        this.ehCacheCacheManager = ehcacheCacheManager;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy