<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 缓存依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-
redisTemplate.setHashValueSerializer(newJackson2JsonRedisSerializer<>(Object.class)); redisTemplate.opsForHash().putAll(hashKeyPrefix, hashData); } // 你需要实现这个方法,将IPage转换为适合存储在Redis Hash中的Map privateMap<String, Object> convertToHashData(IPage<PopularProductResult> popularProducts) ...
要整合 Redis 的话,最好的方式是使用 Spring Cache,仅仅通过 @Cacheable、@CachePut、@CacheEvict、@EnableCaching 等注解就可以轻松使用 Redis 做缓存了。 1)@EnableCaching,开启缓存功能。 2)@Cacheable,调用方法前,去缓存中找,找到就返回,找不到就执行方法,并将返回值放到缓存中。 3)@CachePut,方法调用前不...
@Service@CacheConfig(cacheNames="users")publicclassUserService{@CacheablepublicList<User>getUsers(){// 从数据库中获取用户数据List<User>users=userRepository.findAll();returnusers;}@CacheEvict(allEntries=true)publicvoidrefreshCache(){// 发布缓存更新消息redisTemplate.convertAndSend("cache","users");}...
cacheNames.isEmpty()) { cacheManager.setCacheNames(cacheNames); } return this.customizerInvoker.customize(cacheManager); } ...省略其他 } 此时开发者不需要任何配置,则可以使用spring redis 缓存数据了,但是此时的配置全部是默认的配置。 添加缓存默认超时配置 RedisCacheManager的setDefaultExpiration(long ...
① 第一步:开启基于注解的缓存,使用@EnableCaching标注在 springboot 主启动类上 ② 第二步:标注缓存注解 注:这里使用@Cacheable注解就可以将运行结果缓存,以后查询相同的数据,直接从缓存中取,不需要调用方法。 2、常用属性说明 下面介绍一下@Cacheable这个注解常用的几个属性: ...
三. @Cacheeable/@CachePut/@CacheEvict 的几个常用参数 四. 开始使用 -- 整合 Redis 注:需先启动 Redis 服务器 1. 导入 Maven 包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> ...
3.3.1、spring-boot-starter-data-redis 的依赖包 3.3.2、stringRedisTemplate API(部分展示) opsForHash --> hash 操作 opsForList --> list 操作 opsForSet --> set 操作 opsForValue --> string 操作 opsForZSet --> Zset 操作 3.3.3 StringRedisTemplate 默认序列化机制 ...
本课程属于高级应用部分,着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式(整合Zookeeper/dubbo,整合SpringCloud),SpringBoot应用监管。 相关的阿里云产品:企业级分布式应用服务 EDAS...
数据库:MySQL 8.0+、Redis 6.0+ 2.2 依赖管理 在Spring Initializr中,我们可以为每个微服务选择以下核心依赖: Spring Web (或Spring WebFlux) Spring Data JPA/Reactive Repositories Spring Security Spring Boot Actuator Spring Cloud Gateway Spring Cloud Config ...