SpringBoot整合Redis
Redis介绍
Redis是一个完全开源免费的, 是一个高性能的key-value数据库。
Redis 与其他 key - value 缓存产品有以下三个特点:
-
Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
-
Redis不仅仅支持简单的key-value类型的数据,同时还提供结构list,set,zset,hash等数据的存储。
-
Redis支持数据的备份,即master-slave模式的数据备份。
Redis优势
-
性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
-
丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
-
原子 – Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。
-
丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。
Redis支持五种数据类型:
string(字符串)、list(列表)、set(集合)、hash(哈希)、zset(sorted set有序集合)
安装Redis
redis没有官方版的windows版本,但是GitHub有开源组织维护的版本
redis windows64位版本
启动命令redis-server.exe redis.windows.conf
maven依赖
org.springframework.boot spring-boot-starter-redis org.springframework.boot spring-boot-starter-data-redis
properties配置
#端口号server.port=8066#redis配置spring.redis.database=1spring.redis.host=localhostspring.redis.port=6379spring.redis.timeout=3000
单元测试类测试存取
package com.example.inchlifc;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class InchlifcApplicationTests { @Autowired StringRedisTemplate stringRedisTemplate; @Test public void contextLoads() { //保存字符串 stringRedisTemplate.opsForValue().set("redisTest", "小小渔夫"); }}
测试结果