package com;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* @param key 键
* @param time 时间(秒)
* @return boolean
* @description: 指定缓存失效时间
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
log.error("class:RedisUtils method:expire() => Exception {}", e.getMessage());
}
return false;
}
/**
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
* @description: 根据key 获取过期时间
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* @param key 键
* @return true 存在 false不存在
* @description: 判断key是否存在
*/
public boolean exists(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
log.error("class:RedisUtils method:exists() => Exception {}", e.getMessage());
}
return false;
}
/**
* @param key 可以传一个值 或多个
* @description: 删除缓存
*/
@SuppressWarnings("unchecked")
public void deleteCache(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
public <T> T get(String key, Class<T> clazz, long expire) {
Object value = redisTemplate.opsForValue().get(key);
if (expire != -1L) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : fromJson(value, clazz);
}
public <T> T get(String key, Class<T> clazz) {
return get(key, clazz, -1L);
}
/**
* @param key 键
* @return Object value
* @description: 根据key获取value
*/
public Object getValueByKey(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* @param key 键
* @param value 值
* @param expire 时间(秒) time要大于0 如果expire小于等于0 将设置无限期
* @return true成功 false失败
* @description: 普通缓存放入
*/
public void set(String key, Object value, long expire) {
redisTemplate.opsForValue().set(key, value);
if (expire != -1L) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @return true成功 false 失败
*/
public void set(String key, Object value) {
set(key, value, 3600L);
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public long increasing(String key, long delta) {
if (delta < 0) {
log.error("class:RedisUtils method:increasing() => delta:{} delta must more than 0", delta);
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
log.error("class:RedisUtils method:decr() => delta:{} delta must more than 0", delta);
}
return redisTemplate.opsForValue().increment(key, -delta);
}
//================================Map=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
log.error("class:RedisUtils method:hmset() => Exception {}", e.getMessage());
}
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(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error("class:RedisUtils method:hmset() => Exception {}", e.getMessage());
}
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(key, item, value);
return true;
} catch (Exception e) {
log.error("class:RedisUtils method:hset() => Exception {}", e.getMessage());
}
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(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
log.error("class:RedisUtils method:hset() => Exception {}", e.getMessage());
}
return false;
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public void deleteValueByKey(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一�
redisTemplate封装成redisUtils和分布式锁实现
需积分: 5 126 浏览量
2021-06-18
14:32:13
上传
评论 2
收藏 5KB ZIP 举报
著名特乐
- 粉丝: 12
- 资源: 24
最新资源
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
- 软件测试题详解及答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0