LayeringCacheManager.java 3.4 KB
Newer Older
ibizdev's avatar
ibizdev committed
1 2
package cn.ibizlab.util.cache.cacheManager;

3
import lombok.Data;
ibizdev's avatar
ibizdev committed
4 5 6 7
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
8
import org.springframework.cache.caffeine.CaffeineCache;
ibizdev's avatar
ibizdev committed
9 10 11 12 13 14 15 16 17
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
18 19
import cn.ibizlab.util.cache.cache.CusRedisCache;
import cn.ibizlab.util.cache.cache.LayeringCache;
ibizdev's avatar
ibizdev committed
20 21

/**
22
 * 缓存分层
ibizdev's avatar
ibizdev committed
23 24 25 26 27 28 29
 * 1级缓存为caffeine
 * 2级缓存为redis
 */
@Data
public class LayeringCacheManager implements CacheManager {

    private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>(16);
30 31 32
    private static final int default_expire_after_write = 1;
    private static final int default_initial_capacity = 5;
    private static final int default_maximum_size = 1_000;
ibizdev's avatar
ibizdev committed
33
    private Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder()
34 35 36 37 38 39 40 41 42 43 44 45 46
            .expireAfterAccess(default_expire_after_write, TimeUnit.HOURS)
            .initialCapacity(default_initial_capacity)
            .maximumSize(default_maximum_size);

    private RedisCacheWriter redisCacheWriter;
    private RedisCacheConfiguration redisConfiguration;
    private RedisTemplate<String, Object> redisTemplate;

    public LayeringCacheManager(RedisCacheWriter redisCacheWriter , RedisCacheConfiguration redisConfiguration , RedisTemplate<String, Object> redisTemplate ){
        this.redisCacheWriter=redisCacheWriter;
        this.redisConfiguration=redisConfiguration;
        this.redisTemplate= redisTemplate;
    }
ibizdev's avatar
ibizdev committed
47 48 49

    /**
     * 获取缓存对象
50
     * @param cacheName
ibizdev's avatar
ibizdev committed
51 52 53
     * @return
     */
    @Override
54 55
    public Cache getCache(String cacheName) {
        Cache cache = this.cacheMap.get(cacheName);
ibizdev's avatar
ibizdev committed
56 57
        if (cache == null) {
            synchronized (this.cacheMap) {
58
                cache = this.cacheMap.get(cacheName);
ibizdev's avatar
ibizdev committed
59
                if (cache == null) {
60 61
                    cache = createCache(cacheName);
                    this.cacheMap.put(cacheName, cache);
ibizdev's avatar
ibizdev committed
62 63 64 65 66 67
                }
            }
        }
        return cache;
    }

68 69 70 71
    /**
     * 获取集合中的缓存
     * @return
     */
ibizdev's avatar
ibizdev committed
72 73 74 75 76 77
    @Override
    public Collection<String> getCacheNames() {
        return Collections.unmodifiableSet(this.cacheMap.keySet());
    }

    /**
78 79 80
     * 创建缓存
     * @param cacheName
     * @return
ibizdev's avatar
ibizdev committed
81
     */
82 83
    protected Cache createCache(String cacheName) {
        return new LayeringCache(cacheName,new CaffeineCache(cacheName, this.cacheBuilder.build(), true),new CusRedisCache(cacheName, redisCacheWriter, redisConfiguration),redisTemplate);
ibizdev's avatar
ibizdev committed
84 85
    }

86 87 88 89 90 91
    /**
     * 缓存配置[缓存容量大小、时长等]
     * @param caffeineCacheConfig
     */
    public void setCaffeineCacheConfig(CaffeineSpec caffeineCacheConfig) {
        Caffeine<Object, Object> cacheBuilder = Caffeine.from(caffeineCacheConfig);
ibizdev's avatar
ibizdev committed
92 93 94 95 96
        if (!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) {
            this.cacheBuilder = cacheBuilder;
        }
    }
}