|
@@ -1,10 +1,14 @@
|
|
|
package com.sqx.common.utils;
|
|
package com.sqx.common.utils;
|
|
|
|
|
|
|
|
-import com.google.gson.Gson;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
-import org.springframework.data.redis.core.*;
|
|
|
|
|
|
|
+import org.springframework.data.redis.core.HashOperations;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -13,78 +17,142 @@ import java.util.concurrent.TimeUnit;
|
|
|
*/
|
|
*/
|
|
|
@Component
|
|
@Component
|
|
|
public class RedisUtils {
|
|
public class RedisUtils {
|
|
|
- @Autowired
|
|
|
|
|
|
|
+ @Resource
|
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
|
- @Autowired
|
|
|
|
|
- private ValueOperations<String, String> valueOperations;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private HashOperations<String, String, Object> hashOperations;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private ListOperations<String, Object> listOperations;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private SetOperations<String, Object> setOperations;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private ZSetOperations<String, Object> zSetOperations;
|
|
|
|
|
- /** 默认过期时长,单位:秒 */
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 默认过期时长为24小时,单位:秒
|
|
|
|
|
+ */
|
|
|
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
|
|
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 过期时长为6小时,单位:秒
|
|
|
|
|
+ */
|
|
|
|
|
+ public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 过期时长为1小时,单位:秒
|
|
|
|
|
+ */
|
|
|
|
|
+ public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 过期时长为半小时,单位:秒
|
|
|
|
|
+ */
|
|
|
|
|
+ public final static long HALF_HOUR_ONE_EXPIRE = 30 * 60 * 1L;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 过期时长为10分钟时,单位:秒
|
|
|
|
|
+ */
|
|
|
|
|
+ public final static long TEN_MINUTE_ONE_EXPIRE = 10 * 60 * 1L;
|
|
|
|
|
+
|
|
|
/** 不设置过期时长 */
|
|
/** 不设置过期时长 */
|
|
|
public final static long NOT_EXPIRE = -1;
|
|
public final static long NOT_EXPIRE = -1;
|
|
|
- private final static Gson Gson = new Gson();
|
|
|
|
|
|
|
|
|
|
- public void set(String key, Object value, long expire){
|
|
|
|
|
- valueOperations.set(key, toJson(value));
|
|
|
|
|
- if(expire != NOT_EXPIRE){
|
|
|
|
|
- redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
|
|
|
|
|
|
+ public void set(String key, Object value, long expire) {
|
|
|
|
|
+ redisTemplate.opsForValue().set(key, value);
|
|
|
|
|
+ if (expire != NOT_EXPIRE) {
|
|
|
|
|
+ expire(key, expire);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void set(String key, Object value){
|
|
|
|
|
- set(key, value, DEFAULT_EXPIRE);
|
|
|
|
|
|
|
+ public void set(String key, Object value) {
|
|
|
|
|
+ redisTemplate.opsForValue().set(key, value);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- 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);
|
|
|
|
|
|
|
+ public Object get(String key, long expire) {
|
|
|
|
|
+ Object value = redisTemplate.opsForValue().get(key);
|
|
|
|
|
+ if (expire != NOT_EXPIRE) {
|
|
|
|
|
+ expire(key, expire);
|
|
|
}
|
|
}
|
|
|
- return value == null ? null : fromJson(value, clazz);
|
|
|
|
|
|
|
+ return value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public <T> T get(String key, Class<T> clazz) {
|
|
|
|
|
- return get(key, clazz, NOT_EXPIRE);
|
|
|
|
|
|
|
+ public Object get(String key) {
|
|
|
|
|
+ return get(key, 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 Long increment(String key) {
|
|
|
|
|
+ return redisTemplate.opsForValue().increment(key);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public String get(String key) {
|
|
|
|
|
- return get(key, NOT_EXPIRE);
|
|
|
|
|
|
|
+ public Boolean hasKey(String key) {
|
|
|
|
|
+ return redisTemplate.hasKey(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Set<String> keys(String pattern) {
|
|
|
|
|
+ return redisTemplate.keys(pattern);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void delete(String key) {
|
|
public void delete(String key) {
|
|
|
redisTemplate.delete(key);
|
|
redisTemplate.delete(key);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 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);
|
|
|
|
|
|
|
+ public void delete(Collection<String> keys) {
|
|
|
|
|
+ redisTemplate.delete(keys);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Object hGet(String key, String field) {
|
|
|
|
|
+ return redisTemplate.opsForHash().get(key, field);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Map<String, Object> hGetAll(String key) {
|
|
|
|
|
+ HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
|
|
|
|
|
+ return hashOperations.entries(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void hMSet(String key, Map<String, Object> map) {
|
|
|
|
|
+ hMSet(key, map, DEFAULT_EXPIRE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void hMSet(String key, Map<String, Object> map, long expire) {
|
|
|
|
|
+ redisTemplate.opsForHash().putAll(key, map);
|
|
|
|
|
+
|
|
|
|
|
+ if (expire != NOT_EXPIRE) {
|
|
|
|
|
+ expire(key, expire);
|
|
|
}
|
|
}
|
|
|
- return Gson.toJson(object);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * JSON数据,转成Object
|
|
|
|
|
- */
|
|
|
|
|
- private <T> T fromJson(String json, Class<T> clazz){
|
|
|
|
|
- return Gson.fromJson(json, clazz);
|
|
|
|
|
|
|
+ public void hSet(String key, String field, Object value) {
|
|
|
|
|
+ hSet(key, field, value, DEFAULT_EXPIRE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void hSet(String key, String field, Object value, long expire) {
|
|
|
|
|
+ redisTemplate.opsForHash().put(key, field, value);
|
|
|
|
|
+
|
|
|
|
|
+ if (expire != NOT_EXPIRE) {
|
|
|
|
|
+ expire(key, expire);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void expire(String key, long expire) {
|
|
|
|
|
+ redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void expireAt(String key, Date expire) {
|
|
|
|
|
+ redisTemplate.expireAt(key, expire);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long getExpire(String key) {
|
|
|
|
|
+ return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void hDel(String key, Object... fields) {
|
|
|
|
|
+ redisTemplate.opsForHash().delete(key, fields);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void leftPush(String key, Object value) {
|
|
|
|
|
+ leftPush(key, value, DEFAULT_EXPIRE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void leftPush(String key, Object value, long expire) {
|
|
|
|
|
+ redisTemplate.opsForList().leftPush(key, value);
|
|
|
|
|
+
|
|
|
|
|
+ if (expire != NOT_EXPIRE) {
|
|
|
|
|
+ expire(key, expire);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Object rightPop(String key) {
|
|
|
|
|
+ return redisTemplate.opsForList().rightPop(key);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|