在springcloud中我们可以使用spring-boot-starter-data-redis已经为我们处理好分布式缓存,但是我们还是不满足于只存在于网络中传输的缓存,我们现在来扩展成本地加Redis双级缓存,这样就可以减少网络传输带来的传输效率。

springcloud

以下是针对已经整理好的项目进行直接使用

打包安装项目 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: localhostport: 6379
twocache:enable: trueredis: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<Object,ValueWrapper> 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;}@Overridepublic String getName() {return redisCache.getName();}@Overridepublic Object getNativeCache() {return redisCache.getNativeCache();}@Overridepublic 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;}}@Overridepublic <T> T get(Object key, Class<T> 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();}}@Overridepublic <T> T get(Object key, Callable<T> valueLoader) {return null;}@Overridepublic 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);}@Overridepublic ValueWrapper putIfAbsent(Object key, Object value) {return null;}@Overridepublic void evict(Object key) {redisCache.evict(key);this.notifyNodes(new NotifyMsg(NotifyType.EVICT,node,key,null));}public void clearLocal(){local.clear();}@Overridepublic void clear() {redisCache.clear();this.notifyNodes(new NotifyMsg(NotifyType.CLEAR,node,null,null));}
}

TwoCacheConfig.java Starter 自动配置类

@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();}@BeanRedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter){RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter,new PatternTopic(topic));return container;}@BeanMessageListenerAdapter listenerAdapter(final TwoLevelCacheManager cacheManager){return new MessageListenerAdapter(new MessageListener() {@Overridepublic void onMessage(Message message, byte[] pattern) {try {String topic = new String(message.getChannel(),"utf-8");cacheManager.receiver(message.getBody());} catch (UnsupportedEncodingException e) {e.printStackTrace();}}});}@Beanpublic TwoLevelCacheManager cacheManager(RedisTemplate redisTemplate){return new TwoLevelCacheManager(redisTemplate,topic,port);}
}

TwoLevelCacheManager.java 双级缓存管理器

public class TwoLevelCacheManager extends RedisCacheManager {private String topic;private RedisTemplate<String,Object> redisTemplate;private Integer port;private String node = IpV4.get();public TwoLevelCacheManager(RedisTemplate<String,Object> redisTemplate,String topic, Integer port){super(redisTemplate);this.redisTemplate = redisTemplate;this.topic = topic;this.port = port;}@Overrideprotected 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("消息从自身发送,忽略处理");}}}
}

Springcloud 高效率本地加Redis双级缓存相关推荐

  1. combotree 可以异步加载吗_Unity AssetBundle 资源打包,加载,本地缓存方式,安卓,PC本地加载路径问题...

    AssetBundle的定义和作用: 1,AssetBundle是一个压缩包包含模型.贴图.预制体.声音.甚至整个场景,可以在游戏运行的时候被加载. 2,AssetBundle自身保存着互相的依赖关系 ...

  2. Redis 分布式缓存 Java 框架

    https://dzone.com/articles/java-distributed-caching-in-redis 为什么要在 Java 分布式应用程序中使用缓存? 在提高应用程序速度和性能上, ...

  3. jmeter修改redis_jmeter如何访问redis服务缓存

    Redis(REmote DIctionary Server)是一个开源的内存数据结构存储,用作数据库,缓存和消息代理. 作为内存数据库,它将所有数据保存在RAM中.Redis在读取/写入数据时实现高 ...

  4. Ehcache、memcache、redis三大缓存区别

    Ehcache.memcache.redis三大缓存区别 最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考! Ehcache 在java项目 ...

  5. python使用redis做缓存_python实现类redis缓存

    越来越觉得的缓存是计算机科学里最NB的发明(没有之一), 现在项目用的是redis做的缓存, 它的两个特性用的蛮顺手的: 键值查找功能 缓存可设置过期时间 突突然的,觉得用python也可以简单的模拟 ...

  6. 大厂面试Redis:缓存雪崩、缓存穿透、缓存击穿

    欢迎关注这个有趣的灵魂 眼光不错,小伙子,看到这篇文章了就血赚,这篇文章绝对让你学到开心,这是面试的杀器,其实Redis这个东西吧,我个人认为,真的真的很强大,但是呢,又感觉被吹得有点过头了 不过人家 ...

  7. Redis高性能缓存数据库

    Redis高性能缓存数据库 Redis 基础入 Redis 介绍 特性 使用场景 正确安装与启动 重要的指令使用 全局命令 单线程架构 字符串 - String 内部编码 应用场景 哈希 - Hash ...

  8. 使用redis实现缓存_用下一个js实现一个简单的redis缓存

    使用redis实现缓存 For most websites, the changing pieces don't actually vary that often. That immutability ...

  9. 高性能分布式缓存Redis(缓存分类 安装 数据类型选择和应用场景 发布订阅 事务 Lua脚本 慢查询日志)

    高性能分布式缓存Redis 高性能分布式缓存Redis 1. 缓存发展史&缓存分类 1.1 大型网站中缓存的使用 1.2 常见缓存的分类 1.3 分布式缓存选型方案对比 2. Redis概述& ...

最新文章

  1. as3corelib系列教程之一:ArrayUtil类的用法
  2. 【-】WebKit Layout (布局)
  3. Pipe HDU - 2150(判断线段相交+向量叉乘线代详解)
  4. Java面试题,java工程师求职简历
  5. 给大家推荐9个专业分享生信技术的公众号
  6. mysql 导入dmp_mysql导入导出sql文件
  7. 使用OpenCV-python对植物图片进行分类
  8. DB2 多表空间 重定向 还原
  9. python tkinter画动态时钟
  10. win7原版镜像_封装二:Win7 预装软件 Setup 包集成方法
  11. 飞机大战(完整代码)
  12. Ubuntu 搜狗输入法 双拼输入法
  13. 各种光流算法介绍和原理
  14. 路灯问题 (贪心算法)
  15. C++类模板怎么写在单独的头文件和源文件中
  16. STM32汉字字模制作详解(OLED实战)
  17. LaTex学习笔记第二弹——罗列应用
  18. 基于stm32h743+rtthread的开源项目——板载资源介绍(一)
  19. 网络约炮类应用缘何层出不穷?
  20. 音频预训练模型 Audio and Speech Pre-trained Models

热门文章

  1. 一代上网人的记忆!百度浏览器将不再更新
  2. 结果出来了!视觉中国被罚30万 再次发布道歉声明...
  3. 被新款iPad和AirPods拯救的苹果 夺回全球市值最高公司宝座
  4. 苹果最新专利曝光:苹果可能正研发可折叠iPhone
  5. 前端面试题汇总(jQuery)
  6. jsp 页面刷新_如何应用XML+XSLT+AJAX组合技术实现无刷新数据查询
  7. 李迟2022年3月工作生活总结
  8. coreboot学习2:项目源码的初步了解
  9. GNU C内嵌汇编学习笔记
  10. onvif学习笔记1:环境准备