springboot笔记10——整合Redis
依赖
<dependencies>
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Redis 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
配置
spring:
redis:
port: 6379
host: 127.0.0.1
password: redis
database: 0
Redis工具类
/**
* @description:
* @author: Jotal
* @time: 2019/8/17 21:29
*/
@Component("redisUtils")
public class RedisUtil {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* @Description: 获取
* @Param: [key]
* @Return: java.lang.String
* @Author: Jotal
* @Date: 2019/8/17 21:39
*/
public String get(String key) {
try {
if (StringUtils.isEmpty(key)) {
return null;
}
return stringRedisTemplate.opsForValue().get(key);
} catch (Exception e) {
System.out.println(String.format("redis缓存获取key的值异常!key:%s", key));
e.printStackTrace();
}
return null;
}
/**
* @Description: 设置键值对
* @Param: [key, value]
* @Return: java.lang.Boolean
* @Author: Jotal
* @Date: 2019/8/17 21:43
*/
public Boolean set(String key,String value) {
try {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
return false;
}
stringRedisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
System.out.println(String.format("redis缓存设置键值对!key:%s,value:%s", key,value));
e.printStackTrace();
}
return false;
}
/**
* @Description: 删除键值对
* @Param: [key]
* @Return: java.lang.Boolean
* @Author: Jotal
* @Date: 2019/8/17 21:47
*/
public Boolean del(String key) {
try {
if (StringUtils.isEmpty(key)) {
return false;
}
return stringRedisTemplate.delete(key);
} catch (Exception e) {
System.out.println(String.format("redis删除键值对!key:%s", key));
e.printStackTrace();
}
return false;
}
/**
* @Description: 设置键值对和缓存时间,单位为秒
* @Param: [key, value, time]
* @Return: java.lang.Boolean
* @Author: Jotal
* @Date: 2019/8/17 21:49
*/
public Boolean setEX(String key, String value, Long time) {
try {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
return false;
}
stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
//设置缓存时间
//stringRedisTemplate.expire(key,time, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
System.out.println("设置缓存异常");
e.printStackTrace();
}
return false;
}
/**
* @Description: 获取key的缓存时间
* @Param: [key]
* @Return: java.lang.Long
* @Author: Jotal
* @Date: 2019/8/17 21:55
*/
public Long getExpireTime(String key) {
try {
if (StringUtils.isEmpty(key)) {
return null;
}
return stringRedisTemplate.getExpire(key,TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("获取缓存异常");
e.printStackTrace();
}
return null;
}
}
单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class Springboot10RedisApplicationTests {
//@Resource是根据名字来自动装配 @Autowired是根据类型来自动装配
@Resource
private RedisUtil redisUtils;
@Test
public void setTest() {
Boolean bl = redisUtils.set("jotal", "jotal1314");
log.info("设置键值对"+bl);
}
@Test
public void getTest() {
String value = redisUtils.get("welcome");
log.info("获取值:"+value);
}
@Test
public void testDelete() {
Boolean flag = redisUtils.del("jotal1");
log.info("testDelete:"+flag);
}
@Test
public void testSetEX() {
Boolean flag = redisUtils.setEX("welcome","www",1000L);
log.info("testSetEX:"+flag);
}
@Test
public void testGetExpireTime() {
Long time = redisUtils.getExpireTime("welcome");
log.info("testSetEX:"+time);
}
}
观察Redis数据库中的键值对变化!