1、pom.xml配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2、application.yml配置
spring:
redis:
database: 0
host: 192.168.207.18
pool:
max-active: 150
max-idle: 30
max-wait: 3000
min-idle: 10
port: 6379
3、Controller层
@Autowired
private RedisService redisService;
@RequestMapping("getuser")
public Integer getUser() {
String keys = redisService.get("WECHAT:CUSTOMIZE:MENU:VERSION",String.class);
System.out.println(keys);
Integer userById = userService.getUserById();
return userById;
}
4.service层
package com.example.demo.service;
/**
- @Description:
- @author: YuanTong-ZXY
- @Date: 2019/2/20 13:17
*/
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.annotation.Resource;
import java.util.Set;
@Service
public class RedisService{
@Resource
JedisPool jedisPool;
/**
* 获取当个对象
* */
public <T> T get(String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = jedis.get(key);
T t = stringToBean(str, clazz);
return t;
}finally {
returnToPool(jedis);
}
}
/**
* 设置对象
* */
public <T> boolean set(String key,Integer seconds, T value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if(str == null || str.length() <= 0) {
return false;
}
if(seconds <= 0) {
jedis.set(key, str);
}else {
jedis.setex(key, seconds, str);
}
return true;
}finally {
returnToPool(jedis);
}
}
/**
* 设置对象
* */
public <T> boolean setList(String key,Integer seconds, T value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if(str == null || str.length() <= 0) {
return false;
}
if(seconds <= 0) {
jedis.rpush(key, str);
}else {
jedis.rpush(key, str);
expire(key,seconds);
}
return true;
}finally {
returnToPool(jedis);
}
}
/**
* 判断key是否存在
* */
public <T> boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.exists(key);
}finally {
returnToPool(jedis);
}
}
/**
* 增加值
* */
public <T> Long incr(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.incr(key);
}finally {
returnToPool(jedis);
}
}
/**
* 获取某个前缀key的集合
* @param prefix
* @return
*/
public Set<String> getKeys(String prefix){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Set<String> keys = jedis.keys(prefix);
return keys;
}finally {
returnToPool(jedis);
}
}
/**
* 删除key
* @param key
* @return
*/
public boolean delkey(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Long del = jedis.del(key);
return del > 1 ? true : false;
}finally {
returnToPool(jedis);
}
}
/**
* 减少值
* */
public <T> Long decr(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.decr(key);
}finally {
returnToPool(jedis);
}
}
/**
* 重新设置过期时间
* */
public void expire(String key, int seconds) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.expire(key, seconds);
}finally {
returnToPool(jedis);
}
}
private <T> String beanToString(T value) {
if(value == null) {
return null;
}
Class<?> clazz = value.getClass();
if(clazz == int.class || clazz == Integer.class) {
return ""+value;
}else if(clazz == String.class) {
return (String)value;
}else if(clazz == long.class || clazz == Long.class) {
return ""+value;
}else {
return JSON.toJSONString(value);
}
}
@SuppressWarnings("unchecked")
private <T> T stringToBean(String str, Class<T> clazz) {
if(str == null || str.length() <= 0 || clazz == null) {
return null;
}
if(clazz == int.class || clazz == Integer.class) {
return (T)Integer.valueOf(str);
}else if(clazz == String.class) {
return (T)str;
}else if(clazz == long.class || clazz == Long.class) {
return (T)Long.valueOf(str);
}else {
return JSON.toJavaObject(JSON.parseObject(str), clazz);
}
}
private void returnToPool(Jedis jedis) {
if(jedis != null) {
jedis.close();
}
}
}