|
@@ -0,0 +1,170 @@
|
|
|
+package com.ebei.screen.common.util;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisCallback;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Redis工具类
|
|
|
+ *
|
|
|
+ * @author Levi
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class RedisUtils {
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private ValueOperations<String, String> valueOperations;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认过期时长24小时
|
|
|
+ */
|
|
|
+ public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 永不过期
|
|
|
+ */
|
|
|
+ public final static long NOT_EXPIRE = -1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存值(指定过期时常)
|
|
|
+ *
|
|
|
+ * @param key key值
|
|
|
+ * @param value value值
|
|
|
+ * @param expire 过期时长
|
|
|
+ */
|
|
|
+ public void set(String key, Object value, long expire) {
|
|
|
+ valueOperations.set(key, toJson(value));
|
|
|
+ if (expire != NOT_EXPIRE) {
|
|
|
+ redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量插入数据到redis
|
|
|
+ *
|
|
|
+ * @param dataList
|
|
|
+ * @param expire
|
|
|
+ */
|
|
|
+ public void batchSetRedis(List<Map<String, String>> dataList, long expire) {
|
|
|
+ //使用pipeline方式
|
|
|
+ redisTemplate.executePipelined((RedisCallback<List<Object>>) connection -> {
|
|
|
+ for (Map<String, String> map : dataList) {
|
|
|
+ StringRedisSerializer keySerializer = (StringRedisSerializer) redisTemplate.getKeySerializer();
|
|
|
+ StringRedisSerializer valueSerializer = (StringRedisSerializer) redisTemplate.getValueSerializer();
|
|
|
+ connection.setEx(keySerializer.serialize(map.get("key")), expire, valueSerializer.serialize(map.get("value")));
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存值(默认24小时过期)
|
|
|
+ *
|
|
|
+ * @param key key值
|
|
|
+ * @param value value值
|
|
|
+ */
|
|
|
+ public void set(String key, Object value) {
|
|
|
+ set(key, value, DEFAULT_EXPIRE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取值(指定value要转成的对象&更新过期时间)
|
|
|
+ *
|
|
|
+ * @param key key值
|
|
|
+ * @param clazz 要将value值转成指定类型对象
|
|
|
+ * @param expire 更新过期时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> T get(String key, Class<T> clazz, long expire) {
|
|
|
+ String value = valueOperations.get(key);
|
|
|
+ // 如果key值不是永不过期
|
|
|
+ if (expire != NOT_EXPIRE) {
|
|
|
+ // 更新该key值的过期时间为指定过期时间
|
|
|
+ redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ // 按照指定类型将redis中的JSON数据转换成对象返回
|
|
|
+ return value == null ? null : fromJson(value, clazz);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取值(指定value要转成的对象&默认永不过期)
|
|
|
+ *
|
|
|
+ * @param key key值
|
|
|
+ * @param clazz 要将value值转成指定类型对象
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> T get(String key, Class<T> clazz) {
|
|
|
+ return get(key, clazz, NOT_EXPIRE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取值(直接返回&更新过期时间)
|
|
|
+ *
|
|
|
+ * @param key key值
|
|
|
+ * @param expire 更新过期时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String get(String key, long expire) {
|
|
|
+ String value = valueOperations.get(key);
|
|
|
+ if (expire != NOT_EXPIRE) {
|
|
|
+ // 更新该key值的过期时间为指定过期时间
|
|
|
+ redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取值(根据key取值)
|
|
|
+ *
|
|
|
+ * @param key key值
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String get(String key) {
|
|
|
+ return get(key, NOT_EXPIRE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除(根据key删除)
|
|
|
+ *
|
|
|
+ * @param key key值
|
|
|
+ */
|
|
|
+ public void delete(String key) {
|
|
|
+ redisTemplate.delete(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对象转JSON字符串
|
|
|
+ *
|
|
|
+ * @param object 要转成json的对象
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public 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 JSON.toJSONString(object);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * JSON字符串转指定的对象
|
|
|
+ *
|
|
|
+ * @param json JSON字符串
|
|
|
+ * @param clazz 要转成的对象
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public <T> T fromJson(String json, Class<T> clazz) {
|
|
|
+ return JSONObject.parseObject(json, clazz);
|
|
|
+ }
|
|
|
+}
|