step1:在pom.xml文件中引入所需的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
step2:在application.properties配置文件中写配置信息:
### redis 缓存配置
### 默认redis 数据库为db0
spring.redis.database=0
### 服务器地址,默认为localhost
spring.redis.host=localhost
### 链接端口,默认为6379
spring.redis.port=6379
### redis 密码默认为空
spring.redis.password=
step3:
第一个用于value为字符串类型的key-value键值对操作
第二个用于value为哈希表类型的key-value键值对操作
第三个用于value为列表类型的key-value键值对操作
第四个用于value为set类型的key-value键值对操作
第五个用于value为有序set类型的key-value键值对操作
操作value是字符串类型的key-value
@Resource
private StringRedisTemplate stringRedisTemplate;
@Test
public void testString()
{
stringRedisTemplate.opsForValue().set("city","beijing");
String value=stringRedisTemplate.opsForValue().get("city");
System.out.println(value);// beijing
//最后一个参数表示单位,10秒
stringRedisTemplate.opsForValue().set("city2","shanghai",10l, TimeUnit.SECONDS);
}
操作value是哈希类型的key-value
@Resource
private StringRedisTemplate stringRedisTemplate;
@Test
public void testHash()
{
stringRedisTemplate.opsForHash().put("001","name","xiaoming");
stringRedisTemplate.opsForHash().put("001","age","20");
stringRedisTemplate.opsForHash().put("001","address","beijing");
String age=(String)stringRedisTemplate.opsForHash().get("001","age");
System.out.println(age);
Set feilds=stringRedisTemplate.opsForHash().keys("001");
System.out.println(feilds);//[name, age, address]
List values=stringRedisTemplate.opsForHash().values("001");
System.out.println(values);//[xiaoming, 20, beijing]
}
操作value是list类型的key-value
@Resource
private StringRedisTemplate stringRedisTemplate;
@Test
public void testList()
{
stringRedisTemplate.opsForList().leftPush("002","a");//存入一个值
stringRedisTemplate.opsForList().leftPushAll("002","b","c","d");//一次性存入多个值
List list=stringRedisTemplate.opsForList().range("002",0,-1);
System.out.println(list);//[d, c, b, a]
String temp= stringRedisTemplate.opsForList().rightPop("002");
System.out.println(temp);//a
String temp2= stringRedisTemplate.opsForList().leftPop("002");
System.out.println(temp2);//d
Long length=stringRedisTemplate.opsForList().size("002");
System.out.println(length);//2
}
操作value是set类型的key-value
@Test
public void testSet()
{
stringRedisTemplate.opsForSet().add("003","a","b","c","a");
Set set=stringRedisTemplate.opsForSet().members("003");
System.out.println(set);//[a, b, c]
stringRedisTemplate.opsForSet().remove("003","a");
}
操作value是Zset类型的key-value
@Test
public void testZset()
{
stringRedisTemplate.opsForZSet().add("004","a",10.0);
stringRedisTemplate.opsForZSet().add("004","b",11.0);
stringRedisTemplate.opsForZSet().add("004","c",12.0);
stringRedisTemplate.opsForZSet().add("004","d",13.0);
Set set=stringRedisTemplate.opsForZSet().range("004",0,-1);
System.out.println(set);//[a, b, c, d]
//修改分数
stringRedisTemplate.opsForZSet().incrementScore("004","b",20.0);
set=stringRedisTemplate.opsForZSet().range("004",0,-1);
System.out.println(set);//[a, c, d, b]
stringRedisTemplate.opsForZSet().remove("004","a","b");
}