义齿


springcloudではspring-boot-starter-data-redisを使用して分散キャッシュを処理できますが、ネットワークにのみ存在する転送キャッシュには満足していません。コストを拡張してRedis 2段キャッシュを追加することで、ネットワーク転送による転送効率を削減できます。

以下に整理した項目に対して直接使用する


パッケージインストールプロジェクトspringcloud-twocache
git clone https://github.com/dounine/spring-cloud.git
cd spring-cloud
gradle install -xtest

プロジェクトでbuildを参照します.gradle
dependencies {
    compile('com.dounine.twocache:springcloud-twocache:0.0.1-SNAPSHOT')
}
application.ymlに次のコードを追加します.
spring:
  redis:
    host: localhost
    port: 6379
twocache:
  enable: true
  redis:
    topic:    

JAvaコードで使用(spring cacheでキャッシュを使用するのと同じ)
@Cacheable(cacheNames = "user",key = "#userId")
public String queryUser(@PathVariable String userId) {
...
}

ソースの説明


IpV4.JAvaノードIP取得ツール
import java.net.Inet4Address;
import java.net.UnknownHostException;

public final class IpV4 {
    private static String node;
    static {
        try {
            node = Inet4Address.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    public static final String get(){
        return node;
    }
}

NotifyMsg.JAva Redisメッセージ通知パッケージオブジェクト
public class NotifyMsg implements Serializable {
    private NotifyType notifyType;
    private String cacheName;
    private String node;
    private Object key;
    private Object result;

    public NotifyMsg(NotifyType notifyType,String node,Object key,Object result){
        this.node = node;
        this.notifyType = notifyType;
        this.key = key;
        this.result = result;
    }

    // get set ...
}

NotifyType.JAva Redisキャッシュ通知タイプ
public enum NotifyType {
    PUT,
    EVICT,
    CLEAR
}

RedisAndLocalCache.JAva Redisローカルキャッシュ書き換え
public class RedisAndLocalCache implements Cache {

    private ConcurrentHashMap local = new ConcurrentHashMap<>();
    private RedisCache redisCache;
    private TwoLevelCacheManager cacheManager;
    private String node;

    public RedisAndLocalCache(TwoLevelCacheManager twoLevelCacheManager,RedisCache redisCache,String node){
        this.cacheManager = twoLevelCacheManager;
        this.redisCache = redisCache;
        this.node = node;
    }

    @Override
    public String getName() {
        return redisCache.getName();
    }

    @Override
    public Object getNativeCache() {
        return redisCache.getNativeCache();
    }

    @Override
    public ValueWrapper get(Object key) {
        ValueWrapper valueWrapper = local.get(key);
        if(valueWrapper!=null){
            return valueWrapper;
        }else{
            valueWrapper = redisCache.get(key);
            if(valueWrapper!=null){
                local.put(key,valueWrapper);
            }
            return valueWrapper;
        }
    }

    @Override
    public  T get(Object key, Class type) {
        ValueWrapper valueWrapper = local.get(key);
        if(valueWrapper!=null){
            return (T)valueWrapper.get();
        }else{
            valueWrapper = redisCache.get(key);
            if(valueWrapper!=null){
                local.put(key,valueWrapper);
            }
            return (T)valueWrapper.get();
        }
    }

    @Override
    public  T get(Object key, Callable valueLoader) {
        return null;
    }

    @Override
    public void put(Object key, Object value) {
        this.local.put(key,new SimpleValueWrapper(value));
        this.redisCache.put(key,value);
        this.notifyNodes(new NotifyMsg(NotifyType.PUT,node,key,value));
    }

    private void notifyNodes(NotifyMsg notifyType){
        notifyType.setCacheName(redisCache.getName());
        cacheManager.publishMessage(notifyType);
    }


    @Override
    public ValueWrapper putIfAbsent(Object key, Object value) {
        return null;
    }

    @Override
    public void evict(Object key) {
        redisCache.evict(key);
        this.notifyNodes(new NotifyMsg(NotifyType.EVICT,node,key,null));
    }

    public void clearLocal(){
        local.clear();
    }

    @Override
    public void clear() {
        redisCache.clear();
        this.notifyNodes(new NotifyMsg(NotifyType.CLEAR,node,null,null));
    }
}

TwoCacheConfig.JAvaスター自動構成クラス
@Configuration
@ConditionalOnMissingBean(CacheManager.class)
@ConditionalOnBean({RedisTemplate.class})
@ConditionalOnProperty(name = "twocache.enable",havingValue = "true")
@EnableCaching
public class TwoCacheConfig {

    @Value("${twocache.redis.topic:towcache}")
    private String topic;
    @Value("${server.port}")
    private Integer port;

    @Bean
    @ConditionalOnMissingBean(JedisConnectionFactory.class)
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter,new PatternTopic(topic));
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(final TwoLevelCacheManager cacheManager){
        return new MessageListenerAdapter(new MessageListener() {
            @Override
            public void onMessage(Message message, byte[] pattern) {
                try {
                    String topic = new String(message.getChannel(),"utf-8");
                    cacheManager.receiver(message.getBody());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Bean
    public TwoLevelCacheManager cacheManager(RedisTemplate redisTemplate){
        return new TwoLevelCacheManager(redisTemplate,topic,port);
    }
}

TwoLevelCacheManager.JAvaデュアルキャッシュマネージャ
public class TwoLevelCacheManager extends RedisCacheManager {

    private String topic;
    private RedisTemplate redisTemplate;
    private Integer port;
    private String node = IpV4.get();

    public TwoLevelCacheManager(RedisTemplate redisTemplate,String topic, Integer port){
        super(redisTemplate);
        this.redisTemplate = redisTemplate;
        this.topic = topic;
        this.port = port;
    }

    @Override
    protected Cache decorateCache(Cache cache) {
        return new RedisAndLocalCache(this,(RedisCache) cache,node+":"+port);
    }

    protected void publishMessage(NotifyMsg notifyMsg){
        this.redisTemplate.convertAndSend(topic,notifyMsg);
    }

    public void receiver(byte[] body){
        NotifyMsg notifyMsg = (NotifyMsg)this.redisTemplate.getDefaultSerializer().deserialize(body);
        RedisAndLocalCache cache = (RedisAndLocalCache) this.getCache(notifyMsg.getCacheName());
        if(cache!=null){
            if(!notifyMsg.getNode().equals(node+":"+port)){
                if(notifyMsg.getNotifyType().equals(NotifyType.CLEAR)){
                    cache.clearLocal();
                }else if(notifyMsg.getNotifyType().equals(NotifyType.PUT)){
                    cache.put(notifyMsg.getKey(),notifyMsg.getResult());
                }else if(notifyMsg.getNotifyType().equals(NotifyType.EVICT)){
                    cache.evict(notifyMsg.getKey());
                }
            }else{
//                LOGGER.error("       ,    ");
            }
        }
    }
}