
SpringBoot整合redis前置配置以及序列化
其实Reis环境配好了,那么使用方法跟缓存基础篇的玩法一样,只要你配好了环境,用的缓存就是redis。
因为默认保存对象,使用的是jdk序列化机制,序列化后的数据保存到redis中。 springboot自然考虑到了这一点,所以我们可以自定义序列化的规则:将数据以json的方式保存,自己将对象转为json: 1.想完成这种转变,我们只需要将下面的bean加入容器:
下面写一个显示访问地址次数的demo:
发布日期:2021-05-07 13:38:57
浏览次数:23
分类:精选文章
本文共 5811 字,大约阅读时间需要 19 分钟。
1.springboot引入redis的相关依赖,在pom.xml文件中加入:org.springframework.boot spring-boot-starter-data-redis
2.配置redis的服务ip地址,在application.properties中指定:
#指定redis的主机地址spring.redis.host=192.168.31.53
现在redis的环境已经搭建好了,测试一下,redis可否能用,在springboot的测试类写下面的测试代码:
@Autowired StringRedisTemplate stringRedisTemplate; @Autowired RedisTemplate redisTemplate; /** * Redis常见的五大数据类型 * String(字符串)、List(列表)、Set(集合)、Hash(散列)、ZSet(有序集合) * stringRedisTemplate.opsForValue()[String(字符串)] * stringRedisTemplate.opsForList()[List(列表)] * stringRedisTemplate.opsForSet()[Set(集合)] * stringRedisTemplate.opsForHash()[Hash(散列)] * stringRedisTemplate.opsForZSet()[ZSet(有序集合)] */ @Test public void test01(){ // stringRedisTemplate.opsForValue().append("lp","88");// String a = stringRedisTemplate.opsForValue().get("lp");// System.out.println(a); stringRedisTemplate.opsForList().leftPush("mylist","1"); stringRedisTemplate.opsForList().leftPush("mylist","2"); }
发现操作一切正常!!!!!!

序列化问题
想将对象放进redis中存储,就得解决下面的问题:
1.将对象所代表的类实现Serializable接口:
public class Employee implements Serializable { ... ...}
2.在测试类中模拟存储对象:
@Autowired EmployeeMapper employeeMapper; @Autowired StringRedisTemplate stringRedisTemplate; @Autowired RedisTemplate redisTemplate; @Test public void test02(){ Employee employee = employeeMapper.getEmpById(1); redisTemplate.opsForValue().set("emp-01",employee); }
结果发现虽然存进入了,但key 和 value 我们都看不懂:

@Bean public RedisTemplate
2.在操作时,不用原来的stringRedisTemplate和redisTemplate,而使用该bean来操作:
@Autowired RedisTemplateempRedisTemplate; @Test public void test02(){ Employee employee = employeeMapper.getEmpById(1); empRedisTemplate.opsForValue().set("emp-01",employee); }
运行结果很成功,也很直观:

使用Jedis来操作redis非关系型数据库
在上面的基础下,加下面的依赖:
redis.clients jedis
如果不做任何配置,redis的连接工厂使用的依旧是lettuce
运行下面代码:@Autowired RedisConnectionFactory redisConnectionFactory; @Test void test1(){ System.out.println(redisConnectionFactory.getClass()); }
spring: redis: host: 192.168.2.128 port: 6379 client-type: jedis #!!!!!!!!!!!!!!!!!!!配置这个东西 #下面的可以选择配置# jedis:# pool:# max-active: 10
再次运行上面的测试代码块结果如下:

1.编写拦截器:
package com.atguigu.admin.interceptor;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@Componentpublic class RedisUrlCountInterceptor implements HandlerInterceptor { @Autowired StringRedisTemplate redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String uri = request.getRequestURI(); //默认每次访问当前uri就会计数+1 redisTemplate.opsForValue().increment(uri); return true; }}
2.注册拦截器
@Configurationpublic class AdminWebConfig implements WebMvcConfigurer{ /** * Filter、Interceptor 几乎拥有相同的功能? * 1、Filter是Servlet定义的原生组件。好处,脱离Spring应用也能使用 * 2、Interceptor是Spring定义的接口。可以使用Spring的自动装配等功能 * */ @Autowired RedisUrlCountInterceptor redisUrlCountInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(redisUrlCountInterceptor) .addPathPatterns("/**") .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**", "/js/**","/aa/**"); }
3.编写controller:
@GetMapping("/main.html") public String mainPage(HttpSession session,Model model){ ValueOperationsopsForValue = redisTemplate.opsForValue(); String s = opsForValue.get("/main.html"); String s1 = opsForValue.get("/sql"); model.addAttribute("mainCount",s); model.addAttribute("sqlCount",s1); return "main"; }
4.在main页面用themeleaf模板引擎取值显示:
230/main.html3490/sql
显示效果:

发表评论
最新留言
表示我来过!
[***.240.166.169]2025年03月31日 01时51分09秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Cassandra数据建模
2021-05-09
Internet Explorer 10 专题上线
2021-05-09
云计算之路-阿里云上:0:25~0:40网络存储故障造成网站不能正常访问
2021-05-09
网站故障公告1:使用阿里云RDS之后一个让人欲哭无泪的下午
2021-05-09
上周热点回顾(6.3-6.9)
2021-05-09
上周热点回顾(8.12-8.18)
2021-05-09
【故障公告】升级阿里云 RDS SQL Server 实例故障经过
2021-05-09
蹒跚来迟:新版博客后台上线公测
2021-05-09
[网站公告]11月26日00:00-04:00阿里云RDS升级
2021-05-09
[网站公告]又拍云API故障造成图片无法上传(已恢复)
2021-05-09
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了
2021-05-09
上周热点回顾(6.9-6.15)
2021-05-09
上周热点回顾(10.20-10.26)
2021-05-09
上周热点回顾(2.16-2.22)
2021-05-09
上周热点回顾(3.2-3.8)
2021-05-09
.NET跨平台之旅:借助ASP.NET 5 Beta5的新特性显示CLR与操作系统信息
2021-05-09
上周热点回顾(7.27-8.2)
2021-05-09
上周热点回顾(5.9-5.15)
2021-05-09
上周热点回顾(1.16-1.22)
2021-05-09
上周热点回顾(1.23-1.29)
2021-05-09