Commit 3b48cfc7 by gaopeng

modify 更新主干代码

parent edadc760
......@@ -84,4 +84,6 @@ liveRunEnvironment = prod_
uedThirdPartyOrderUrl=https://rayscloud.chubanyun.net
## \u7CFB\u7EDF\u73AF\u5883
system.env = pro
\ No newline at end of file
system.env = pro
jsbj.agent.id = 1000009100
\ No newline at end of file
......@@ -86,4 +86,6 @@ liveRunEnvironment = uat_
uedThirdPartyOrderUrl=http://deveapi.chubanyun.net
## \u7CFB\u7EDF\u73AF\u5883
system.env = uat
\ No newline at end of file
system.env = uat
jsbj.agent.id = 12829
\ No newline at end of file
......@@ -51,6 +51,7 @@ public class ParamLogAspect {
Signature signature = joinPoint.getSignature();
String methodName = signature.getName();
String description = paramLog.description();
description = StringUtil.isEmpty(description) ? paramLog.value() : description;
StringBuffer msg = new StringBuffer("【" + methodName + " before】");
if (StringUtil.isEmpty(description)) {
msg.append(joinPoint.getSignature().toString());
......@@ -79,6 +80,7 @@ public class ParamLogAspect {
Signature signature = joinPoint.getSignature();
String methodName = signature.getName();
String description = paramLog.description();
description = StringUtil.isEmpty(description) ? paramLog.value() : description;
StringBuffer msg = new StringBuffer("【" + methodName + " afterReturn】");
if (StringUtil.isEmpty(description)) {
msg.append(joinPoint.getSignature().toString());
......
......@@ -70,6 +70,11 @@ public enum AppTypeEnum {
TB("tb"),
/**
* 直播
*/
LIVE("live"),
/**
* 打卡
*/
CLOCK("clock");
......
package com.pcloud.common.core.dto;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 事件埋点
*
* @author 石纯山
* @date 2018/4/16
*/
public class FrontEventDto implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3065716090982449711L;
/**
* 事件id
*/
private String eventId;
/**
* 漏斗id,当为漏斗事件时
*/
private String funnelId;
/**
* 编辑id
*/
private Long adviserId;
/**
* 渠道id
*/
private Long channelId;
/**
* 来源类型0 后端,1 h5,2 小程序,3 app
*/
private short sourceType;
/**
* 触发时间
*/
private Long timestamp;
/**
* 1:RAYS
*/
private String platCode;
/**
* 0:不是读者,1:读者
*/
private short isReader;
/**
* 当事人id
*/
private Long partyId;
/**
* 用户角色编码
*/
private String systemCode;
/**
* 读者所在的公众号
*/
private Long officeAccountId;
/**
* 平台读者id
*/
private Long wechatUserId;
/**
* 场景id
*/
private Long sceneId;
/**
* 出版id
*/
private Long agentId;
private String Exp1;
private String Exp2;
private String Exp3;
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
public String getFunnelId() {
return funnelId;
}
public void setFunnelId(String funnelId) {
this.funnelId = funnelId;
}
public Long getAdviserId() {
return adviserId;
}
public void setAdviserId(Long adviserId) {
this.adviserId = adviserId;
}
public Long getChannelId() {
return channelId;
}
public void setChannelId(Long channelId) {
this.channelId = channelId;
}
public short getSourceType() {
return sourceType;
}
public void setSourceType(short sourceType) {
this.sourceType = sourceType;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public String getPlatCode() {
return platCode;
}
public void setPlatCode(String platCode) {
this.platCode = platCode;
}
public short getIsReader() {
return isReader;
}
public void setIsReader(short isReader) {
this.isReader = isReader;
}
public Long getOfficeAccountId() {
return officeAccountId;
}
public void setOfficeAccountId(Long officeAccountId) {
this.officeAccountId = officeAccountId;
}
public Long getWechatUserId() {
return wechatUserId;
}
public void setWechatUserId(Long wechatUserId) {
this.wechatUserId = wechatUserId;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
public Long getAgentId() {
return agentId;
}
public void setAgentId(Long agentId) {
this.agentId = agentId;
}
public String getExp1() {
return Exp1;
}
public void setExp1(String exp1) {
Exp1 = exp1;
}
public String getExp2() {
return Exp2;
}
public void setExp2(String exp2) {
Exp2 = exp2;
}
public String getExp3() {
return Exp3;
}
public void setExp3(String exp3) {
Exp3 = exp3;
}
public Long getPartyId() {
return partyId;
}
public void setPartyId(Long partyId) {
this.partyId = partyId;
}
public String getSystemCode() {
return systemCode;
}
public void setSystemCode(String systemCode) {
this.systemCode = systemCode;
}
@Override
public String toString() {
return "PaymentEventDto{" + "eventId='" + eventId + '\'' + ", funnelId='" + funnelId + '\'' + ", adviserId="
+ adviserId + ", channelId=" + channelId + ", sourceType=" + sourceType + ", timestamp=" + timestamp
+ ", platCode='" + platCode + '\'' + ", isReader=" + isReader + ", partyId=" + partyId
+ ", systemCode='" + systemCode + '\'' + ", officeAccountId=" + officeAccountId + ", wechatUserId="
+ wechatUserId + ", sceneId=" + sceneId + ", agentId=" + agentId + ", Exp1='" + Exp1 + '\'' + ", Exp2='"
+ Exp2 + '\'' + ", Exp3='" + Exp3 + '\'' + '}';
}
private static final long serialVersionUID = 7668311875701119389L;
/**
* 事件id
*/
private String eventId;
/**
* 漏斗id,当为漏斗事件时
*/
private String funnelId;
/**
* 事件id集合
*/
private Map<String,String> eventMap;
/**
* 编辑id
*/
private Long adviserId;
/**
* 渠道id
*/
private Long channelId;
/**
* 来源类型0 后端,1 h5,2 小程序,3 app
*/
private short sourceType;
/**
* 触发时间
*/
private Long timestamp;
/**
* 1:RAYS
*/
private String platCode;
/**
* 0:不是读者,1:读者
*/
private short isReader;
/**
* 当事人id
*/
private Long partyId;
/**
* 用户角色编码
*/
private String systemCode;
/**
* 读者所在的公众号
*/
private Long officeAccountId;
/**
* 平台读者id
*/
private Long wechatUserId;
/**
* 场景id
*/
private Long sceneId;
/**
* 出版id
*/
private Long agentId;
private String Exp1;
private String Exp2;
private String Exp3;
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
public String getFunnelId() {
return funnelId;
}
public void setFunnelId(String funnelId) {
this.funnelId = funnelId;
}
public Long getAdviserId() {
return adviserId;
}
public void setAdviserId(Long adviserId) {
this.adviserId = adviserId;
}
public Long getChannelId() {
return channelId;
}
public void setChannelId(Long channelId) {
this.channelId = channelId;
}
public short getSourceType() {
return sourceType;
}
public void setSourceType(short sourceType) {
this.sourceType = sourceType;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public String getPlatCode() {
return platCode;
}
public void setPlatCode(String platCode) {
this.platCode = platCode;
}
public short getIsReader() {
return isReader;
}
public void setIsReader(short isReader) {
this.isReader = isReader;
}
public Long getOfficeAccountId() {
return officeAccountId;
}
public void setOfficeAccountId(Long officeAccountId) {
this.officeAccountId = officeAccountId;
}
public Long getWechatUserId() {
return wechatUserId;
}
public void setWechatUserId(Long wechatUserId) {
this.wechatUserId = wechatUserId;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
public Long getAgentId() {
return agentId;
}
public void setAgentId(Long agentId) {
this.agentId = agentId;
}
public String getExp1() {
return Exp1;
}
public void setExp1(String exp1) {
Exp1 = exp1;
}
public String getExp2() {
return Exp2;
}
public void setExp2(String exp2) {
Exp2 = exp2;
}
public String getExp3() {
return Exp3;
}
public void setExp3(String exp3) {
Exp3 = exp3;
}
public Long getPartyId() {
return partyId;
}
public void setPartyId(Long partyId) {
this.partyId = partyId;
}
public String getSystemCode() {
return systemCode;
}
public void setSystemCode(String systemCode) {
this.systemCode = systemCode;
}
public Map<String, String> getEventMap() {
return eventMap;
}
public void setEventMap(Map<String, String> eventMap) {
this.eventMap = eventMap;
}
public void setEventMap(String eventId, String funnelId) {
if(eventMap == null){
eventMap = new HashMap<>();
}
eventMap.put(eventId,funnelId);
}
@Override
public String toString() {
return "FrontEventDto{" +
"eventId='" + eventId + '\'' +
", funnelId='" + funnelId + '\'' +
", eventMap=" + eventMap +
", adviserId=" + adviserId +
", channelId=" + channelId +
", sourceType=" + sourceType +
", timestamp=" + timestamp +
", platCode='" + platCode + '\'' +
", isReader=" + isReader +
", partyId=" + partyId +
", systemCode='" + systemCode + '\'' +
", officeAccountId=" + officeAccountId +
", wechatUserId=" + wechatUserId +
", sceneId=" + sceneId +
", agentId=" + agentId +
", Exp1='" + Exp1 + '\'' +
", Exp2='" + Exp2 + '\'' +
", Exp3='" + Exp3 + '\'' +
'}';
}
}
/**
*
*/
package com.pcloud.common.enums;
import com.google.common.collect.Maps;
import java.util.Arrays;
import java.util.Map;
/**
* @描述:应用类型常量枚举
* @作者:songx
......@@ -10,55 +12,66 @@ package com.pcloud.common.enums;
*/
public enum AppTypeEnum {
MASTER("MASTER"), // 主应用
RECOMMEND("RECOMMEND"), // 推荐应用
CHARGE("CHARGE"), // 付费应用
MASTER("MASTER"), // 主应用
RECOMMEND("RECOMMEND"), // 推荐应用
CHARGE("CHARGE"), // 付费应用
ARTICLE("ARTICLE"), // 文章
KK("KK"), // 看一看
LIVE("LIVE"), // 直播
VOTE("VOTE"), // 投票
ENROLL("ENROLL"), // 编创赛报名
BM("BM"), // 报名
NAIRE("NAIRE"), // 问卷调查
DA("DA"), // 数据采集
BOOKCARD("BOOKCARD"), // 读书卡片
TUTOR("TUTOR"), // 一对一辅导
GROUP("GROUP"), // 与你同行(圈子)(add by gaop at 2017-6-13 16:52:07)
ITEM_BANK("ITEM_BANK"), // 题库
VOICE_MSG("VOICE_MSG"), // 语音留言
AUDIO("AUDIO"), // 音频
VIDEO("VIDEO"), // 视频
IMAGE("IMAGE"), // 图片
PDF("PDF"), // PDF
BOOK("BOOK"), // 图书
EF("EF"), // 教育表格
QA("QA"), // 问答
LISTEN("LISTEN"), // 听力
ANSWER("ANSWER"), // 答案
ACTIVITY("ACTIVITY"), // 分享互动
ZSCORE("ZSCORE"), // 标准分
RECITE_WORD("RECITE_WORD"), // 背单词
INTERACT("INTERACT"), // 互动墙
SPECIAL("SPECIAL"), // 专题
CLOCK("CLOCK"), // 打卡
TEST("TEST"), // 测试
LIVE_TIMETABLE("LIVE_TIMETABLE"), // 直播课表
DRAW("DRAW"), // 抽奖
WISH("WISH"); // 愿望单
/**
* 枚举转换
*/
public static final Map<String, AppTypeEnum> APP_TYPE_MAP = Maps.newHashMap();
ARTICLE("ARTICLE"), // 文章
KK("KK"), // 看一看
LIVE("LIVE"), // 直播
VOTE("VOTE"), // 投票
ENROLL("ENROLL"), // 编创赛报名
BM("BM"), // 报名
NAIRE("NAIRE"), // 问卷调查
DA("DA"), // 数据采集
BOOKCARD("BOOKCARD"), // 读书卡片
TUTOR("TUTOR"), // 一对一辅导
GROUP("GROUP"), // 与你同行(圈子)(add by gaop at 2017-6-13 16:52:07)
ITEM_BANK("ITEM_BANK"), // 题库
VOICE_MSG("VOICE_MSG"), // 语音留言
AUDIO("AUDIO"), // 音频
VIDEO("VIDEO"), // 视频
IMAGE("IMAGE"), // 图片
PDF("PDF"), // PDF
BOOK("BOOK"), // 图书
EF("EF"), // 教育表格
QA("QA"), // 问答
LISTEN("LISTEN"), // 听力
ANSWER("ANSWER"), // 答案
ACTIVITY("ACTIVITY"), // 分享互动
ZSCORE("ZSCORE"), // 标准分
RECITE_WORD("RECITE_WORD"), // 背单词
INTERACT("INTERACT"), // 互动墙
SPECIAL("SPECIAL"), // 专题
CLOCK("CLOCK"), // 打卡
TEST("TEST"), // 测试
LIVE_TIMETABLE("LIVE_TIMETABLE"), // 直播课表
DRAW("DRAW"), // 抽奖
WISH("WISH"); // 愿望单
static {
Arrays.asList(AppTypeEnum.values()).forEach(m -> {
APP_TYPE_MAP.put(m.value, m);
});
}
/**
* 值
*/
public final String value;
/**
* 值
*/
public final String value;
/**
* 构造
*
* @param value
*/
private AppTypeEnum(String value) {
this.value = value;
}
/**
* 构造
*
* @param value
*/
private AppTypeEnum(String value) {
this.value = value;
}
}
package com.pcloud.common.utils;
import org.springframework.beans.BeanUtils;
/**
*
*
* @author:songx
* @date:2018年8月24日,下午2:18:40
*/
public class BeanNewUtils {
/**
* 实体类之间的转换
*
* @param source
* 来源
* @param clazz
* 目标对象
* @return
*/
public static <T> T copyProperties(Object source, T t) {
if (source == null || t == null) {
return t;
}
BeanUtils.copyProperties(source, t);
return t;
}
}
......@@ -6,6 +6,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.slf4j.Logger;
......@@ -1603,4 +1604,78 @@ public class DateUtils extends org.apache.commons.lang.time.DateUtils {
return calendar.getTime();
}
/**
* 获取今天开始时间
* @return
*/
public static Date getDayBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* 获取今天结束时间
* @return
*/
public static Date getDayEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
/**
* 获取某个日期的开始时间
* @param day
* @return
*/
public static Timestamp getDayStartTime(Date day) {
Calendar calendar = Calendar.getInstance();
if (null != day) calendar.setTime(day);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
/**
* 获取某个日期的结束时间
* @param day 日期
* @return
*/
public static Timestamp getDayEndTime(Date day) {
Calendar calendar = Calendar.getInstance();
if (null != day) calendar.setTime(day);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
/**
* 获取昨天开始时间
*
* @return
*/
public static Date getBeginDayOfYesterday() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
/**
* 获取昨天的结束时间
* @return
*/
public static Date getEndDayOfYesterDay() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
}
/**
*
*
*/
package com.pcloud.common.utils.cache.redis;
......@@ -8,6 +8,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -19,6 +20,7 @@ import com.google.common.collect.Maps;
import com.pcloud.common.utils.ListUtils;
import com.pcloud.common.utils.string.StringUtil;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.JedisCluster;
/**
......@@ -30,1322 +32,1374 @@ import redis.clients.jedis.JedisCluster;
@Component("jedisClusterUtils")
public class JedisClusterUtils {
/**
*
*/
private static Logger LOGGER = LoggerFactory.getLogger(JedisClusterUtils.class);
/**
* 默认过期时间(2小时)
*/
private static final Integer REDIS_SESSION_TIMEOUT = 2 * 60 * 60;
private static JedisCluster jedisCluster;
/**
* 保存一个字符串到redis中,长期有效
*
* @param key
* 键
* @param value
* 值
* @return
*/
public static boolean set(String key, String value) {
try {
jedisCluster.set(key, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个字符串到redis中并指定过期时间
*
* @param key
* 键
* @param value
* 值
* @param seconds
* 有效时间,为null使用默认时间30分钟
* @return
*/
public static boolean set(String key, String value, Integer seconds) {
try {
jedisCluster.set(key, value);
expire(key, seconds);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个字符串到redis中,长期有效
*
* @param objecKey
* 键
* @param objecValue
* 缓存对象
* @return
*/
public static boolean setObject(Object key, Object value) {
try {
jedisCluster.set(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个字符串到redis中并指定过期时间
*
* @param objecKey
* 键
* @param objecValue
* 缓存对象
* @param seconds
* 时间,如果为null,则使用默认时间30分钟
* @return
*/
public static boolean setObject(Object key, Object value, Integer seconds) {
if (key == null || value == null) {
return false;
}
try {
jedisCluster.set(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
expireObject(key, seconds);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 当键在redis中不存在时才设置
*
* @param key
* @param value
* @return
*/
public static boolean setnx(String key, String value) {
long result = 0;
try {
result = jedisCluster.setnx(key, value);
} catch (Exception e) {
LOGGER.warn("jedis Setn缓存保存失败:" + e.getMessage(), e);
return false;
}
return result == 0 ? false : true;
}
/**
* 追加一个字符串到指定的key
*
* @param key
* @param value
* @return
*/
public static boolean append(String key, String value) {
try {
jedisCluster.append(key, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键获取Redis缓存中的值.<br/>
*
* @param key
* 键
* @return
*/
public static String get(String key) {
try {
return jedisCluster.get(key);
} catch (Exception e) {
LOGGER.warn("jedis获取缓存失败:" + e.getMessage(), e);
return null;
}
}
/**
* 根据缓存键获取Redis缓存中的对象.<br/>
*
* @param objecKey
* 键
* @return
*/
public static Object getObject(Object key) {
try {
byte[] bytes = jedisCluster.get(SerializeUtils.serialize(key));
return bytes == null ? null : SerializeUtils.unSerialize(bytes);
} catch (Exception e) {
LOGGER.warn("jedis获取缓存失败:" + e.getMessage(), e);
}
return null;
}
/**
* 根据缓存键清除Redis缓存中的对象.
*
* @param key
* @return
*/
public static boolean delObject(Object key) {
try {
jedisCluster.del(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis删除缓存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键批量清除Redis缓存中的对象.
*
* @param keys
* @return
*/
public static boolean delObject(Object... keys) {
try {
jedisCluster.del(SerializeUtils.serialize(keys));
} catch (Exception e) {
LOGGER.warn("jedis批量删除缓存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键删除Redis缓存中的值.
*
* @param key
* @return
*/
public static boolean del(String key) {
try {
jedisCluster.del(key);
} catch (Exception e) {
LOGGER.warn("jedis删除缓存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键批量清除Redis缓存中的值.
*
* @param keys
* @return
*/
public static boolean del(String... keys) {
try {
jedisCluster.del(keys);
} catch (Exception e) {
LOGGER.warn("jedis批量删除缓存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个Map对象到redis中,长期有效
*
* @param key
* 键
* @param hash
* Map组合的字符串
* @return
*/
public static boolean hmset(String key, Map<String, String> hash) {
try {
jedisCluster.hmset(key, hash);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个Map对象到redis中并指定过期时间
*
* @param key
* 键
* @param hash
* Map组合的字符串
* @param seconds
* 时间
* @return
*/
public static boolean hmset(String key, Map<String, String> hash, Integer seconds) {
try {
jedisCluster.hmset(key, hash);
expire(key, seconds);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 添加一个内容到指定key的hash中
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean hset(String key, String field, String value) {
try {
jedisCluster.hset(key, field, value);
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的hash失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 添加一个内容到指定key的hash中,并转换成json格式
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean hset2Json(String key, String field, Object value) {
try {
jedisCluster.hset(key, field, JSONObject.toJSONString(value));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的hash失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 添加一个Fieid内容到指定key的hash中,如果Fieid存在就不添加,反之添加
*
* @param key
* @param field
* @param value
* @return
*/
public static boolean hsetnx(String key, String field, String value) {
try {
jedisCluster.hsetnx(key, field, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 添加一个Fieid内容到指定key的hash中,如果Fieid存在就不添加,反之添加
*
* @param key
* @param field
* @param value
* @return
*/
public static boolean hsetnx2Json(String key, String field, Object value) {
try {
jedisCluster.hsetnx(key, field, JSONObject.toJSONString(value));
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 指定Map中file的值自增
*
* @param key
* @param field
* @param value
* @return
*/
public static Long hincrBy(String key, String field, Long value) {
try {
return jedisCluster.hincrBy(key, field, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return null;
}
}
/**
* 添加一个内容到指定key的hash中
*
* @param key
* @param field
* @param value
* @return
*/
@Deprecated
public static Boolean hsetObject(Object key, Object field, Object value) {
try {
jedisCluster.hset(SerializeUtils.serialize(key), SerializeUtils.serialize(field),
SerializeUtils.serialize(value));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的hash失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键获取Redis缓存中的Map.<br/>
*
* @param key
* @param fields
* 具体的map键
* @return
*/
public static Map<String, String> hgetAll(String key) {
try {
return jedisCluster.hgetAll(key);
} catch (Exception e) {
LOGGER.warn("jedis获取缓存失败:" + e.getMessage(), e);
return null;
}
}
/**
* 根据缓存键获取Redis缓存中的Map.<br/>
* 可以指定Map具体的键值获取精确数据
*
* @param key
* @param fields
* 具体的map键
* @return
*/
public static Map<String, String> hmget(String key, String... fields) {
Map<String, String> resultMap = Maps.newHashMap();
try {
// 获取完整的map集合
if (fields == null || fields.length == 0) {
return jedisCluster.hgetAll(key);
} else {
List<String> resultList = jedisCluster.hmget(key, fields);
if (!ListUtils.isEmpty(resultList)) {
for (int i = 0; i < resultList.size(); i++) {
resultMap.put(fields[i], resultList.get(i));
}
}
}
} catch (Exception e) {
LOGGER.warn("jedis获取缓存失败:" + e.getMessage(), e);
return null;
}
return resultMap;
}
/**
* 从指定hash中取值
*
* @param key
* @param field
* @return
*/
public static String hget(String key, String field) {
try {
return jedisCluster.hget(key, field);
} catch (Exception e) {
LOGGER.warn("jedis从指定hash中取值失败:" + e.getMessage(), e);
return null;
}
}
/**
* 从指定hash中取值,并将取到的json字符串转换成指定的对象
*
* @param key
* @param field
* @param clazz
* @return
*/
public static <T> T hgetJson2Class(String key, String field, Class<T> clazz) {
try {
return JSONObject.parseObject(jedisCluster.hget(key, field), clazz);
} catch (Exception e) {
LOGGER.warn("jedis从指定hash中取值失败:" + e.getMessage(), e);
return null;
}
}
/**
* 从指定hash中取值,并将取到的json字符串转换成指定的对象集合
*
* @param key
* @param field
* @param clazz
* @return
*/
public static <T> List<T> hgetJson2List(String key, String field, Class<T> clazz) {
try {
return JSONArray.parseArray(jedisCluster.hget(key, field), clazz);
} catch (Exception e) {
LOGGER.warn("jedis从指定hash中取值失败:" + e.getMessage(), e);
return null;
}
}
/**
* 从指定hash中取值
*
* @param key
* @param field
* @return
*/
@Deprecated
public static Object hgetObject(Object key, Object field) {
try {
byte[] bytes = jedisCluster.hget(SerializeUtils.serialize(key), SerializeUtils.serialize(field));
return bytes == null ? null : SerializeUtils.unSerialize(bytes);
} catch (Exception e) {
LOGGER.warn("jedis从指定hash中取对象失败:" + e.getMessage(), e);
}
return null;
}
/**
* 根据缓存键删除map中的指定键值
*
* @param key
* 缓存键
* @param field
* map键值
* @return 删除成功返回1,失败返回0
*/
public static Boolean hdel(String key, String... fields) {
long result = 0L;
try {
result = jedisCluster.hdel(key, fields);
} catch (Exception e) {
LOGGER.warn("jedis删除map中的指定键值失败:" + e.getMessage(), e);
}
return result > 0 ? true : false;
}
/**
* 从hash中删除指定filed的对象
*
* @param key
* @param field
* @return
*/
@Deprecated
public static Boolean hdelObject(Object key, Object... field) {
long result = 0L;
try {
result = jedisCluster.hdel(SerializeUtils.serialize(key), SerializeUtils.serialize(field));
} catch (Exception e) {
LOGGER.warn("jedis从hash中删除指定filed的对象失败:" + e.getMessage(), e);
}
return result > 0 ? true : false;
}
/**
* 获取指定key中存放的hash值个数
*
* @param key
* @return
*/
public static Long hlen(String key) {
try {
return jedisCluster.hlen(key);
} catch (Exception e) {
LOGGER.warn("jedis获取指定key中存放的hash值个数失败:" + e.getMessage(), e);
}
return null;
}
/**
* 判断指定key中是否存在fileId键
*
* @param key
* @return
*/
public static Boolean hexists(String key, String fileId) {
try {
return jedisCluster.hexists(key, fileId);
} catch (Exception e) {
LOGGER.warn("jedis判断指定key中是否存在fileId键失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key中存放的hash值个数
*
* @param key
* @return
*/
public static Long hlenObject(Object key) {
try {
return jedisCluster.hlen(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis获取指定key中存放的hash值个数失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key中map对象中的所有key
*
* @param key
* @return
*/
public static Set<String> hkeys(String key) {
try {
return jedisCluster.hkeys(key);
} catch (Exception e) {
LOGGER.warn("jedis获取指定key中map对象中的所有key失败:" + e.getMessage(), e);
}
return null;
}
/**
* 添加内容到指定key的list中,先进后出
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean lpush(String key, String value) {
Long result = 0L;
try {
result = jedisCluster.lpush(key, value);
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进后出
*
* @param key
* @param obj
* @return
*/
public static Boolean lpushJson(String key, Object obj) {
Long result = 0L;
try {
result = jedisCluster.lpush(key, JSONObject.toJSONString(obj));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进后出
*
* @param key
* @param value
* @return
*/
public static Boolean lpushObject(Object key, Object value) {
Long result = 0L;
try {
result = jedisCluster.lpush(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进先出
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean rpush(String key, String value) {
Long result = 0L;
try {
result = jedisCluster.rpush(key, value);
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进先出
*
* @param key
* @param obj
* @return
*/
public static Boolean rpushJson(String key, Object obj) {
Long result = 0L;
try {
result = jedisCluster.rpush(key, JSONObject.toJSONString(obj));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进先出
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean rpushObject(Object key, Object value) {
Long result = 0L;
try {
result = jedisCluster.rpush(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 获取指定key的list集合
*
* @param key
* @param start
* list中的起始位置,从0开始
* @param len
* 长度,-1获取所有
* @return
*/
public static List<String> lrange(String key, long start, long len) {
try {
return jedisCluster.lrange(key, start, len);
} catch (Exception e) {
LOGGER.warn("jedis获取指定key的list集合失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的list长度
*
* @param key
* @return
*/
public static Long llen(String key) {
try {
return jedisCluster.llen(key);
} catch (Exception e) {
LOGGER.warn("jedis获取指定key的list长度失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的list长度
*
* @param key
* @return
*/
public static Long llenObject(Object key) {
try {
return jedisCluster.llen(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis获取指定key的list长度失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(左端)
*
* @param key
* @return
*/
public static String lpop(String key) {
try {
return jedisCluster.lpop(key);
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(左端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(左端)
*
* @param key
* @return
*/
public static List<String> lpop(String key, long count) {
try {
List<String> list = new ArrayList<String>();
for (int i = 0; i < count; i++) {
String vel = jedisCluster.lpop(key);
if (vel != null) {
list.add(vel);
}
}
return list;
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(左端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(左端)
*
* @param key
* @return
*/
public static <T> T lpopJson(String key, Class<T> clazz) {
try {
String vel = jedisCluster.lpop(key);
if (!StringUtil.isEmpty(vel)) {
return JSONObject.parseObject(vel, clazz);
}
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(左端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(左端)
*
* @param key
* @return
*/
public static <T> List<T> lpopJson(String key, long count, Class<T> clazz) {
try {
List<T> list = new ArrayList<T>();
for (int i = 0; i < count; i++) {
String vel = jedisCluster.lpop(key);
if (!StringUtil.isEmpty(vel)) {
list.add(JSONObject.parseObject(vel, clazz));
}
}
return list;
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(左端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(右端)
*
* @param key
* @return
*/
public static String rpop(String key) {
try {
return jedisCluster.rpop(key);
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(右端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(右端)
*
* @param key
* @return
*/
public static List<String> rpop(String key, long count) {
try {
List<String> list = new ArrayList<String>();
for (int i = 0; i < count; i++) {
String vel = jedisCluster.rpop(key);
if (vel != null) {
list.add(vel);
}
}
return list;
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(右端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(右端)
*
* @param key
* @return
*/
public static <T> T rpopJson(String key, Class<T> clazz) {
try {
String vel = jedisCluster.rpop(key);
if (!StringUtil.isEmpty(vel)) {
return JSONObject.parseObject(vel, clazz);
}
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(右端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(右端)
*
* @param key
* @return
*/
public static <T> List<T> rpopJson(String key, long count, Class<T> clazz) {
try {
List<T> list = new ArrayList<T>();
for (int i = 0; i < count; i++) {
String vel = jedisCluster.rpop(key);
if (!StringUtil.isEmpty(vel)) {
list.add(JSONObject.parseObject(vel, clazz));
}
}
return list;
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(右端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 删除指定区间之外的元素
*
* @param key
* @return
*/
public static String ltrim(String key, long start, long len) {
try {
return jedisCluster.ltrim(key, start, len);
} catch (Exception e) {
LOGGER.warn("jedis删除指定区间之外的元素失败:" + e.getMessage(), e);
}
return null;
}
/**
* 有序序列(Sort-Set)集合:添加数据
* @param key
* @return
*/
public static boolean zset(String key, double score, String member) {
try {
jedisCluster.zadd(key, score, member);
} catch (Exception e) {
LOGGER.error("jedis有序序列集合:添加数据失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 获取指定key的Sort-Set集合长度
* @return
*/
public static Long zcard(String key) {
try {
return jedisCluster.zcard(key);
} catch (Exception e) {
LOGGER.error("jedis有序序列集合长度失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的Sort-Set集合中member元素所在的位置排行(从 0 开始)
* @return
*/
public static Long zrank(String key, String member) {
try {
return jedisCluster.zrank(key, member);
} catch (Exception e) {
LOGGER.error("jedis获取有序序列集合中MEMBER位置失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的Sort-Set集合的元素(按照score从小到大排序)
* @return
*/
public static Set<String> zrange(String key, long start, long end) {
try {
return jedisCluster.zrange(key, start, end);
} catch (Exception e) {
LOGGER.error("jedis有序序列集合的元素失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的Sort-Set集合的元素(按照score从大到小排序)
* @return
*/
public static Set<String> zrevrange(String key, long start, long end) {
try {
return jedisCluster.zrevrange(key, start, end);
} catch (Exception e) {
LOGGER.error("jedis有序序列集合的元素失败:" + e.getMessage(), e);
}
return null;
}
/**
* 删除指定key的Sort-Set集合的元素
* @return
*/
public static boolean zrem(String key, long start, long end) {
try {
jedisCluster.zremrangeByRank(key, start, end);
} catch (Exception e) {
LOGGER.error("jedis删除有序序列集合的元素失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据clazz和key获取对应 的类型
*
* @param key
* @param clazz
* @return
*/
public static <T> T getClass(Object key, Class<T> clazz) {
try {
Object object = getObject(key);
return object == null ? null : clazz.cast(object);
} catch (Exception e) {
LOGGER.warn("jedis根据clazz和key获取对应 的类型:" + e.getMessage(), e);
}
return null;
}
/**
* 根据clazz和key获取对应的list
*
* @param key
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getList(Object key, Class<T> clazz) {
try {
Object object = getObject(key);
return object == null ? null : (List<T>) object;
} catch (Exception e) {
LOGGER.warn("jedis根据clazz和key获取对应的list:" + e.getMessage(), e);
}
return null;
}
/**
* 根据clazz和key获取对应的map
*
* @param key
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> getMap(Object key, Class<K> kClazz, Class<V> vClazz) {
try {
Object object = getObject(key);
return object == null ? null : (Map<K, V>) object;
} catch (Exception e) {
LOGGER.warn("jedis根据clazz和key获取对应的map:" + e.getMessage(), e);
}
return null;
}
/**
* 根据key,及clazz获取对象.使用json进行转换
*
* @param key
* @param clazz
* @return
*/
public static <T> T getJson(String key, Class<T> clazz) {
String jsonValue = get(key);
if (StringUtil.isEmpty(jsonValue)) {
return null;
}
try {
return JSONObject.parseObject(jsonValue, clazz);
} catch (Exception e) {
LOGGER.warn("jedis根据key获取clazz对象,JSON转换异常:" + e.getMessage(), e);
}
return null;
}
/**
* 根据key,及clazz获取对象集合.使用json进行转换
*
* @param key
* @param clazz
* @return
*/
public static <T> List<T> getJsonList(String key, Class<T> clazz) {
String jsonValue = get(key);
if (StringUtil.isEmpty(jsonValue)) {
return null;
}
try {
return JSONArray.parseArray(jsonValue, clazz);
} catch (Exception e) {
LOGGER.warn("jedis根据key获取clazz对象,JSON转换异常:" + e.getMessage(), e);
}
return null;
}
/**
* 缓存对象.使用json进行转换
*
* @param key
* @param obj
*/
public static Boolean setJson(String key, Object obj) {
try {
return set(key, JSONObject.toJSONString(obj));
} catch (Exception e) {
LOGGER.warn("jedis根据key缓存对象,JSON转换异常:" + e.getMessage(), e);
}
return false;
}
/**
* 缓存对象.使用json进行转换,带有效期
*
* @param key
* @param obj
* @param seconds
* 有效期(秒)
* @return
*/
public static Boolean setJson(String key, Object obj, Integer seconds) {
try {
return set(key, JSONObject.toJSONString(obj), seconds);
} catch (Exception e) {
LOGGER.warn("jedis根据key缓存对象,JSON转换异常:" + e.getMessage(), e);
}
return false;
}
/**
* 缓存对象集合.使用json进行转换
*
* @param key
* @param obj
* @return
*/
public static Boolean setJsonList(String key, Object obj) {
try {
return set(key, JSONArray.toJSONString(obj));
} catch (Exception e) {
LOGGER.warn("jedis根据key缓存对象,JSON转换异常:" + e.getMessage(), e);
}
return false;
}
/**
* 缓存对象集合.使用json进行转换
*
* @param key
* @param obj
* @return
*/
public static Boolean setJsonList(String key, Object obj, Integer seconds) {
try {
return set(key, JSONArray.toJSONString(obj), seconds);
} catch (Exception e) {
LOGGER.warn("jedis根据key缓存对象,JSON转换异常:" + e.getMessage(), e);
}
return false;
}
/**
* 设置超时时间
*
* @param key
* @param seconds
* @return
*/
public static boolean expire(String key, Integer seconds) {
try {
jedisCluster.expire(key, seconds == null || seconds < 1 ? REDIS_SESSION_TIMEOUT : seconds);
} catch (Exception e) {
LOGGER.warn("jedis设置超时时间失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 设置超时时间
*
* @param key
* @param seconds
* 超时时间(单位为秒)
* @return
*/
public static boolean expireObject(Object key, Integer seconds) {
try {
jedisCluster.expire(SerializeUtils.serialize(key),
seconds == null || seconds < 1 ? REDIS_SESSION_TIMEOUT : seconds);
} catch (Exception e) {
LOGGER.warn("jedis设置超时时间失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 查看键还有多长时间过期(秒)
*
* @param key
* @return
*/
public static Long ttl(String key) {
try {
return jedisCluster.ttl(key);
} catch (Exception e) {
LOGGER.warn("jedis查看过期时间失败:" + e.getMessage(), e);
}
return null;
}
/**
* 查看键还有多长时间过期(秒)
*
* @param key
* @return
*/
public static Long ttlObject(Object key) {
try {
return jedisCluster.ttl(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis查看过期时间失败:" + e.getMessage(), e);
}
return null;
}
/**
* 判断一个key是否存在
*
* @param key
* @return
*/
public static boolean exists(String key) {
try {
return jedisCluster.exists(key);
} catch (Exception e) {
LOGGER.warn("jedis判断key是否存在失败:" + e.getMessage(), e);
}
return false;
}
/**
* 判断一个key是否存在
*
* @param key
* @return
*/
public static boolean existsObject(Object key) {
try {
return jedisCluster.exists(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis判断key是否存在失败:" + e.getMessage(), e);
}
return false;
}
/**
* 缓存值自增1
*
* @param key
* @return
*/
public static Long incr(String key) {
Long result = null;
try {
result = jedisCluster.incr(key);
} catch (Exception e) {
LOGGER.warn("jedis缓存值自增1失败:" + e.getMessage(), e);
}
return result;
}
/**
* 缓存值自增1
*
* @param key
* @return
*/
public static Long incrObject(Object key) {
Long result = null;
try {
result = jedisCluster.incr(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis缓存值自增1失败:" + e.getMessage(), e);
}
return result;
}
/**
* 缓存值自减1
*
* @param key
* @return
*/
public static Long decr(String key) {
Long result = null;
try {
result = jedisCluster.decr(key);
} catch (Exception e) {
LOGGER.warn("jedis缓存值自减1失败:" + e.getMessage(), e);
}
return result;
}
/**
* 缓存值自减1
*
* @param key
* @return
*/
public static Long decrObject(Object key) {
Long result = null;
try {
result = jedisCluster.decr(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis缓存值自减1失败:" + e.getMessage(), e);
}
return result;
}
@Autowired
public void setJedisCluster(JedisCluster jedisCluster) {
JedisClusterUtils.jedisCluster = jedisCluster;
}
/**
*
*/
private static Logger LOGGER = LoggerFactory.getLogger(JedisClusterUtils.class);
/**
* 默认过期时间(2小时)
*/
private static final Integer REDIS_SESSION_TIMEOUT = 2 * 60 * 60;
private static JedisCluster jedisCluster;
/**
* 保存一个字符串到redis中,长期有效
*
* @param key 键
* @param value 值
* @return
*/
public static boolean set(String key, String value) {
try {
jedisCluster.set(key, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据前缀匹配模糊删除
*
* @param fuzzyKey 模糊前缀
* @return 删除的数量
*/
public static int fuzzyDel(String fuzzyKey) {
if (StringUtils.isBlank(fuzzyKey)) {
return 0;
}
Set<String> keys = jedisCluster.hkeys(fuzzyKey);
if (CollectionUtils.isEmpty(keys)) {
return 0;
}
int num = 0;
for (String key : keys) {
if (del(key)) {
num++;
}
}
return num;
}
/**
* 保存一个字符串到redis中,长期有效
*
* @param key 键
* @param value 值
* @return
*/
public static boolean getSet(String key, String value) {
try {
jedisCluster.getSet(key, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
public static String getSetR(String key,String value){
String set;
try {
set = jedisCluster.getSet(key, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return null;
}
return set;
}
/**
* 保存一个字符串到redis中并指定过期时间
*
* @param key 键
* @param value 值
* @param seconds 有效时间,为null使用默认时间30分钟
* @return 旧值
*/
public static String getSet(String key, String value, Integer seconds) {
String set;
try {
set = jedisCluster.getSet(key, value);
expire(key, seconds);
} catch (Exception e) {
LOGGER.warn("jedis getSet缓存保存失败:" + e.getMessage(), e);
return null;
}
return set;
}
/**
* 保存一个字符串到redis中并指定过期时间
*
* @param key 键
* @param value 值
* @param seconds 有效时间,为null使用默认时间30分钟
* @return
*/
public static boolean set(String key, String value, Integer seconds) {
try {
jedisCluster.set(key, value);
expire(key, seconds);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个字符串到redis中,长期有效
*
* @param objecKey 键
* @param objecValue 缓存对象
* @return
*/
public static boolean setObject(Object key, Object value) {
try {
jedisCluster.set(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个字符串到redis中并指定过期时间
*
* @param objecKey 键
* @param objecValue 缓存对象
* @param seconds 时间,如果为null,则使用默认时间30分钟
* @return
*/
public static boolean setObject(Object key, Object value, Integer seconds) {
if (key == null || value == null) {
return false;
}
try {
jedisCluster.set(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
expireObject(key, seconds);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 当键在redis中不存在时才设置
*
* @param key
* @param value
* @return
*/
public static boolean setnx(String key, String value) {
long result = 0;
try {
result = jedisCluster.setnx(key, value);
} catch (Exception e) {
LOGGER.warn("jedis Setn缓存保存失败:" + e.getMessage(), e);
return false;
}
return result == 0 ? false : true;
}
/**
* 追加一个字符串到指定的key
*
* @param key
* @param value
* @return
*/
public static boolean append(String key, String value) {
try {
jedisCluster.append(key, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键获取Redis缓存中的值.<br/>
*
* @param key 键
* @return
*/
public static String get(String key) {
try {
return jedisCluster.get(key);
} catch (Exception e) {
LOGGER.warn("jedis获取缓存失败:" + e.getMessage(), e);
return null;
}
}
/**
* 根据缓存键获取Redis缓存中的对象.<br/>
*
* @param objecKey 键
* @return
*/
public static Object getObject(Object key) {
try {
byte[] bytes = jedisCluster.get(SerializeUtils.serialize(key));
return bytes == null ? null : SerializeUtils.unSerialize(bytes);
} catch (Exception e) {
LOGGER.warn("jedis获取缓存失败:" + e.getMessage(), e);
}
return null;
}
/**
* 根据缓存键清除Redis缓存中的对象.
*
* @param key
* @return
*/
public static boolean delObject(Object key) {
try {
jedisCluster.del(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis删除缓存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键批量清除Redis缓存中的对象.
*
* @param keys
* @return
*/
public static boolean delObject(Object... keys) {
try {
jedisCluster.del(SerializeUtils.serialize(keys));
} catch (Exception e) {
LOGGER.warn("jedis批量删除缓存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键删除Redis缓存中的值.
*
* @param key
* @return
*/
public static boolean del(String key) {
try {
jedisCluster.del(key);
} catch (Exception e) {
LOGGER.warn("jedis删除缓存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键批量清除Redis缓存中的值.
*
* @param keys
* @return
*/
public static boolean del(String... keys) {
try {
jedisCluster.del(keys);
} catch (Exception e) {
LOGGER.warn("jedis批量删除缓存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个Map对象到redis中,长期有效
*
* @param key 键
* @param hash Map组合的字符串
* @return
*/
public static boolean hmset(String key, Map<String, String> hash) {
try {
jedisCluster.hmset(key, hash);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 保存一个Map对象到redis中并指定过期时间
*
* @param key 键
* @param hash Map组合的字符串
* @param seconds 时间
* @return
*/
public static boolean hmset(String key, Map<String, String> hash, Integer seconds) {
try {
jedisCluster.hmset(key, hash);
expire(key, seconds);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 添加一个内容到指定key的hash中
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean hset(String key, String field, String value) {
try {
jedisCluster.hset(key, field, value);
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的hash失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 添加一个内容到指定key的hash中,并转换成json格式
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean hset2Json(String key, String field, Object value) {
try {
jedisCluster.hset(key, field, JSONObject.toJSONString(value));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的hash失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 添加一个Fieid内容到指定key的hash中,如果Fieid存在就不添加,反之添加
*
* @param key
* @param field
* @param value
* @return
*/
public static boolean hsetnx(String key, String field, String value) {
try {
jedisCluster.hsetnx(key, field, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 添加一个Fieid内容到指定key的hash中,如果Fieid存在就不添加,反之添加
*
* @param key
* @param field
* @param value
* @return
*/
public static boolean hsetnx2Json(String key, String field, Object value) {
try {
jedisCluster.hsetnx(key, field, JSONObject.toJSONString(value));
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 指定Map中file的值自增
*
* @param key
* @param field
* @param value
* @return
*/
public static Long hincrBy(String key, String field, Long value) {
try {
return jedisCluster.hincrBy(key, field, value);
} catch (Exception e) {
LOGGER.warn("jedis缓存保存失败:" + e.getMessage(), e);
return null;
}
}
/**
* 添加一个内容到指定key的hash中
*
* @param key
* @param field
* @param value
* @return
*/
@Deprecated
public static Boolean hsetObject(Object key, Object field, Object value) {
try {
jedisCluster.hset(SerializeUtils.serialize(key), SerializeUtils.serialize(field),
SerializeUtils.serialize(value));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的hash失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据缓存键获取Redis缓存中的Map.<br/>
*
* @param key
* @param fields 具体的map键
* @return
*/
public static Map<String, String> hgetAll(String key) {
try {
return jedisCluster.hgetAll(key);
} catch (Exception e) {
LOGGER.warn("jedis获取缓存失败:" + e.getMessage(), e);
return null;
}
}
/**
* 根据缓存键获取Redis缓存中的Map.<br/>
* 可以指定Map具体的键值获取精确数据
*
* @param key
* @param fields 具体的map键
* @return
*/
public static Map<String, String> hmget(String key, String... fields) {
Map<String, String> resultMap = Maps.newHashMap();
try {
// 获取完整的map集合
if (fields == null || fields.length == 0) {
return jedisCluster.hgetAll(key);
} else {
List<String> resultList = jedisCluster.hmget(key, fields);
if (!ListUtils.isEmpty(resultList)) {
for (int i = 0; i < resultList.size(); i++) {
resultMap.put(fields[i], resultList.get(i));
}
}
}
} catch (Exception e) {
LOGGER.warn("jedis获取缓存失败:" + e.getMessage(), e);
return null;
}
return resultMap;
}
/**
* 从指定hash中取值
*
* @param key
* @param field
* @return
*/
public static String hget(String key, String field) {
try {
return jedisCluster.hget(key, field);
} catch (Exception e) {
LOGGER.warn("jedis从指定hash中取值失败:" + e.getMessage(), e);
return null;
}
}
/**
* 从指定hash中取值,并将取到的json字符串转换成指定的对象
*
* @param key
* @param field
* @param clazz
* @return
*/
public static <T> T hgetJson2Class(String key, String field, Class<T> clazz) {
try {
return JSONObject.parseObject(jedisCluster.hget(key, field), clazz);
} catch (Exception e) {
LOGGER.warn("jedis从指定hash中取值失败:" + e.getMessage(), e);
return null;
}
}
/**
* 从指定hash中取值,并将取到的json字符串转换成指定的对象集合
*
* @param key
* @param field
* @param clazz
* @return
*/
public static <T> List<T> hgetJson2List(String key, String field, Class<T> clazz) {
try {
return JSONArray.parseArray(jedisCluster.hget(key, field), clazz);
} catch (Exception e) {
LOGGER.warn("jedis从指定hash中取值失败:" + e.getMessage(), e);
return null;
}
}
/**
* 从指定hash中取值
*
* @param key
* @param field
* @return
*/
@Deprecated
public static Object hgetObject(Object key, Object field) {
try {
byte[] bytes = jedisCluster.hget(SerializeUtils.serialize(key), SerializeUtils.serialize(field));
return bytes == null ? null : SerializeUtils.unSerialize(bytes);
} catch (Exception e) {
LOGGER.warn("jedis从指定hash中取对象失败:" + e.getMessage(), e);
}
return null;
}
/**
* 根据缓存键删除map中的指定键值
*
* @param key 缓存键
* @param field map键值
* @return 删除成功返回1,失败返回0
*/
public static Boolean hdel(String key, String... fields) {
long result = 0L;
try {
result = jedisCluster.hdel(key, fields);
} catch (Exception e) {
LOGGER.warn("jedis删除map中的指定键值失败:" + e.getMessage(), e);
}
return result > 0 ? true : false;
}
/**
* 从hash中删除指定filed的对象
*
* @param key
* @param field
* @return
*/
@Deprecated
public static Boolean hdelObject(Object key, Object... field) {
long result = 0L;
try {
result = jedisCluster.hdel(SerializeUtils.serialize(key), SerializeUtils.serialize(field));
} catch (Exception e) {
LOGGER.warn("jedis从hash中删除指定filed的对象失败:" + e.getMessage(), e);
}
return result > 0 ? true : false;
}
/**
* 获取指定key中存放的hash值个数
*
* @param key
* @return
*/
public static Long hlen(String key) {
try {
return jedisCluster.hlen(key);
} catch (Exception e) {
LOGGER.warn("jedis获取指定key中存放的hash值个数失败:" + e.getMessage(), e);
}
return null;
}
/**
* 判断指定key中是否存在fileId键
*
* @param key
* @return
*/
public static Boolean hexists(String key, String fileId) {
try {
return jedisCluster.hexists(key, fileId);
} catch (Exception e) {
LOGGER.warn("jedis判断指定key中是否存在fileId键失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key中存放的hash值个数
*
* @param key
* @return
*/
public static Long hlenObject(Object key) {
try {
return jedisCluster.hlen(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis获取指定key中存放的hash值个数失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key中map对象中的所有key
*
* @param key
* @return
*/
public static Set<String> hkeys(String key) {
try {
return jedisCluster.hkeys(key);
} catch (Exception e) {
LOGGER.warn("jedis获取指定key中map对象中的所有key失败:" + e.getMessage(), e);
}
return null;
}
/**
* 添加内容到指定key的list中,先进后出
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean lpush(String key, String value) {
Long result = 0L;
try {
result = jedisCluster.lpush(key, value);
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进后出
*
* @param key
* @param obj
* @return
*/
public static Boolean lpushJson(String key, Object obj) {
Long result = 0L;
try {
result = jedisCluster.lpush(key, JSONObject.toJSONString(obj));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进后出
*
* @param key
* @param value
* @return
*/
public static Boolean lpushObject(Object key, Object value) {
Long result = 0L;
try {
result = jedisCluster.lpush(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进先出
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean rpush(String key, String value) {
Long result = 0L;
try {
result = jedisCluster.rpush(key, value);
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进先出
*
* @param key
* @param obj
* @return
*/
public static Boolean rpushJson(String key, Object obj) {
Long result = 0L;
try {
result = jedisCluster.rpush(key, JSONObject.toJSONString(obj));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 添加内容到指定key的list中,先进先出
*
* @param key
* @param field
* @param value
* @return
*/
public static Boolean rpushObject(Object key, Object value) {
Long result = 0L;
try {
result = jedisCluster.rpush(SerializeUtils.serialize(key), SerializeUtils.serialize(value));
} catch (Exception e) {
LOGGER.warn("jedis添加内容到指定key的list中失败:" + e.getMessage(), e);
}
return result == 0 ? false : true;
}
/**
* 获取指定key的list集合
*
* @param key
* @param start list中的起始位置,从0开始
* @param len 长度,-1获取所有
* @return
*/
public static List<String> lrange(String key, long start, long len) {
try {
return jedisCluster.lrange(key, start, len);
} catch (Exception e) {
LOGGER.warn("jedis获取指定key的list集合失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的list长度
*
* @param key
* @return
*/
public static Long llen(String key) {
try {
return jedisCluster.llen(key);
} catch (Exception e) {
LOGGER.warn("jedis获取指定key的list长度失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的list长度
*
* @param key
* @return
*/
public static Long llenObject(Object key) {
try {
return jedisCluster.llen(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis获取指定key的list长度失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(左端)
*
* @param key
* @return
*/
public static String lpop(String key) {
try {
return jedisCluster.lpop(key);
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(左端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(左端)
*
* @param key
* @return
*/
public static List<String> lpop(String key, long count) {
try {
List<String> list = new ArrayList<String>();
for (int i = 0; i < count; i++) {
String vel = jedisCluster.lpop(key);
if (vel != null) {
list.add(vel);
}
}
return list;
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(左端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(左端)
*
* @param key
* @return
*/
public static <T> T lpopJson(String key, Class<T> clazz) {
try {
String vel = jedisCluster.lpop(key);
if (!StringUtil.isEmpty(vel)) {
return JSONObject.parseObject(vel, clazz);
}
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(左端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(左端)
*
* @param key
* @return
*/
public static <T> List<T> lpopJson(String key, long count, Class<T> clazz) {
try {
List<T> list = new ArrayList<T>();
for (int i = 0; i < count; i++) {
String vel = jedisCluster.lpop(key);
if (!StringUtil.isEmpty(vel)) {
list.add(JSONObject.parseObject(vel, clazz));
}
}
return list;
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(左端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(右端)
*
* @param key
* @return
*/
public static String rpop(String key) {
try {
return jedisCluster.rpop(key);
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(右端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(右端)
*
* @param key
* @return
*/
public static List<String> rpop(String key, long count) {
try {
List<String> list = new ArrayList<String>();
for (int i = 0; i < count; i++) {
String vel = jedisCluster.rpop(key);
if (vel != null) {
list.add(vel);
}
}
return list;
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(右端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(右端)
*
* @param key
* @return
*/
public static <T> T rpopJson(String key, Class<T> clazz) {
try {
String vel = jedisCluster.rpop(key);
if (!StringUtil.isEmpty(vel)) {
return JSONObject.parseObject(vel, clazz);
}
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(右端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 列表出栈(右端)
*
* @param key
* @return
*/
public static <T> List<T> rpopJson(String key, long count, Class<T> clazz) {
try {
List<T> list = new ArrayList<T>();
for (int i = 0; i < count; i++) {
String vel = jedisCluster.rpop(key);
if (!StringUtil.isEmpty(vel)) {
list.add(JSONObject.parseObject(vel, clazz));
}
}
return list;
} catch (Exception e) {
LOGGER.warn("jedis列表出栈(右端)失败:" + e.getMessage(), e);
}
return null;
}
/**
* 删除指定区间之外的元素
*
* @param key
* @return
*/
public static String ltrim(String key, long start, long len) {
try {
return jedisCluster.ltrim(key, start, len);
} catch (Exception e) {
LOGGER.warn("jedis删除指定区间之外的元素失败:" + e.getMessage(), e);
}
return null;
}
/**
* 有序序列(Sort-Set)集合:添加数据
*
* @param key
* @return
*/
public static boolean zset(String key, double score, String member) {
try {
jedisCluster.zadd(key, score, member);
} catch (Exception e) {
LOGGER.error("jedis有序序列集合:添加数据失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 获取指定key的Sort-Set集合长度
*
* @return
*/
public static Long zcard(String key) {
try {
return jedisCluster.zcard(key);
} catch (Exception e) {
LOGGER.error("jedis有序序列集合长度失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的Sort-Set集合中member元素所在的位置排行(从 0 开始)
*
* @return
*/
public static Long zrank(String key, String member) {
try {
return jedisCluster.zrank(key, member);
} catch (Exception e) {
LOGGER.error("jedis获取有序序列集合中MEMBER位置失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的Sort-Set集合的元素(按照score从小到大排序)
*
* @return
*/
public static Set<String> zrange(String key, long start, long end) {
try {
return jedisCluster.zrange(key, start, end);
} catch (Exception e) {
LOGGER.error("jedis有序序列集合的元素失败:" + e.getMessage(), e);
}
return null;
}
/**
* 获取指定key的Sort-Set集合的元素(按照score从大到小排序)
*
* @return
*/
public static Set<String> zrevrange(String key, long start, long end) {
try {
return jedisCluster.zrevrange(key, start, end);
} catch (Exception e) {
LOGGER.error("jedis有序序列集合的元素失败:" + e.getMessage(), e);
}
return null;
}
/**
* 删除指定key的Sort-Set集合的元素
*
* @return
*/
public static boolean zrem(String key, long start, long end) {
try {
jedisCluster.zremrangeByRank(key, start, end);
} catch (Exception e) {
LOGGER.error("jedis删除有序序列集合的元素失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 根据clazz和key获取对应 的类型
*
* @param key
* @param clazz
* @return
*/
public static <T> T getClass(Object key, Class<T> clazz) {
try {
Object object = getObject(key);
return object == null ? null : clazz.cast(object);
} catch (Exception e) {
LOGGER.warn("jedis根据clazz和key获取对应 的类型:" + e.getMessage(), e);
}
return null;
}
/**
* 根据clazz和key获取对应的list
*
* @param key
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <T> List<T> getList(Object key, Class<T> clazz) {
try {
Object object = getObject(key);
return object == null ? null : (List<T>) object;
} catch (Exception e) {
LOGGER.warn("jedis根据clazz和key获取对应的list:" + e.getMessage(), e);
}
return null;
}
/**
* 根据clazz和key获取对应的map
*
* @param key
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> getMap(Object key, Class<K> kClazz, Class<V> vClazz) {
try {
Object object = getObject(key);
return object == null ? null : (Map<K, V>) object;
} catch (Exception e) {
LOGGER.warn("jedis根据clazz和key获取对应的map:" + e.getMessage(), e);
}
return null;
}
/**
* 根据key,及clazz获取对象.使用json进行转换
*
* @param key
* @param clazz
* @return
*/
public static <T> T getJson(String key, Class<T> clazz) {
String jsonValue = get(key);
if (StringUtil.isEmpty(jsonValue)) {
return null;
}
try {
return JSONObject.parseObject(jsonValue, clazz);
} catch (Exception e) {
LOGGER.warn("jedis根据key获取clazz对象,JSON转换异常:" + e.getMessage(), e);
}
return null;
}
/**
* 根据key,及clazz获取对象集合.使用json进行转换
*
* @param key
* @param clazz
* @return
*/
public static <T> List<T> getJsonList(String key, Class<T> clazz) {
String jsonValue = get(key);
if (StringUtil.isEmpty(jsonValue)) {
return null;
}
try {
return JSONArray.parseArray(jsonValue, clazz);
} catch (Exception e) {
LOGGER.warn("jedis根据key获取clazz对象,JSON转换异常:" + e.getMessage(), e);
}
return null;
}
/**
* 缓存对象.使用json进行转换
*
* @param key
* @param obj
*/
public static Boolean setJson(String key, Object obj) {
try {
return set(key, JSONObject.toJSONString(obj));
} catch (Exception e) {
LOGGER.warn("jedis根据key缓存对象,JSON转换异常:" + e.getMessage(), e);
}
return false;
}
/**
* 缓存对象.使用json进行转换,带有效期
*
* @param key
* @param obj
* @param seconds 有效期(秒)
* @return
*/
public static Boolean setJson(String key, Object obj, Integer seconds) {
try {
return set(key, JSONObject.toJSONString(obj), seconds);
} catch (Exception e) {
LOGGER.warn("jedis根据key缓存对象,JSON转换异常:" + e.getMessage(), e);
}
return false;
}
/**
* 缓存对象集合.使用json进行转换
*
* @param key
* @param obj
* @return
*/
public static Boolean setJsonList(String key, Object obj) {
try {
return set(key, JSONArray.toJSONString(obj));
} catch (Exception e) {
LOGGER.warn("jedis根据key缓存对象,JSON转换异常:" + e.getMessage(), e);
}
return false;
}
/**
* 缓存对象集合.使用json进行转换
*
* @param key
* @param obj
* @return
*/
public static Boolean setJsonList(String key, Object obj, Integer seconds) {
try {
return set(key, JSONArray.toJSONString(obj), seconds);
} catch (Exception e) {
LOGGER.warn("jedis根据key缓存对象,JSON转换异常:" + e.getMessage(), e);
}
return false;
}
/**
* 设置超时时间
*
* @param key
* @param seconds
* @return
*/
public static boolean expire(String key, Integer seconds) {
try {
jedisCluster.expire(key, seconds == null || seconds < 1 ? REDIS_SESSION_TIMEOUT : seconds);
} catch (Exception e) {
LOGGER.warn("jedis设置超时时间失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 设置超时时间
*
* @param key
* @param seconds 超时时间(单位为秒)
* @return
*/
public static boolean expireObject(Object key, Integer seconds) {
try {
jedisCluster.expire(SerializeUtils.serialize(key),
seconds == null || seconds < 1 ? REDIS_SESSION_TIMEOUT : seconds);
} catch (Exception e) {
LOGGER.warn("jedis设置超时时间失败:" + e.getMessage(), e);
return false;
}
return true;
}
/**
* 查看键还有多长时间过期(秒)
*
* @param key
* @return
*/
public static Long ttl(String key) {
try {
return jedisCluster.ttl(key);
} catch (Exception e) {
LOGGER.warn("jedis查看过期时间失败:" + e.getMessage(), e);
}
return null;
}
/**
* 查看键还有多长时间过期(秒)
*
* @param key
* @return
*/
public static Long ttlObject(Object key) {
try {
return jedisCluster.ttl(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis查看过期时间失败:" + e.getMessage(), e);
}
return null;
}
/**
* 判断一个key是否存在
*
* @param key
* @return
*/
public static boolean exists(String key) {
try {
return jedisCluster.exists(key);
} catch (Exception e) {
LOGGER.warn("jedis判断key是否存在失败:" + e.getMessage(), e);
}
return false;
}
/**
* 判断一个key是否存在
*
* @param key
* @return
*/
public static boolean existsObject(Object key) {
try {
return jedisCluster.exists(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis判断key是否存在失败:" + e.getMessage(), e);
}
return false;
}
/**
* 缓存值自增1
*
* @param key
* @return
*/
public static Long incr(String key) {
Long result = null;
try {
result = jedisCluster.incr(key);
} catch (Exception e) {
LOGGER.warn("jedis缓存值自增1失败:" + e.getMessage(), e);
}
return result;
}
/**
* 缓存值自增1
*
* @param key
* @return
*/
public static Long incrObject(Object key) {
Long result = null;
try {
result = jedisCluster.incr(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis缓存值自增1失败:" + e.getMessage(), e);
}
return result;
}
/**
* 缓存值自减1
*
* @param key
* @return
*/
public static Long decr(String key) {
Long result = null;
try {
result = jedisCluster.decr(key);
} catch (Exception e) {
LOGGER.warn("jedis缓存值自减1失败:" + e.getMessage(), e);
}
return result;
}
/**
* 缓存值自减1
*
* @param key
* @return
*/
public static Long decrObject(Object key) {
Long result = null;
try {
result = jedisCluster.decr(SerializeUtils.serialize(key));
} catch (Exception e) {
LOGGER.warn("jedis缓存值自减1失败:" + e.getMessage(), e);
}
return result;
}
@Autowired
public void setJedisCluster(JedisCluster jedisCluster) {
JedisClusterUtils.jedisCluster = jedisCluster;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment