本地安装redis及客户端工具请参考:redis安装详细步骤 Redis Desktop Manager安装详细步骤
此demo继续在此前SpringBootMybtis项目上做延伸扩展,感兴趣的请参考:SpringBoot+MyBatis+Mysql整合详细步骤
1、pom文件引入redis架包
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、application.yml文件配置redis信息
spring:
#redis配置
redis:
host: 127.0.0.1
port: 6379
password:
lettuce:
pool:
#最大连接数
max-active: 8
#最大阻塞等待时间
max-wait: 6000
#最大空闲连接
max-idle: 8
#最小空闲连接
min-idle: 0
timeout: 5000
database: 0
3、编写RedisController.java
package com.example.api.testRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value="/redis", produces = "application/json; charset=UTF-8")
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping( "/set")
public String setUserName( String name) {
redisTemplate.opsForValue().set("1", name);
return "succees";
}
@RequestMapping( "/get")
public String getUserName( String key) {
return redisTemplate.opsForValue().get(key).toString();
}
}
4、启动项目,使用Postman进行接口测试redis的数据操作
1)set操作如下图
2)get操作
当然如上操作只是最基本的操作,在实际开发过程中不建议直接这样使用,因为不够灵活,所以在现有的基础上继续扩展
5、编写RedisUtil工具类(便于开发使用工具类进行开发提高效率,更加方便快捷)
package com.example.api.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* CreateTime: 2019-08-01 18:14
* ClassName: RedisUtil
* Package: com.example.api.utils
* Describe:Redis工具类
*
* @author XieZhiXin
*/
@Component
public class RedisUtil {
private static final Long SUCCESS = 1L;
@Autowired
private RedisTemplate redisTemplate;
//---------------------- common --------------------------
/**
* 指定缓存失效时间
*
* @param key key值
* @param time 缓存时间
*/
public void expire(String key, long time) {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
} else {
throw MyExceptionUtils.mxe("设置的时间不能为0或者小于0!!");
}
}
/**
* 判断key是否存在
*
* @param key 传入ke值
* @return true 存在 false 不存在
*/
public Boolean existsKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 判断key存储的值类型
*
* @param key key值
* @return DataType[string、list、set、zset、hash]
*/
public DataType typeKey(String key) {
return redisTemplate.type(key);
}
/**
* 删除指定的一个数据
*
* @param key key值
* @return true 删除成功,否则返回异常信息
*/
public Boolean deleteKey(String key) {
try {
redisTemplate.delete(key);
return true;
} catch (Exception ex) {
throw MyExceptionUtils.mxe("删除失败!", ex);
}
}
/**
* 删除多个数据
*
* @param keys key的集合
* @return true删除成功,false删除失败
*/
public Boolean deleteKey(Collection<String> keys) {
try {
redisTemplate.delete(keys);
return true;
} catch