Redis之sql缓存的具体使用
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习数据库相关编程知识。下面本篇文章就来带大家聊聊《Redis之sql缓存的具体使用》,介绍一下缓存、Redissql,希望对大家的知识积累有所帮助,助力实战开发!
1.环境搭建
<!-- RedisTemplate --> <dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-data-redis</artifactid></dependency>
spring: redis: host: 192.168.8.128 port: 6380 password: 1234 database: 0 timeout: 3000 jedis: pool: max-wait: -1 max-active: -1 max-idle: 20 min-idle: 10
2.Redis配置
package com.yzm.redis01.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.IOException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @Configuration public class ObjectMapperConfig { private static final String PATTERN = "yyyy-MM-dd HH:mm:ss"; @Bean(name = "myObjectMapper") public ObjectMapper objectMapper() { JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); return new ObjectMapper() // 转换为格式化的json(控制台打印时,自动格式化规范) //.enable(SerializationFeature.INDENT_OUTPUT) // Include.ALWAYS 是序列化对像所有属性(默认) // Include.NON_NULL 只有不为null的字段才被序列化,属性为NULL 不序列化 // Include.NON_EMPTY 如果为null或者 空字符串和空集合都不会被序列化 // Include.NON_DEFAULT 属性为默认值不序列化 .setSerializationInclusion(JsonInclude.Include.NON_NULL) // 如果是空对象的时候,不抛异常 .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) // 反序列化的时候如果多了其他属性,不抛出异常 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) // 取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式 .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .setDateFormat(new SimpleDateFormat(PATTERN)) // 对LocalDateTime序列化跟反序列化 .registerModule(javaTimeModule) .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY) // 此项必须配置,否则会报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXX .enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY) ; } static class LocalDateTimeSerializer extends JsonSerializer<localdatetime> { @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(value.format(DateTimeFormatter.ofPattern(PATTERN))); } } static class LocalDateTimeDeserializer extends JsonDeserializer<localdatetime> { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException { return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(PATTERN)); } } } </localdatetime></localdatetime>
package com.yzm.redis01.config; import lombok.extern.slf4j.Slf4j; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.SimpleKey; import java.lang.reflect.Method; import java.util.Arrays; /** * key生成器 */ @Slf4j public class MyKeyGenerator implements KeyGenerator { private static final String NO_PARAM = "[]"; private static final String NULL_PARAM = "_"; @Override public Object generate(Object target, Method method, Object... params) { StringBuilder key = new StringBuilder(); key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":"); if (params.length == 0) { return new SimpleKey(key.append(NO_PARAM).toString()); } return new SimpleKey(key.append(Arrays.toString(params).replace("null", NULL_PARAM)).toString()); } }
package com.yzm.redis01.config; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.*; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer; import javax.annotation.Resource; import java.time.Duration; @Configuration @EnableCaching // 启动缓存 public class RedisConfig { @Bean(name = "myKeyGenerator") public MyKeyGenerator myKeyGenerator() { return new MyKeyGenerator(); } @Resource(name = "myObjectMapper") private ObjectMapper objectMapper; /** * 选择redis作为默认缓存工具 */ @Bean public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration .defaultCacheConfig() // 默认缓存时间(秒) .entryTtl(Duration.ofSeconds(300L)) // 序列化key、value .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer())) // 禁用缓存空值 .disableCachingNullValues(); return new RedisCacheManager(redisCacheWriter, cacheConfiguration); } /** * redisTemplate配置 */ @Bean public RedisTemplate<string object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<string object> template = new RedisTemplate(); // 配置连接工厂 template.setConnectionFactory(factory); Jackson2JsonRedisSerializer<object> jacksonSerializer = jackson2JsonRedisSerializer(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // 使用StringRedisSerializer来序列化和反序列化redis的key,value采用json序列化 template.setKeySerializer(stringRedisSerializer); template.setValueSerializer(jacksonSerializer); // 设置hash key 和value序列化模式 template.setHashKeySerializer(stringRedisSerializer); template.setHashValueSerializer(jacksonSerializer); //支持事务 template.setEnableTransactionSupport(true); template.afterPropertiesSet(); return template; } private Jackson2JsonRedisSerializer<object> jackson2JsonRedisSerializer() { //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer<object> jacksonSerializer = new Jackson2JsonRedisSerializer(Object.class); jacksonSerializer.setObjectMapper(objectMapper); return jacksonSerializer; } } </object></object></object></string></string>
3.功能实现
新增、更新、删除、查询数据时,对缓存执行对应相同的操作
package com.yzm.redis01.entity; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; import java.time.LocalDateTime; import java.util.Date; @Data @Builder @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) public class User implements Serializable { private static final long serialVersionUID = -2468903864827432779L; private Integer id; private String username; private String password; private Date createDate; private LocalDateTime updateDate; }
package com.yzm.redis01.service; import com.yzm.redis01.entity.User; import java.util.List; public interface UserService { User saveUser(User user); User updateUser(User user); int deleteUser(Integer id); void deleteAllCache(); User getUserById(Integer id); List<user> selectAll(); List<user> findAll(Object... params); } </user></user>
为了简便,数据不从数据库获取,这里是创建Map存储数据实现
package com.yzm.redis01.service.impl; import com.yzm.redis01.entity.User; import com.yzm.redis01.service.UserService; import lombok.extern.slf4j.Slf4j; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.*; @Slf4j @Service @CacheConfig(cacheNames = "users") public class UserServiceImpl implements UserService { private static final Map<integer user> userMap; static { userMap = new HashMap(); userMap.put(userMap.size() + 1, User.builder() .id(userMap.size() + 1).username("root").password("root").createDate(new Date()).updateDate(LocalDateTime.now()).build()); userMap.put(userMap.size() + 1, User.builder() .id(userMap.size() + 1).username("admin").password("admin").createDate(new Date()).updateDate(LocalDateTime.now()).build()); } @Override @CachePut(key = "#result.id", condition = "#result.id gt 0") public User saveUser(User user) { log.info("保存数据"); int id = userMap.size() + 1; User build = User.builder() .id(id) .username(user.getUsername()) .password(user.getPassword()) .createDate(new Date()) .updateDate(LocalDateTime.now()) .build(); userMap.put(id, build); return build; } @Override @CachePut(key = "#user.id", unless = "#result eq null") public User updateUser(User user) { log.info("更新数据"); if (userMap.containsKey(user.getId())) { User update = userMap.get(user.getId()); update.setUsername(user.getUsername()) .setPassword(user.getPassword()) .setUpdateDate(LocalDateTime.now()); userMap.replace(user.getId(), update); return update; } return null; } @Override @CacheEvict(key = "#id", condition = "#result gt 0") public int deleteUser(Integer id) { log.info("删除数据"); if (userMap.containsKey(id)) { userMap.remove(id); return 1; } return 0; } @Override @CacheEvict(allEntries = true) public void deleteAllCache() { log.info("清空缓存"); } @Override @Cacheable(key = "#id", condition = "#id gt 1") public User getUserById(Integer id) { log.info("查询用户"); return userMap.get(id); } @Override @Cacheable(key = "#root.methodName") public List<user> selectAll() { log.info("查询所有"); return new ArrayList(userMap.values()); } @Override @Cacheable(keyGenerator = "myKeyGenerator") public List<user> findAll(Object... params) { log.info("查询所有"); return new ArrayList(userMap.values()); } } </user></user></integer>
package com.yzm.redis01.controller; import com.yzm.redis01.entity.User; import com.yzm.redis01.service.UserService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping("/saveUser") public void saveUser() { User user = new User(); user.setUsername("yzm"); user.setPassword("yzm"); System.out.println(userService.saveUser(user)); } @GetMapping("/updateUser") public void updateUser(Integer id) { User user = new User(); user.setId(id); user.setUsername("yzm"); user.setPassword("123"); System.out.println(userService.updateUser(user)); } @GetMapping("/deleteUser") public void deleteUser(@RequestParam Integer id) { System.out.println(userService.deleteUser(id)); } @GetMapping("/deleteAllCache") public void deleteAllCache() { userService.deleteAllCache(); } @GetMapping("/getUserById") public void getUserById(@RequestParam Integer id) { System.out.println(userService.getUserById(id)); } @GetMapping("/selectAll") public void selectAll() { List<user> users = userService.selectAll(); users.forEach(System.out::println); } } </user>
4.缓存注解的使用说明
@CacheConfig:注解在类上,表示该类所有缓存方法使用统一指定的缓存区,也可以作用在方法上
@CacheAble:注解在方法上,应用到读数据的方法上,如查找方法:调用方法之前根据条件判断是否从缓存获取相应的数据,缓存没有数据,方法执行后添加到缓存
#id 直接使用参数名
#p0 p0对应参数列表的第一个参数,以此类推
#user.id 参数是对象时,使用对象属性
#root. 可以点出很多方法
#root.methodName
#result 返回值
http://localhost:8080/user/getUserById?id=1
http://localhost:8080/user/getUserById?id=2
http://localhost:8080/user/selectAll
@Cacheable运行流程:在调用方法之前判断condition,如果为true,则查缓存;没有缓存就调用方法并将数据添加到缓存;condition=false就与缓存无关了
@CachePut:注解在方法上,应用到写数据的方法上,如新增/修改方法,调用方法之后根据条件判断是否添加/更新相应的数据到缓存:
http://localhost:8080/user/saveUser
condition条件为true,添加到缓存,根据id查询直接从缓存获取
http://localhost:8080/user/getUserById?id=3
http://localhost:8080/user/updateUser?id=3
http://localhost:8080/user/getUserById?id=3
条件condition=true,执行缓存操作
条件unless=false,执行缓存操作;跟condition相反
@CacheEvict 注解在方法上,应用到删除数据的方法上,如删除方法,调用方法之后根据条件判断是否从缓存中移除相应的数据
http://localhost:8080/user/saveUser
http://localhost:8080/user/getUserById?id=3
http://localhost:8080/user/deleteUser?id=3
自定义缓存key自动生成器
@Override @Cacheable(keyGenerator = "myKeyGenerator") public List<user> findAll(Object... params) { log.info("查询所有"); return new ArrayList(userMap.values()); } </user>
@Slf4j public class MyKeyGenerator implements KeyGenerator { private static final String NO_PARAM = "[]"; private static final String NULL_PARAM = "_"; @Override public Object generate(Object target, Method method, Object... params) { StringBuilder key = new StringBuilder(); key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":"); if (params.length == 0) { return new SimpleKey(key.append(NO_PARAM).toString()); } return new SimpleKey(key.append(Arrays.toString(params).replace("null", NULL_PARAM)).toString()); } }
http://localhost:8080/user/findAll
http://localhost:8080/user/findAll?id=123
http://localhost:8080/user/findAll?username=yzm
@Caching
有时候我们可能组合多个Cache注解使用;比如用户新增成功后,我们要添加id–>user;username—>user;email—>user的缓存;
此时就需要@Caching组合多个注解标签了。
@Caching( put = { @CachePut(value = "users", key = "#user.id"), @CachePut(value = "users", key = "#user.username"), @CachePut(value = "users", key = "#user.email") } ) public User save(User user) {}
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于数据库的相关知识,也可关注golang学习网公众号。

