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

com.github.bootfastconfig.cache.RedisCaffeineL2CacheManager Maven / Gradle / Ivy

package com.github.bootfastconfig.cache;


import com.github.bootfastconfig.cache.l2cache.L2Cache;
import com.github.bootfastconfig.cache.properties.L2CacheProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author mister
 * 一个二级缓存Manager
 */

public class RedisCaffeineL2CacheManager implements CacheManager {

    private final static Logger log = LoggerFactory.getLogger(RedisCaffeineL2CacheManager.class);

    /**
     * 一级缓存 Manager
     */
    private CacheManager cacheManager1;

    /**
     * 二级缓存 Manager
     */
    private CacheManager cacheManager2;


    private ConcurrentMap cacheMap = new ConcurrentHashMap();


    private L2CacheProperties l2CacheProperties;


    private boolean dynamic = true;


    private RedisTemplate redisTemplate;


    public RedisCaffeineL2CacheManager(CacheManager cacheManager1, CacheManager cacheManager2) {
        super();
        this.cacheManager1 = cacheManager1;
        this.cacheManager2 = cacheManager2;

    }


    public RedisCaffeineL2CacheManager(L2CacheProperties l2CacheProperties,
                                       RedisTemplate redisTemplate, CacheManager cacheManager1, CacheManager cacheManager2) {
        super();
        this.l2CacheProperties = l2CacheProperties;
        this.cacheManager1 = cacheManager1;
        this.cacheManager2 = cacheManager2;
        this.redisTemplate = redisTemplate;

    }


    @Override
    public Cache getCache(String name) {
        Cache cache = this.cacheMap.get(name);
        if (cache == null && this.dynamic) {
            synchronized (this.cacheMap) {
                cache = this.cacheMap.get(name);
                if (cache == null) {
                    cache = createL2Cache(name);
                    this.cacheMap.put(name, cache);
                }
            }
        }
        return cache;
    }

    private Cache createL2Cache(String name) {
        String topic = l2CacheProperties.getTopic();
        return new L2Cache(name, topic, cacheManager1.getCache(name), cacheManager2.getCache(name), this::eventPubl);
    }


    @Override
    public Collection getCacheNames() {
        return Collections.unmodifiableSet(this.cacheMap.keySet());
    }


    public void eventPubl(CacheMessage cacheMessage) {
        redisTemplate.convertAndSend(cacheMessage.getTopic(), cacheMessage);
    }


    public void clearCache(CacheMessage cacheMessage) {
        L2Cache cache = (L2Cache) getCache(cacheMessage.getCacheName());
        cache.clearCache(cacheMessage);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy