spring-data-redis对redis进行操作

官网文档

// string 类型数据操作
// 获取对string的进行操作的操作对象
ValueOperations<String, String> stringOperations = redisTemplate.opsForValue();

//永久保存,不设置过期时间
stringOperations.set("key1", "value1");

//存储并设置30秒过期
stringOperations.set("key2", "value2", 30, TimeUnit.SECONDS);

//判断可以是否存在,存在则不进行存储,不存在则进行存储
stringOperations.setIfAbsent("key1", "value3");
stringOperations.setIfAbsent("key3", "value3");

String value1 = stringOperations.get("key1");
String value2 = stringOperations.get("key2");
String value3 = stringOperations.get("key3");
System.out.println("value1:" + value1);
System.out.println("value2:" + value2);
System.out.println("value3:" + value3);

//-----------------------------------------------------------------------------------------------


猜你喜欢

转载自my.oschina.net/haokevin/blog/1825857