导入依赖
<!--springboot工程需要继承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<!-- junit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
application.yml配置文件
server:
port: 8000
spring:
redis:
host: localhost
port: 6379
测试
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
public void TestString(){
//存储数据
redisTemplate.opsForValue().set("a1","1");
//查询数据
String a1 = redisTemplate.opsForValue().get("a1");
System.out.println("查询道德数据"+a1);
//判断该键是否存在
Boolean a11 = redisTemplate.hasKey("a1");
System.out.println("该键是否存在"+a11);
//增加2
Long a12 = redisTemplate.opsForValue().increment("a1", 2);
System.out.println("增加后的值为"+a12);
//删除
Boolean aboolean = redisTemplate.delete("a1");
System.out.println("是否删除"+aboolean);
//设置时间 1秒
redisTemplate.opsForValue().set("a1","1", 1,TimeUnit.SECONDS);
String a13="0";
while (a13!=null){
a13 = redisTemplate.opsForValue().get("a1");
}
System.out.println("延时过后数据为null");
}