package com.picchealth.special.redis.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
//@ConditionalOnProperty(
// prefix = "spring.redis",
// value = {"enable"},
// havingValue = "true"
//)
public final class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
public RedisUtil() {
}
/**
* 爲給定 key 設置過期時間,以秒計
*
* @author: YaoGX
* @Date: 2020/6/22 10:39
*/
public boolean expire(String key, long time) {
try {
if (time > 0L) {
this.redisTemplate.expire("insure_" + key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception var5) {
var5.printStackTrace();
return false;
}
}
/**
* 獲取key 設置的過期時間
*
* @author: YaoGX
* @Date: 2020/6/22 10:40
*/
public long getExpire(String key) {
return this.redisTemplate.getExpire("insure_" + key, TimeUnit.SECONDS);
}
/**
* 確定哈希hashKey是否存在
*
* @author: YaoGX
* @Date: 2020/6/22 11:07
*/
public boolean hasKey(String key) {
try {
return this.redisTemplate.hasKey("insure_" + key);
} catch (Exception var3) {
var3.printStackTrace();
return false;
}
}
/**
* 批量刪除指定 key
*
* @author: YaoGX
* @Date: 2020/6/22 11:23
*/
public void del(String... keys) {
if (keys != null && keys.length > 0) {
List list = new ArrayList();
for (int i = 0; i < keys.length; i++) {
String key= "insure_"+ keys[i];
list.add(key);
}
if (keys.length == 1) {
this.redisTemplate.delete(list.get(0));
} else {
this.redisTemplate.delete(list);
}
}
}
/**
* 根據key 獲取value
*
* @author: YaoGX
* @Date: 2020/6/22 11:24
*/
public Object get(String key) {
return key == null ? null : this.redisTemplate.opsForValue().get("insure_" + key);
}
/**
* 向指定 key 賦值 value
*
* @author: YaoGX
* @Date: 2020/6/22 11:26
*/
public boolean set(String key, Object value) {
try {
this.redisTemplate.opsForValue().set("insure_" + key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
/**
* 向 key 中賦值 value 並設置過期時間 time ,單位 秒
*
* @author: YaoGX
* @Date: 2020/6/22 11:27
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0L) {
this.redisTemplate.opsForValue().set("insure_" + key, value, time, TimeUnit.SECONDS);
} else {
this.set("insure_" + key, value);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
/**
* 給指定 key 設置自增因子
*
* @author: YaoGX
* @Date: 2020/6/22 11:34
*/
public long incr(String key, long delta) {
if (delta < 0L) {
throw new RuntimeException("遞增因子必須大於0");
} else {
return this.redisTemplate.opsForValue().increment("insure_" + key, delta);
}
}
/**
* 給指定 key 設置自減因子
*
* @author: YaoGX
* @Date: 2020/6/22 11:34
*/
public long decr(String key, long delta) {
if (delta < 0L) {
throw new RuntimeException("遞減因子必須大於0");
} else {
return this.redisTemplate.opsForValue().increment("insure_" + key, -delta);
}
}
/**** Redis 的 hash 類型 ****/
/**
* hash 取值,根據 key和item 取值
*
* @author: YaoGX
* @Date: 2020/6/22 11:36
*/
public Object hget(String key, String item) {
return this.redisTemplate.opsForHash().get("insure_" + key, item);
}
/**
* 根據key 獲取變量中的鍵值對。
*
* @author: YaoGX
* @Date: 2020/6/22 11:41
*/
public Map<Object, Object> hmget(String key) {
return this.redisTemplate.opsForHash().entries("insure_" + key);
}
/**
* 以map集合的形式添加鍵值對。
*
* @author: YaoGX
* @Date: 2020/6/22 11:43
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
this.redisTemplate.opsForHash().putAll("insure_" + key, map);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
/**
* 以map集合的形式添加鍵值對並設置過期時間
*
* @author: YaoGX
* @Date: 2020/6/22 13:32
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
this.redisTemplate.opsForHash().putAll("insure_" + key, map);
if (time > 0L) {
this.expire("insure_" + key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
/**
* 新增hashMap值
*
* @author: YaoGX
* @Date: 2020/6/22 13:33
*/
public boolean hset(String key, String item, Object value) {
try {
this.redisTemplate.opsForHash().put("insure_" + key, item, value);
return true;
} catch (Exception var5) {
var5.printStackTrace();
return false;
}
}
/**
* 新增hashMap值,並設置超時時間
*
* @author: YaoGX
* @Date: 2020/6/22 13:33
*/
public boolean hset(String key, String item, Object value, long time) {
try {
this.redisTemplate.opsForHash().put("insure_" + key, item, value);
if (time > 0L) {
this.expire("insure_" + key, time);
}
return true;
} catch (Exception var7) {
var7.printStackTrace();
return false;
}
}
/**
* 刪除變量中的鍵值對,可以傳入多個參數,刪除多個鍵值對。
*
* @author: YaoGX
* @Date: 2020/6/22 13:37
*/
public void hdel(String key, Object... item) {
this.redisTemplate.opsForHash().delete("insure_" + key, item);
}
/**
* 判斷變量中是否有指定的map鍵。
*
* @author: YaoGX
* @Date: 2020/6/22 13:39
*/
public boolean hHasKey(String key, String item) {
return this.redisTemplate.opsForHash().hasKey("insure_" + key, item);
}
/**
* 使變量中的鍵以double值的大小進行自增長。
*
* @author: YaoGX
* @Date: 2020/6/22 13:40
*/
public double hincr(String key, String item, double by) {
return this.redisTemplate.opsForHash().increment("insure_" + key, item, by);
}
/**
* 使變量中的鍵以double值的大小進行自減。
*
* @author: YaoGX
* @Date: 2020/6/22 13:40
*/
public double hdecr(String key, String item, double by) {
return this.redisTemplate.opsForHash().increment("insure_" + key, item, -by);
}
/**** Redis set ****/
/**
* 根據 key獲取 值
*
* @author: YaoGX
* @Date: 2020/6/22 13:42
*/
public Set<Object> sGet(String key) {
try {
return this.redisTemplate.opsForSet().members("insure_" + key);
} catch (Exception var3) {
var3.printStackTrace();
return null;
}
}
/**
* 檢查給定的元素是否在變量中
* @author: YaoGX
* @Date: 2020/6/22 13:43
*/
public boolean sHasKey(String key, Object value) {
try {
return this.redisTemplate.opsForSet().isMember("insure_" + key, value);
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
/**
* 向變量中批量添加值。
* @author: YaoGX
* @Date: 2020/6/22 13:45
*/
public long sSet(String key, Object... values) {
try {
return this.redisTemplate.opsForSet().add("insure_" + key, values);
} catch (Exception var4) {
var4.printStackTrace();
return 0L;
}
}
/**
* 向變量中批量添加值並設置過期時間。
* @author: YaoGX
* @Date: 2020/6/22 13:45
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = this.redisTemplate.opsForSet().add("insure_" + key, values);
if (time > 0L) {
this.expire("insure_" + key, time);
}
return count;
} catch (Exception var6) {
var6.printStackTrace();
return 0L;
}
}
/**
* 獲取變量中值的個數。
* @author: YaoGX
* @Date: 2020/6/22 13:47
*/
public long sGetSetSize(String key) {
try {
return this.redisTemplate.opsForSet().size("insure_" + key);
} catch (Exception var3) {
var3.printStackTrace();
return 0L;
}
}
/**
* 根據key 批量移除變量中的元素。
* @author: YaoGX
* @Date: 2020/6/22 13:49
*/
public long setRemove(String key, Object... values) {
try {
Long count = this.redisTemplate.opsForSet().remove("insure_" + key, values);
return count;
} catch (Exception var4) {
var4.printStackTrace();
return 0L;
}
}
/**** Redis List ****/
/**
* 根據key 獲取指定區間的值
* @author: YaoGX
* @Date: 2020/6/22 13:49
*/
public List<Object> lGet(String key, long start, long end) {
try {
return this.redisTemplate.opsForList().range("insure_" + key, start, end);
} catch (Exception var7) {
var7.printStackTrace();
return null;
}
}
/**
* 根據key 獲取 list集合的長度
* @author: YaoGX
* @Date: 2020/6/22 13:57
*/
public long lGetListSize(String key) {
try {
return this.redisTemplate.opsForList().size("insure_" + key);
} catch (Exception var3) {
var3.printStackTrace();
return 0L;
}
}
/**
* 根據key 獲取集合指定位置的值
* @author: YaoGX
* @Date: 2020/6/22 13:58
*/
public Object lGetIndex(String key, long index) {
try {
return this.redisTemplate.opsForList().index("insure_" + key, index);
} catch (Exception var5) {
var5.printStackTrace();
return null;
}
}
/**
* 向集合最右邊添加元素。
* @author: YaoGX
* @Date: 2020/6/22 13:59
*/
public boolean lSet(String key, Object value) {
try {
this.redisTemplate.opsForList().rightPush("insure_" + key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
/**
* 向集合最右邊添加元素並設置過期時間。
* @author: YaoGX
* @Date: 2020/6/22 14:00
*/
public boolean lSet(String key, Object value, long time) {
try {
this.redisTemplate.opsForList().rightPush("insure_" + key, value);
if (time > 0L) {
this.expire("insure_" + key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
/**
* 向集合右邊批量添加元素。
* @author: YaoGX
* @Date: 2020/6/22 14:00
*/
public boolean lSet(String key, List<Object> value) {
try {
this.redisTemplate.opsForList().rightPushAll("insure_" + key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
/**
* 向集合右邊批量添加元素並設置過期時間。
* @author: YaoGX
* @Date: 2020/6/22 14:00
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
this.redisTemplate.opsForList().rightPushAll("insure_" + key, value);
if (time > 0L) {
this.expire("insure_" + key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
/**
* 在集合的指定位置插入元素,如果指定位置已有元素,則覆蓋,沒有則新增,超過集合下標+n則會報錯。
* @author: YaoGX
* @Date: 2020/6/22 14:06
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
this.redisTemplate.opsForList().set("insure_" + key, index, value);
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
/**
* 從存儲在鍵中的列表中刪除等於值的元素的第一個計數事件。
* count> 0:刪除等於從左到右移動的值的第一個元素;
* count = 0:刪除等於value的所有元素。
* @author: YaoGX
* @Date: 2020/6/22 14:18
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = this.redisTemplate.opsForList().remove("insure_" + key, count, value);
return remove;
} catch (Exception var6) {
var6.printStackTrace();
return 0L;
}
}
}