ServiceException.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package zj.common.exception;
public class ServiceException extends RuntimeException {
public ServiceException() {
}
public ServiceException(String var1) {
super(var1);
}
public ServiceException(String var1, Throwable var2) {
super(var1, var2);
}
public ServiceException(Throwable var1) {
super(var1);
}
}BeansUtil.java
package project.framework.util;
import org.springframework.context.ApplicationContext;
/**
* @author SHNKCS 張軍 {@link <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
* @version 1.00 2020/8/1
*/
public class BeansUtil {
private static ApplicationContext appliactionContext;
public static void setAppliactionContext(ApplicationContext ac) {
appliactionContext = ac;
}
public static ApplicationContext getAppliactionContext() {
return appliactionContext;
}
public static Object getBean(String name) {
return appliactionContext.getBean(name);
}
}RedisUtil.java
package project.framework.util;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import zj.common.exception.ServiceException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author SHNKCS 張軍 {@link <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
* 在Redis中數據都是以key-value的形式存儲的,key規定只能是string類型的,所以我們所討論的數據量類型默認是對value來說的
*/
public final class RedisUtil {
private static RedisTemplate redisTemplate;
static {
redisTemplate = (RedisTemplate) BeansUtil.getBean("redisTemplate");
}
// =============================common============================
/**
* 指定緩存失效時間
*
* @param key 鍵
* @param time 時間
* @param unit 時間格式
* @return
*/
public static Boolean expire(String key, long time, TimeUnit unit) {
try {
if (time > 0) {
return redisTemplate.expire(key, time, unit);
}
return false;
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 指定緩存失效時間
*
* @param key 鍵
* @param time 時間(秒)
* @return
*/
public static Boolean expire(String key, long time) {
return expire(key, time, TimeUnit.SECONDS);
}
/**
* 根據key 獲取過期時間
*
* @param key 鍵 不能為null
* @param unit 時間格式
* @return 時間(秒) 返回0代表為永久有效
*/
public static Long getExpire(String key, TimeUnit unit) {
return redisTemplate.getExpire(key, unit);
}
/**
* 根據key 獲取過期時間
*
* @param key 鍵 不能為null
* @return 時間(秒) 返回0代表為永久有效
*/
public static Long getExpire(String key) {
return getExpire(key, TimeUnit.SECONDS);
}
/**
* 判斷key是否存在
*
* @param key 鍵
* @return true 存在 false不存在
*/
public static Boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 刪除緩存
*
* @param key 可以傳一個值 或多個
*/
@SuppressWarnings("unchecked")
public static <K> void delete(K key) {
redisTemplate.delete(key);
}
// ============================String=============================
/**
* 普通緩存獲取
*
* @param key 鍵
* @return 值
*/
public static <String, V> V get(String key) {
try {
return ((ValueOperations<String, V>) redisTemplate.opsForValue()).get(key);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 普通緩存放入
*
* @param key 鍵
* @param value 值
*/
public static <String, V> void set(String key, V value) {
try {
((ValueOperations<String, V>) redisTemplate.opsForValue()).set(key, value);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 普通緩存放入并設置時間
*
* @param key 鍵
* @param value 值
* @param time 時間 time要大于0 如果time小于等于0 將設置無限期
* @param unit 時間格式
* @return true成功 false 失敗
*/
public static <String, V> void set(String key, V value, long time, TimeUnit var5) {
try {
if (time > 0) {
((ValueOperations<String, V>) redisTemplate.opsForValue()).set(key, value, time, var5);
} else {
set(key, value);
}
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 普通緩存放入并設置時間
*
* @param key 鍵
* @param value 值
* @param time 時間(秒) time要大于0 如果time小于等于0 將設置無限期
* @return true成功 false 失敗
*/
public static <String, V> void set(String key, V value, long time) {
set(key, value, time, TimeUnit.SECONDS);
}
/**
* 遞增/遞減
*
* @param key 鍵
* @param delta 遞增值(大于0遞增因子,小于0遞減因子)
* @return
*/
public static <String, V> Long increment(String key, long delta) {
return ((ValueOperations<String, V>) redisTemplate.opsForValue()).increment(key, delta);
}
// ================================Map=================================
/**
* Map設置值
*
* @param key 鍵
* @param map 對應多個鍵值
* @return true 成功 false 失敗
*/
public static <String, HK, HV> void putAll(String key, Map<? extends HK, ? extends HV> map) {
try {
((HashOperations<String, HK, HV>) redisTemplate.opsForHash()).putAll(key, map);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 獲取map中的值
*
* @param key 鍵 不能為null
* @param item 項 不能為null
* @return 值
*/
public static <String, HK, HV> HV get(String key, HK item) {
return ((HashOperations<String, HK, HV>) redisTemplate.opsForHash()).get(key, item);
}
/**
* 獲取hashKey對應的所有鍵值
*
* @param key 鍵
* @return 對應的多個鍵值
*/
public static <String, HK, HV> Map<HK, HV> entries(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 向一張hash表中放入數據,如果不存在將創建
*
* @param key 鍵
* @param item 項
* @param value 值
*/
public static <String, HK, HV> void put(String key, HK item, HV value) {
try {
redisTemplate.opsForHash().put(key, item, value);
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 刪除hash表中的值
*
* @param key 鍵 不能為null
* @param item 項 可以使多個 不能為null
*/
public static <String, HK> Long delete(String key, HK... item) {
return redisTemplate.opsForHash().delete(key, item);
}
/**
* 判斷hash表中是否有該項的值
*
* @param key 鍵 不能為null
* @param item 項 不能為null
* @return true 存在 false不存在
*/
public static <String, HK> Boolean hasKey(String key, HK item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash遞增 如果不存在,就會創建一個 并把新增后的值返回
*
* @param key 鍵
* @param item 項
* @param by 要增加幾(大于0)
* @return
*/
public static double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash遞減
*
* @param key 鍵
* @param item 項
* @param by 要減少記(小于0)
* @return
*/
public static double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ============================set=============================
/**
* 根據key獲取Set中的所有值
*
* @param key 鍵
* @return
*/
public static Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根據value從一個set中查詢,是否存在
*
* @param key 鍵
* @param value 值
* @return true 存在 false不存在
*/
public static boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將數據放入set緩存
*
* @param key 鍵
* @param values 值 可以是多個
* @return 成功個數
*/
public static long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 將set數據放入緩存
*
* @param key 鍵
* @param time 時間(秒)
* @param values 值 可以是多個
* @return 成功個數
*/
public static long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0)
expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 獲取set緩存的長度
*
* @param key 鍵
* @return
*/
public static long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值為value的
*
* @param key 鍵
* @param values 值 可以是多個
* @return 移除的個數
*/
public static long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ===============================list=================================
/**
* 獲取list緩存的內容
*
* @param key 鍵
* @param start 開始
* @param end 結束 0 到 -1代表所有值
* @return
*/
public static List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 獲取list緩存的長度
*
* @param key 鍵
* @return
*/
public static long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通過索引 獲取list中的值
*
* @param key 鍵
* @param index 索引 index>=0時, 0 表頭,1 第二個元素,依次類推;index<0時,-1,表尾,-2倒數第二個元素,依次類推
* @return
*/
public static Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 將list放入緩存
*
* @param key 鍵
* @param value 值
* @param time 時間(秒)
* @return
*/
public static boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入緩存
*
* @param key 鍵
* @param value 值
* @param time 時間(秒)
* @return
*/
public static boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入緩存
*
* @param key 鍵
* @param value 值
* @param time 時間(秒)
* @return
*/
public static boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入緩存
*
* @param key 鍵
* @param value 值
* @param time 時間(秒)
* @return
*/
public static boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0)
expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根據索引修改list中的某條數據
*
* @param key 鍵
* @param index 索引
* @param value 值
* @return
*/
public static boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N個值為value
*
* @param key 鍵
* @param count 移除多少個
* @param value 值
* @return 移除的個數
*/
public static long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}測試工具類
@Test
public void 測試緩存() {
RedisUtil.set("sks", "字符串值1");
RedisUtil.set("ski", 1);
RedisUtil.set("skd", new Date());
Map<String, Object> m = new HashMap<>();
m.put("ska", "va");
m.put("skb", 2);
RedisUtil.set("skm1", m);
m.put("skc", new Date());
//一定先設置值再緩存
RedisUtil.set("skm1", m);
RedisUtil.set("skm2", m);
String vs = RedisUtil.get("sks");
Integer vi = RedisUtil.get("ski");
Date vd = RedisUtil.get("skd");
Map<String, Object> vm = RedisUtil.get("skm");
System.out.println("值字符串:" + vs);
System.out.println("值數字:" + vi);
System.out.println("值日期:" + vd);
System.out.println("值map:" + vm);
m = new HashMap<>();
m.put("hka", "va");
m.put("hkb", 2);
m.put("hkc", new Date());
m.put("hm2", "m2v");
RedisUtil.putAll("hkm2", m);
Map<String, Object> vm2 = RedisUtil.entries("hkm2");
System.out.println("值map:" + vm2);
RedisUtil.put("hkm2", "ha", "hv");
vm2 = RedisUtil.entries("hkm2");
System.out.println("值map:" + vm2);
}
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://www.dlhighland.cn
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

