StringRedisTemplate簡單操作Redis
阿新 • • 發佈:2019-01-28
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); } }