1.转换成json字符串 ,再压缩字符串
redisUtil.set(createdBy + "orderDatas", RedisUtil.compress(JsonUtil.beanToJson(dataList)), 3600);
2.解压字符串,然后转换成对象
JsonUtil.jsonToList(RedisUtil.uncompress((String) redisUtil.get(userName + "_orderFail")),xxx.class);
有日期,还需要转换格式,不然会报错
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = jp.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
在需要转换日期的属性加上
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
private Date createdDate;
下面是需要的工具类
JsonUtil
import com.ccp.common.exception.BusinessException;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.map.annotate.*;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.*;
import org.codehaus.jackson.type.JavaType;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
public class JsonUtil {
private JsonUtil() {
}
private static class UtilHolder {
private static ObjectMapper instance = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
}
/**
* 获取ObjectMapper实例
*
* @return
*/
public static ObjectMapper getMapperInstance() {
ObjectMapper mapper = UtilHolder.instance;
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}
/**
* 将java对象转换成json字符串
*
* @param obj
* 准备转换的对象
* @return json字符串
* @throws Exception
*/
public static String beanToJson2(Object obj){
try {
//ObjectMapper objectMapper = getMapperInstance();
ObjectMapper objectMapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException,
JsonProcessingException {
gen.writeString("");
}
});
return objectMapper.writeValueAsString(obj);
} catch (JsonGenerationException e) {
throw new BusinessException("JsonParseException",e);
} catch (JsonMappingException e) {
throw new BusinessException("JsonMappingException",e);
} catch (IOException e) {
throw new BusinessException("IOException",e);
}catch (Exception e) {
throw new BusinessException("Exception",e);
}
}
/**
*
* @param obj
* @return
*/
public static String beanToJson(Object obj){
try {
ObjectMapper objectMapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.NONE);
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return objectMapper.writeValueAsString(obj);
} catch (JsonGenerationException e) {
throw new BusinessException("JsonParseException",e);
} catch (JsonMappingException e) {
throw new BusinessException("JsonMappingException",e);
} catch (IOException e) {
throw new BusinessException("IOException",e);
}catch (Exception e) {
throw new BusinessException("Exception",e);
}
}
/**
* 将json字符串转换成java对象
*
* @param json
* 准备转换的json字符串
* @param cls
* 准备转换的类
* @return
* @throws Exception
*/
public static <T> T jsonToBean(String json, Class<T> cls) {
try {
ObjectMapper objectMapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return objectMapper.readValue(json, cls);
} catch (JsonParseException e) {
throw new BusinessException("JsonParseException",e);
} catch (JsonMappingException e) {
throw new BusinessException("JsonMappingException",e);
} catch (IOException e) {
throw new BusinessException("IOException",e);
}catch (Exception e) {
throw new BusinessException("Exception",e);
}
}
/**
* 将json字符串转换成java对象
*
* @param json 准备转换的json字符串
* @param cls 准备转换的类
* @return T
*/
public static List jsonToList(String json, Class cls) {
try {
ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, cls);
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List list=mapper.readValue(json, javaType);
return list;
} catch (JsonParseException e) {
throw new BusinessException("JsonParseException",e);
} catch (JsonMappingException e) {
throw new BusinessException("JsonMappingException",e);
} catch (IOException e) {
throw new BusinessException("IOException",e);
}catch (NullPointerException e) {
throw new BusinessException("NullPointerException",e);
}catch (Exception e) {
throw new BusinessException("Exception",e);
}
}
}
RedisUtil
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* Redis工具类
*/
@Component
public final class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
// =============================common============================
public static String appName;
/**
* 微信token
*/
private static String appNameToken;
@Value("${redis_prefix}")
public void setAppName(String redis_prefix) {
RedisUtil.appName = redis_prefix;
}
@Value("${redis_token}")
public void setAppNameToken(String redis_prefix) {
RedisUtil.appNameToken = redis_prefix;
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(appName + key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(appName + key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(appName + key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param keyArr 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... keyArr) {
if (keyArr != null && keyArr.length > 0) {
for (String key : keyArr) {
redisTemplate.delete(appName + key);
}
// if (keyArr.length == 1) {
// redisTemplate.delete(keyArr[0]);
// } else {
// redisTemplate.delete(CollectionUtils.arrayToList(keyArr));
// }
}
}
/**
* 删除缓存
*
* @param keyArr 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void delNoKey(String... keyArr) {
if (keyArr != null && keyArr.length > 0) {
for (String key : keyArr) {
redisTemplate.delete(key);
}
}
}
// ============================String=============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(appName + key);
}
public String getString(String key) {
Object result = key == null ? null : redisTemplate.opsForValue().get(appName + key);
return result != null ? (String) result : null;
}
public String getStringToken(String key) {
Object result = key == null ? null : redisTemplate.opsForValue().get(appNameToken + key);
return result != null ? (String) result : null;
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(appName + key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(appName + key, value, time, TimeUnit.SECONDS);
} else {
set(appName + key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean setToken(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(appNameToken + key, value, time, TimeUnit.SECONDS);
} else {
set(appNameToken + key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(appName + key, delta);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(appName + key, -delta);
}
// ================================Map=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(appName + key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(appName + key);
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(appName + key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset4Str(String key, Map<String, String> map) {
try {
redisTemplate.opsForHash().putAll(appName + key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(appName + key, map);
if (time > 0) {
expire(appName + key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(appName + key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(appName + key, item, value);
if (time > 0) {
expire(appName + key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(appName + key, item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(appName + key, item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(appName + key, item, by);
}
/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return
*/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(appName + key, item, -by);
}
// ============================set=============================
/**
* 根据key获取Set中的所有值
*
* @param key 键
* @return
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(appName + key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(appName + key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将数据放入set缓存
*
* @param key 键
* @param values 值 可以是多个
* @return 成功个数
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(appName + key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 将set数据放入缓存
*
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(appName + key, values);
if (time > 0) {
expire(appName + key, time);
}
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取set缓存的长度
*
* @param key 键
* @return
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(appName + key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值为value的
*
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
*/
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(appName + key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ===============================list=================================
/**
* 获取list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(appName + key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取list缓存的长度
*
* @param key 键
* @return
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(appName + key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通过索引 获取list中的值
*
* @param key 键
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(appName + key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(appName + key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(appName + key, value);
if (time > 0) {
expire(appName + key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @return
*/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(appName + key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(appName + key, value);
if (time > 0) {
expire(appName + key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据索引修改list中的某条数据
*
* @param key 键
* @param index 索引
* @param value 值
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(appName + key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N个值为value
*
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(appName + key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public List<String> getKeysByPre(String preKey){
try {
Set<String> keys = redisTemplate.keys(preKey);
List<String> returnList = new ArrayList<>(keys);
return returnList;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 使用gzip压缩字符串
*
* @param originString 要压缩的字符串
* @return 压缩后的字符串
*/
public static String compress(String originString) {
if (originString == null || originString.length() == 0) {
return originString;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (
GZIPOutputStream gzip = new GZIPOutputStream(out);
) {
gzip.write(originString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
return new sun.misc.BASE64Encoder().encode(out.toByteArray());
}
/**
* 使用gzip解压缩
*
* @param compressedString 压缩字符串
* @return
*/
public static String uncompress(String compressedString) {
if (compressedString == null || compressedString.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] compressedByte = new byte[0];
try {
compressedByte = new sun.misc.BASE64Decoder().decodeBuffer(compressedString);
} catch (IOException e) {
e.printStackTrace();
}
String originString = null;
try (
ByteArrayInputStream in = new ByteArrayInputStream(compressedByte);
GZIPInputStream ginzip = new GZIPInputStream(in);
) {
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
originString = out.toString();
} catch (IOException e) {
e.printStackTrace();
}
return originString;
}
}