redis介绍
redis是REmote DIctionary Server(远程字典服务器)的缩写,他以字典结构存储数据,并允许其他应用通过TCP协议读写字典中的内容。
Redis支持的数据类型包括:
字符串类型(String)、
散列类型(hash)、
列表类型(list)、
集合类型(set)、
有序集合类型(Zset)。
redis数据库中的所有数据都存储在内存中,由于内存的读写速度远远快于硬盘,redis在性能上对于其他基于硬盘存储的数据库有非常明显的优势,
下载redis
官网下载:https://redis.io/download/
,Windows版本我在GitHub上下载:https://github.com/tporadowski/redis/releases
启动redis server命令:redis-server
指定端口:redis-server --port 6378
这样服务就启动了!
spring boot整合redis
SpringDataRedis
SpringDataRedis提供对redis客户端的整合,提供RedisTemplate统一API来操作Redis,支持Redis的发布订阅模型,支持Redis哨兵和Redis集群,支持基于Lettuce的响应式编程
RedisTemplate
API | 返回值类型 | 说明 |
---|---|---|
redisTemplate.opsForValue() | ValueOperations<K, V> | 操作String类型 |
redisTemplate.opsForList() | ListOperations<K, V> | 操作List类型 |
redisTemplate.opsForHash() | HashOperations<K, HK, HV> | 操作Hash类型 |
redisTemplate.opsForSet() | SetOperations<K, V> | 操作set类型 |
redisTemplate.opsForZSet() | ZSetOperations<K, V> | 操作zset类型 |
idea创建spring boot项目
pom.xml文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml文件redis配置
spring:
redis:
# Redis 服务器地址
host: 127.0.0.1
# Redis 服务器连接端口
port: 6379
# Redis 数据库索引(默认为0)
database: 0
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 超时连接
connect-timeout: 1800
# Redis 服务器连接密码(默认为空)
password:
这里我直接用测试类来操作redis
@SpringBootTest
class SpringbootOaApplicationTests {
@Resource
private RedisTemplate redisTemplate;
/**
* String类型
*/
@Test
public void test(){
redisTemplate.opsForValue().set("bar","abc");
System.out.println(redisTemplate.opsForValue().get("bar"));
}
/**
* List类型
*/
@Test
public void test1(){
String key = "list";
ListOperations<String ,String > listOperations = redisTemplate.opsForList();
//push存在list中
listOperations.leftPush(key,"a");
listOperations.leftPush(key,"b");
listOperations.leftPush(key,"c");
//取出最左边和最右边的数据
System.out.println(listOperations.leftPop(key)+listOperations.rightPop(key));
}
@Test
public void test2(){
String key = "list";
ListOperations<String ,String > listOperations = redisTemplate.opsForList();
//push存在list中
listOperations.leftPush(key,"a");
listOperations.leftPush(key,"b");
listOperations.leftPush(key,"c");
//获取列表中所有元素
List<String> range = listOperations.range(key, 0, -1);
for ( String r : range){
System.out.println("list range :"+r);
}
}
/**
* hash类型
*/
@Test
public void test3(){
HashOperations<String,Object,Object> hashOperations = redisTemplate.opsForHash();
hashOperations.put("hash","java","hello!");
System.out.println(hashOperations.get("hash","java"));
}
/**
* set类型
*/
@Test
public void test4(){
SetOperations<String,Object> setOperations = redisTemplate.opsForSet();
setOperations.add("set","just","do","it");
System.out.println(setOperations.members("set"));
}
/**
* zset类型
*/
@Test
public void test5(){
String key = "zset";
//删除键
redisTemplate.delete(key);
ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset","just",1);
zSetOperations.add("zset","do",2);
zSetOperations.add("zset","it",3);
Set<String> zset = zSetOperations.rangeByScore("zset", 0, 3);
for (String set : zset){
System.out.println("zet range:"+set);
}
}
}
redis设置超时失效
/**
* redis设置超时连接
* @throws InterruptedException
*/
@Test
public void test6() throws InterruptedException {
ValueOperations<String ,String> valueOperations = redisTemplate.opsForValue();
valueOperations.set("bar","java", 100, TimeUnit.MILLISECONDS);
Thread.sleep(1000);
String bar = valueOperations.get("bar");
if (bar!=null){
System.out.println("bar is true");
}else {
System.out.println("bar is false");
}
}