StringRedisTemplate
SpringBoot中注解@Autowired即可
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;
import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
public void setStrRedis() {
//给String类型赋值
//key
String key = "name";
//值
String value = "redis";
stringRedisTemplate.opsForValue().set(key, value);
}
@Test
public void getStrRedis() {
//查询String类型key的value
String key = "name";
System.out.println(stringRedisTemplate.opsForValue().get(key));
}
@Test
public void expire() {
// 设置key的过期时间
String key = "name1";
long time = 100;
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
}
@Test
public void del() {
//删除
String key = "name";
stringRedisTemplate.delete(key);
}
}