系统为了提高数据访问速度,先将数据加载到redis缓存中,但是每次从缓存获取数据,要通过网络访问才能获取,效率还是不够逆天快。如果访问量很大,并发很高,性能不够快不说,还容易造成reids负载过高,redis的主机出现各种物理故障。因此,可以在redis前增加本地一级缓存,本地一级缓存和系统应用在同一个JVM内,这样速度最快,redis退居二线当作二级缓存。每次请求先从一级缓存读取数据,一级缓存没有数据,再从二级缓存读取,并同步到一级缓存里,通过redis的消息发布订阅通知其他client机器更新缓存。这同CPU的一级缓存,二级缓存是一个道理。
本文并不涉及spring boot cache的详细使用介绍,需要熟悉spring boot cache基本使用。对于spring boot cache详细使用介绍请上度娘。扩展比较简单,闲话少说,直接上代码。
pom.xml,加入spring boot cache依赖,本地一级缓存使用ehcache3,二级缓存使用redis
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M7</version> </parent> <groupId>springboot</groupId> <artifactId>springboot-2level-cache</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <!-- Only used to expose cache metrics --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
appliaction.properties里配置属性
spring.cache.type=redis spring.ext.cache.name=countries spring.ext.cache.redis.topic=cache
本地一级缓存使用ehcache3,ehcache3.xml配置
<config xmlns='http://www.ehcache.org/v3' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jsr107="http://www.ehcache.org/v3/jsr107" xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd"> <cache alias="countries"> <expiry> <ttl unit="seconds">600</ttl> </expiry> <heap unit="entries">200</heap> <jsr107:mbeans enable-statistics="true"/> </cache> </config>
创建LocalRedisCache ,继承RedisCache,这里是实现redis一二级分布式缓存的核心,重载RedisCache的get、put、evict、clean等方法,增加本地一级缓存的读写,增加pub方法,通过redis发布缓存更新消息通知其他 redis clent 更新缓存,增加sub方法,获取缓存更新消息更新缓存。
package org.springframework.data.redis.cache; import org.springframework.cache.Cache; import org.springframework.data.redis.core.RedisOperations; import org.springframework.util.Assert; import java.util.concurrent.Callable; /** * extends RedisCache,增加本地一级缓存,redis作为二级缓存 */ public class LocalRedisCache extends RedisCache { private final Cache localCache;//本地一级缓存 private final RedisOperations redisOperations;//配合topicName,发布缓存更新消息 private final String topicName;//redis topic ,发布缓存更新消息通知其他client更新缓存 /** * Create new {@link RedisCache}. * * @param name must not be {@literal null}. * @param cacheWriter must not be {@literal null}. * @param cacheConfig must not be {@literal null}. * @param localCache must not be {@literal null}. * @param redisOperations must not be {@literal null}. * @param topicName must not be {@literal null}. */ protected LocalRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig, Cache localCache, RedisOperations redisOperations, String topicName) { super(name, cacheWriter, cacheConfig); Assert.notNull(localCache, "localCache must not be null!"); Assert.notNull(redisOperations, "redisOperations must not be null!"); Assert.hasText(topicName, "topicName must not be empty!"); this.localCache = localCache; this.redisOperations = redisOperations; this.topicName = topicName; } @Override public synchronized <T> T get(Object key, Callable<T> valueLoader) { //先读取本地一级缓存 T value = localCache.get(key, valueLoader); if (value == null) { //本地一级缓存不存在,读取redis二级缓存 value = super.get(key, valueLoader); if (value != null) { //redis二级缓存存在,存入本地一级缓存 localCache.put(key, value); //发布缓存更新消息通知其他client更新缓存 pub(new UpdateMessage(key, value, UpdateMessage.Type.PUT)); } } return value; } @Override public void put(Object key, Object value) { super.put(key, value); localCache.put(key, value); pub(new UpdateMessage(key, value, UpdateMessage.Type.PUT)); } @Override public ValueWrapper putIfAbsent(Object key, Object value) { ValueWrapper vw1 = localCache.putIfAbsent(key, value); ValueWrapper vw2 = super.putIfAbsent(key, value); pub(new UpdateMessage(key, value, UpdateMessage.Type.PUTIFABSENT)); return vw1 == null ? vw2 : vw1; } @Override public void evict(Object key) { localCache.evict(key); super.evict(key); pub(new UpdateMessage(key, UpdateMessage.Type.REMOVE)); } @Override public void clear() { localCache.clear(); super.clear(); pub(new UpdateMessage(UpdateMessage.Type.CLEAN)); } @Override public ValueWrapper get(Object key) { ValueWrapper valueWrapper = localCache.get(key); if (valueWrapper == null) { valueWrapper = super.get(key); if (valueWrapper != null) { localCache.put(key, valueWrapper.get()); pub(new UpdateMessage(key, valueWrapper.get(), UpdateMessage.Type.PUT)); } } return valueWrapper; } @Override public <T> T get(Object key, Class<T> type) { T value = localCache.get(key, type); if (value == null) { value = super.get(key, type); if (value != null) { localCache.put(key, value); pub(new UpdateMessage(key, value, UpdateMessage.Type.PUT)); } } return value; } /** * 更新缓存 * * @param updateMessage */ public void sub(final UpdateMessage updateMessage) { if (updateMessage.getType() == UpdateMessage.Type.CLEAN) { //清除所有缓存 localCache.clear(); super.clear(); } else if (updateMessage.getType() == UpdateMessage.Type.PUT) { //更新缓存 localCache.put(updateMessage.getKey(), updateMessage.getValue()); super.put(updateMessage.getKey(), updateMessage.getValue()); } else if (updateMessage.getType() == UpdateMessage.Type.PUTIFABSENT) { //更新缓存 localCache.putIfAbsent(updateMessage.getKey(), updateMessage.getValue()); super.putIfAbsent(updateMessage.getKey(), updateMessage.getValue()); } else if (updateMessage.getType() == UpdateMessage.Type.REMOVE) { //删除缓存 localCache.evict(updateMessage.getKey()); super.evict(updateMessage.getKey()); } } /** * 通知其他 redis clent 更新缓存 * * @param message */ private void pub(final UpdateMessage message) { this.redisOperations.convertAndSend(topicName, message); } }
创建新的缓存管理器,命名为LocalRedisCacheManager,继承了Spring Boot的RedisCacheManager,重载createRedisCache方法。
package org.springframework.data.redis.cache; import org.springframework.cache.Cache; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisOperations; import org.springframework.util.Assert; import java.util.Map; public class LocalRedisCacheManager extends RedisCacheManager { private final RedisConnectionFactory connectionFactory; private final Cache localCache; private final RedisOperations redisOperations; private final String topicName; public LocalRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, RedisConnectionFactory connectionFactory, Cache localCache, RedisOperations redisOperations, String topicName) { super(cacheWriter, defaultCacheConfiguration); this.connectionFactory = connectionFactory; this.localCache = localCache; this.redisOperations = redisOperations; this.topicName = topicName; check(); } public LocalRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, RedisConnectionFactory connectionFactory, Cache localCache, RedisOperations redisOperations, String topicName, String... initialCacheNames) { super(cacheWriter, defaultCacheConfiguration, initialCacheNames); this.connectionFactory = connectionFactory; this.localCache = localCache; this.redisOperations = redisOperations; this.topicName = topicName; check(); } public LocalRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map<String, RedisCacheConfiguration> initialCacheConfigurations, RedisConnectionFactory connectionFactory, Cache localCache, RedisOperations redisOperations, String topicName) { super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations); this.connectionFactory = connectionFactory; this.localCache = localCache; this.redisOperations = redisOperations; this.topicName = topicName; check(); } @Override protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) { return new LocalRedisCache(name, new DefaultRedisCacheWriter(connectionFactory), cacheConfig != null ? cacheConfig : RedisCacheConfiguration.defaultCacheConfig(), localCache, redisOperations, topicName); } public static LocalRedisCacheManager create(RedisConnectionFactory connectionFactory, Cache localCache, RedisOperations redisOperations, String topicName) { Assert.notNull(localCache, "localCache must not be null"); Assert.notNull(connectionFactory, "connectionFactory must not be null"); Assert.notNull(redisOperations, "redisOperations must not be null"); Assert.notNull(topicName, "topicName must not be null"); return new LocalRedisCacheManager(new DefaultRedisCacheWriter(connectionFactory), RedisCacheConfiguration.defaultCacheConfig(), connectionFactory, localCache, redisOperations, topicName); } private void check() { Assert.notNull(localCache, "localCache must not be null"); Assert.notNull(connectionFactory, "connectionFactory must not be null"); Assert.notNull(redisOperations, "redisOperations must not be null"); Assert.notNull(topicName, "topicName must not be null"); } }
缓存配置实现,需要使用注解 @EnableCaching 打开缓存功能
package org.springframework.data.redis.cache; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.jcache.JCacheCacheManager; import org.springframework.cache.jcache.JCacheManagerFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; import java.io.IOException; @Configuration @EnableCaching public class CacheConfig { @Value("${spring.ext.cache.name:countries}") private String localCacheName; @Value("${spring.ext.cache.redis.topic:cache}") private String topicName; @Bean public CacheManager jCacheCacheManager() { return new JCacheCacheManager(jCacheManagerFactoryBean().getObject()); } @Bean public JCacheManagerFactoryBean jCacheManagerFactoryBean() { JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean(); Resource resource = new ClassPathResource("ehcache3.xml"); try { jCacheManagerFactoryBean.setCacheManagerUri(resource.getURI()); } catch (IOException e) { throw new RuntimeException(e); } return jCacheManagerFactoryBean; } @Bean public JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); return factory; } @Bean public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(jedisConnectionFactory); return redisTemplate; } @Primary @Bean public RedisCacheManager redisCacheManager(JedisConnectionFactory jedisConnectionFactory, RedisTemplate redisTemplate) { RedisCacheManager cacheManager = LocalRedisCacheManager.create(jedisConnectionFactory, jCacheCacheManager().getCache(localCacheName), redisTemplate, topicName); return cacheManager; } @Bean public RedisMessageListenerContainer container(JedisConnectionFactory jedisConnectionFactory, MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(jedisConnectionFactory); container.addMessageListener(listenerAdapter, new PatternTopic(topicName)); return container; } @Bean public MessageListenerAdapter listenerAdapter(LocalRedisCacheManager cacheManager, RedisTemplate redisTemplate) { return new MessageListenerAdapter(new MessageListener() { @Override public void onMessage(Message message, byte[] pattern) { byte[] channel = message.getChannel(); byte[] body = message.getBody(); String cacheName = (String) redisTemplate.getStringSerializer().deserialize(channel); LocalRedisCache cache = (LocalRedisCache) cacheManager.getCache(cacheName); if (cache == null) { return; } UpdateMessage updateMessage = (UpdateMessage) redisTemplate.getValueSerializer().deserialize(body); cache.sub( updateMessage); } }); } }
到些,实现基本完成,其他相关简单代码,参考附件。
相关推荐
"Spring Boot+Spring Cache实现两级缓存(Redis+Caffeine)" 知识点一:缓存与两级缓存 缓存是将数据从读取较慢的介质上读取出来放到读取较快的介质上,如磁盘-->内存。平时我们会将数据存储到磁盘上,如:数据库。...
在本项目中,我们主要探讨的是如何在Spring Boot应用中整合Redis实现缓存功能,并提供了Windows环境下Redis的安装包以及Redis Desktop Manager这款桌面管理工具。这个整合过程涉及到多个关键知识点,接下来将逐一...
在本文中,我们将深入探讨如何在Spring Boot 2.1.4.RELEASE项目中结合JPA(Java Persistence API)和Hibernate实现Redis作为二级缓存。首先,我们需要理解这些技术的基本概念。 Spring Boot 是一个用于简化Spring...
通过上述步骤,我们可以成功地在Spring Boot项目中整合Spring Cache与Redis,实现了一个高效的分布式缓存解决方案。这种方式不仅可以提高系统的响应速度,还可以有效减轻数据库的压力,对于构建高并发、高可用的应用...
在IT行业中,Spring Cache是一个非常流行的缓存抽象层,它允许开发者通过简单的注解来实现应用的缓存功能。Spring Boot则是一个简化Spring应用程序开发的框架,它使得配置过程更加简单,同时也支持自动配置。Maven是...
在现代的Web应用程序开发中,...总结,Spring Redis的整合使得我们可以充分利用Redis的特性,实现高效的分布式缓存。通过详细注释的工具类,可以帮助团队更好地理解和使用这一功能,提升项目的开发效率和运行性能。
同时,他们也配置了Redis连接,可能使用了Spring Cache或Spring Data Redis来实现缓存功能。Netty可能被用来实现内部通信或者提供一种不同于默认HTTP服务的接口。而日志框架logback则负责记录应用程序的日志信息,...
Spring Cache 是 Spring 框架提供的一种统一的缓存抽象,它允许开发者通过简单的注解来实现缓存功能,而无需关心具体的缓存实现。在 Spring Boot 2.X 版本中,我们可以很方便地将 Spring Cache 与 Redis 结合,利用 ...
综上所述,"redis-cluster和spring集成,基于cache注解" 的项目是一个使用 Spring Cache 集成 Redis 集群的实例,旨在通过注解的方式简化缓存管理,提高应用性能。开发者可以通过导入提供的项目,快速了解和实践这一...
4. **使用 Redis**:在 Spring Boot 应用中,可以使用 `@Cacheable`、`@CacheEvict` 和 `@CachePut` 等注解来实现方法级别的缓存操作。例如,为一个方法添加缓存: ```java @Service public class UserService { ...
在本项目"spring-boot-redis.zip"中,我们将探讨如何将Spring Boot与MySQL数据库、MyBatis持久层框架以及Redis内存数据存储相结合,构建一个高效的数据访问和缓存系统。 首先,让我们深入了解Spring Boot。Spring ...
Spring Boot 整合 Redis 实现 Shiro 的分布式 Session 共享 Shiro 是一个优秀的 Java 安全框架,提供了强大的身份验证、授权和会话管理功能。然而,在分布式架构中,Shiro 的会话管理机制需要进行特殊处理,以便...
Spring Boot项目利用Redis实现集中式缓存实例是实现高性能、可扩展、高可用性的关键技术之一。本文将介绍如何在Spring Boot项目中利用Redis实现集中式缓存实例,提高系统性能和可扩展性。 1. Spring Boot项目初始化...
"Spring Boot + Shiro + Redis 实现 Session 共享方案二" 1. 概述 本文档旨在介绍如何使用 Spring Boot、Shiro 和 Redis 实现分布式 session 共享,以解决 Web 应用程序的登录 session 统一问题。 2. 相关依赖 ...
在本文中,我们将深入探讨如何在Spring Boot 1.x版本中使用Spring注解来实现J2Cache的两级缓存机制,其中包括一级缓存Ehcache和二级缓存Redis。通过这种方式,我们可以显著提高应用程序的性能,减少对数据库的依赖,...
在本实战项目 "s-cache-practice" 中,我们将学习如何将 Redis 集成到 Spring Boot 应用中,以实现通用缓存功能。 首先,让我们了解集成的步骤: 1. **添加依赖**:在 `pom.xml` 文件中,我们需要引入 Spring Boot...
本文将详细介绍Spring Boot整合Redis的完整步骤,包括Spring Boot对Redis的支持、添加依赖、配置RedisTemplate和StringRedisTemplate、使用Redis缓存等。 一、Spring Boot对Redis的支持 Spring Boot对Redis的支持...
Spring Cache是Spring框架中的一种缓存机制,它可以将缓存数据存储在Redis中。然而,在某些情况下,我们需要手动清理Redis缓存,以便释放内存空间或更新缓存数据。在本文中,我们将介绍如何使用Spring Cache手动清理...
在IT行业中,构建高效、可扩展的Web应用是至关重要的,而Spring框架、MyBatis持久层框架以及Redis缓存系统的结合使用,是实现这一目标的常见方式。本教程主要针对初学者,介绍如何将这三者整合,实现数据缓存功能,...