Redis是一种高性能的非关系型数据库。redis作用在内存,性能极高。SpringBoot同样可以把Redis整合到项目里。
首先,第一步就是为项目添加Redis依赖。在SpringBoot下有spring-boot-starter-data-redis,使用Redis就相当的简单。
![fa1eeeae2951861e10673bc7c0de497b.png](https://img-blog.csdnimg.cn/img_convert/fa1eeeae2951861e10673bc7c0de497b.png)
第二步添加上Redis配置信息。包括Redis服务器的IP、端口、密码等信息,前提是已经安装好Redis服务,密码等信息必须和服务器一致。
![5bb953d89af042109e8f30865732c3ba.png](https://img-blog.csdnimg.cn/img_convert/5bb953d89af042109e8f30865732c3ba.png)
第三步,接下来就要写例子来使用使用Redis,在这个例子中使用的是框架封装Redis的类StringRedisTemplate。为啥用这个呢?,因为例子设计的k-v都是String类型,官方建议符合这种格式的推荐使用 StringRedisTemplate。当k-v是非String类型的object时,推荐使用RedisTemplate。
![376cd7267bb1a7ea888514ea730c997a.png](https://img-blog.csdnimg.cn/img_convert/376cd7267bb1a7ea888514ea730c997a.png)
第四步测试下结果,按照controller里写的路径请求下得到employee里设置的参数。
![2fe0edcc8874652c4ec07f4ffc19adbe.png](https://img-blog.csdnimg.cn/img_convert/2fe0edcc8874652c4ec07f4ffc19adbe.png)
这样看不出redis有没有写入内存,使用redis下的redis-cli可以访问redis数据。这种方式访问数据的时候展示不是很友好。推荐使用RedisDesktopManager,这个图形界面工具可是比命令行好使。
![93d52a1f29830381d2bdf0dc146b590d.png](https://img-blog.csdnimg.cn/img_convert/93d52a1f29830381d2bdf0dc146b590d.png)
这个工具看上去就友好多了,这里面包含了所有写入的值。里面的json字符串也自动转了。
![afcd230e2f2735c9178d94fde2e070a7.png](https://img-blog.csdnimg.cn/img_convert/afcd230e2f2735c9178d94fde2e070a7.png)
这个controller里的redisTemplete.opsForValue().set("redis--3