package com.sandu.ximon.admin.utils;
|
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.google.gson.Gson;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.*;
|
import org.springframework.stereotype.Component;
|
|
import java.util.List;
|
import java.util.Set;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* Redis工具类
|
*
|
* @author Mark sunlightcs@gmail.com
|
*/
|
@Component("RedisUtils")
|
|
public class RedisUtils {
|
@Autowired
|
private RedisTemplate<String, Object> redisTemplate;
|
@Autowired
|
private ValueOperations<String, String> valueOperations;
|
|
private HashOperations<String, String, Object> hashOperations;
|
|
private ListOperations<String, Object> listOperations;
|
|
private SetOperations<String, Object> setOperations;
|
|
private ZSetOperations<String, Object> zSetOperations;
|
/** 默认过期时长,单位:秒 */
|
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
|
/** 不设置过期时长 */
|
public final static long NOT_EXPIRE = -1;
|
private final static Gson gson = new Gson();
|
|
/**
|
*
|
* @param key
|
* @param value
|
* @param expire 秒数
|
* @return
|
*/
|
public boolean set(String key, Object value, long expire){
|
valueOperations.set(key, toJson(value));
|
if(expire != NOT_EXPIRE){
|
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
}
|
return false;
|
}
|
|
public static RedisUtils getBean(){
|
return (RedisUtils)SpringContextUtils.getBean("RedisUtils");
|
}
|
|
public boolean set(String key, Object value){
|
return set(key, value, DEFAULT_EXPIRE);
|
}
|
|
public <T> T get(String key, Class<T> clazz, long expire) {
|
String value = valueOperations.get(key);
|
if(expire != NOT_EXPIRE){
|
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
}
|
return value == null ? null : fromJson(value, clazz);
|
}
|
|
public <T> T get(String key, Class<T> clazz) {
|
return get(key, clazz, NOT_EXPIRE);
|
}
|
|
public String get(String key, long expire) {
|
String value = valueOperations.get(key);
|
if(expire != NOT_EXPIRE){
|
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
}
|
return value;
|
}
|
|
public String get(String key) {
|
return get(key, NOT_EXPIRE);
|
}
|
|
public boolean delete(String key) {
|
return redisTemplate.delete(key);
|
}
|
|
|
public Set<String> allKeys(){
|
return redisTemplate.keys("*");
|
}
|
|
public Set<String> keys(String str) {
|
return redisTemplate.keys(str);
|
}
|
|
/**
|
* Object转成JSON数据
|
*/
|
private String toJson(Object object){
|
if(object instanceof Integer || object instanceof Long || object instanceof Float ||
|
object instanceof Double || object instanceof Boolean || object instanceof String){
|
return String.valueOf(object);
|
}
|
return gson.toJson(object);
|
}
|
|
/**
|
* JSON数据,转成Object
|
*/
|
private <T> T fromJson(String json, Class<T> clazz){
|
return gson.fromJson(json, clazz);
|
}
|
|
|
public String listToJson(List list) {
|
return JSON.toJSONString(list);
|
}
|
|
public List jsonToList(String json, Class clazz){
|
if(json.length() == 0) {
|
return null;
|
}
|
JSONArray jsonArray = JSON.parseArray(json);
|
return jsonArray.toJavaList(clazz);
|
}
|
|
}
|