- 上一篇
- 使用Redis实现令牌桶算法原理解析

- 下一篇
- Redis分布式锁详细介绍
-
- 缓慢的帆布鞋
- 这篇技术文章真是及时雨啊,好细啊,太给力了,码起来,关注楼主了!希望楼主能多写数据库相关的文章。
- 2023-01-28 01:03:49
-
- 知性的楼房
- 这篇文章内容真及时,很详细,感谢大佬分享,码起来,关注老哥了!希望老哥能多写数据库相关的文章。
- 2023-01-09 16:57:21
-
- 义气的豆芽
- 太详细了,已收藏,感谢作者的这篇文章,我会继续支持!
- 2023-01-07 00:43:18
-
- 甜美的美女
- 很有用,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢博主分享文章!
- 2023-01-06 17:47:35
-
- 独特的发夹
- 好细啊,码起来,感谢作者的这篇技术文章,我会继续支持!
- 2023-01-03 21:30:31
-
- 激昂的老师
- 这篇博文真及时,很详细,赞 ??,收藏了,关注楼主了!希望楼主能多写数据库相关的文章。
- 2023-01-03 01:53:06
-
- 顺利的服饰
- 很好,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢作者大大分享技术文章!
- 2023-01-02 21:31:16
-
- 迅速的皮带
- 这篇博文真是及时雨啊,太细致了,很好,码住,关注up主了!希望up主能多写数据库相关的文章。
- 2022-12-31 19:32:07
-
- 爱听歌的短靴
- 写的不错,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢up主分享技术文章!
- 2022-12-31 18:05:49
-
- 数据库 · Redis | 8小时前 |
- Redis与MySQL缓存同步方法解析
- 245浏览 收藏
-
- 数据库 · Redis | 9小时前 |
- Redis性能监控工具有哪些
- 124浏览 收藏
-
- 数据库 · Redis | 10小时前 |
- RedisList队列优化方法分享
- 378浏览 收藏
-
- 数据库 · Redis | 10小时前 |
- Redis位图实现用户签到优化方案
- 322浏览 收藏
-
- 数据库 · Redis | 19小时前 |
- Redis数据安全防护全攻略
- 112浏览 收藏
-
- 数据库 · Redis | 19小时前 |
- Redis哈希技巧与实战应用
- 204浏览 收藏
-
- 数据库 · Redis | 19小时前 |
- 扩展Redis集群节点的步骤与注意事项
- 163浏览 收藏
-
- 数据库 · Redis | 20小时前 |
- 高并发Redis优化技巧分享
- 147浏览 收藏
-
- 数据库 · Redis | 20小时前 |
- Redis主从复制故障排查指南
- 477浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis与HBase存储方案详解
- 414浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis与MongoDB缓存优化方法
- 193浏览 收藏
-
- 数据库 · Redis | 1天前 |
- Redis安全配置更新操作教程
- 313浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 千音漫语
- 千音漫语,北京熠声科技倾力打造的智能声音创作助手,提供AI配音、音视频翻译、语音识别、声音克隆等强大功能,助力有声书制作、视频创作、教育培训等领域,官网:https://qianyin123.com
- 96次使用
-
- MiniWork
- MiniWork是一款智能高效的AI工具平台,专为提升工作与学习效率而设计。整合文本处理、图像生成、营销策划及运营管理等多元AI工具,提供精准智能解决方案,让复杂工作简单高效。
- 89次使用
-
- NoCode
- NoCode (nocode.cn)是领先的无代码开发平台,通过拖放、AI对话等简单操作,助您快速创建各类应用、网站与管理系统。无需编程知识,轻松实现个人生活、商业经营、企业管理多场景需求,大幅降低开发门槛,高效低成本。
- 107次使用
-
- 达医智影
- 达医智影,阿里巴巴达摩院医疗AI创新力作。全球率先利用平扫CT实现“一扫多筛”,仅一次CT扫描即可高效识别多种癌症、急症及慢病,为疾病早期发现提供智能、精准的AI影像早筛解决方案。
- 98次使用
-
- 智慧芽Eureka
- 智慧芽Eureka,专为技术创新打造的AI Agent平台。深度理解专利、研发、生物医药、材料、科创等复杂场景,通过专家级AI Agent精准执行任务,智能化工作流解放70%生产力,让您专注核心创新。
- 98次使用
-
- 关于golangtest缓存问题
- 2023-01-01 298浏览
-
- 基于 Spring Aop 环绕通知实现 Redis 缓存双删功能(示例代码)
- 2022-12-31 334浏览
-
- Django使用Redis进行缓存详细步骤
- 2023-01-07 291浏览
-
- Go语言基于HTTP的内存缓存服务的实现
- 2022-12-24 388浏览
-
- MySQL+Redis缓存+Gearman共同构建数据库缓存的方法
- 2023-01-21 495浏览