123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- package com.kcim.common.token;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.HashOperations;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.support.atomic.RedisAtomicLong;
- import org.springframework.stereotype.Component;
- import java.time.Duration;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- /**
- * Redis的工具类文件, 注意结果可能不是你想要的,建议使用string
- */
- @Component
- public class RedisUtil {
- @Autowired
- private RedisTemplate redisTemplate;
- @Autowired
- private HashOperations hashOperations;
- /**
- * 指定缓存失效时间
- *
- * @param key 键
- * @param time 时间(秒)
- * @return
- */
- public boolean expire(String key, long time) {
- try {
- if (time > 0) {
- redisTemplate.expire(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(key, TimeUnit.SECONDS);
- }
- /**
- * 判断key是否存在
- *
- * @param key 键
- * @return true 存在 false不存在
- */
- public boolean hasKey(String key) {
- try {
- return redisTemplate.hasKey(key);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 普通缓存获取
- *
- * @param key 键
- * @return 值
- */
- public Object get(String key) {
- return key == null ? null : redisTemplate.opsForValue().get(key);
- }
- /**
- * 删除缓存
- *
- * @param key 可以传一个值
- */
- @SuppressWarnings("unchecked")
- public void del(Object key) {
- redisTemplate.delete(key);
- }
- /**
- * 普通缓存放入
- *
- * @param key 键
- * @param value 值
- * @return true成功 false失败
- */
- public boolean set(String key, Object value) {
- try {
- redisTemplate.opsForValue().set(key, value);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 普通缓存放入并设置时间
- *
- * @param key
- * @param value
- * @param duration 过期时间,灵活选择
- * @return
- */
- public boolean set(String key, Object value, Duration duration) {
- try {
- redisTemplate.opsForValue().set(key, value, duration);
- 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(key, value, time, TimeUnit.SECONDS);
- } else {
- set(key, value);
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * Push到 队列List
- *
- * @param key
- * @param value
- * @param
- */
- public void leftPush(String key, Object value) {
- redisTemplate.opsForList().leftPush(key, value);
- }
- /**
- * Push到 队列List
- *
- * @param key
- * @param value
- * @param
- */
- public void leftPush(String key, Object value, long expireTime) {
- redisTemplate.opsForList().leftPush(key, value);
- expire(key, expireTime);
- }
- /**
- * 从队列List中Pop出来对象
- *
- * @param key
- * @return
- */
- public Object rightPop(String key) {
- return redisTemplate.opsForList().rightPop(key);
- }
- /**
- * 从队列List中Pop出来对象
- *
- * @param key
- * @param t
- * @param <T>
- * @return
- */
- public <T> T rightPop(String key, Class<T> t) {
- return (T) redisTemplate.opsForList().rightPop(key);
- }
- /**
- * 对缓存在key中的值做减1操作
- *
- * @param key
- */
- public Long decrement(String key) {
- return redisTemplate.opsForValue().increment(key, -1);
- }
- /**
- * 不存在时设置, 返回true为设置成功
- *
- * @param key
- * @param value
- */
- public boolean setIfAbsent(Object key, Object value) {
- return redisTemplate.opsForValue().setIfAbsent(key, value);
- }
- /**
- * 不存在时设置,同时设置过期时间 返回true为设置成功
- *
- * @param key
- * @param value
- * @param timeout
- * @return
- */
- public boolean setIfAbsent(String key, Object value, Duration timeout) {
- return redisTemplate.opsForValue().setIfAbsent(key, value, timeout);
- }
- public long incrementAndGet(String key) {
- RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
- return entityIdCounter.incrementAndGet();
- }
- /**
- * 存hash值
- *
- * @param key
- * @param hKey
- * @param hValue
- */
- public void hPut(Object key, Object hKey, Object hValue) {
- hashOperations.put(key, hKey, hValue);
- }
- /**
- * 存hash值
- *
- * @param key
- * @param map
- */
- public void hPutAll(Object key, Map map) {
- hashOperations.putAll(key, map);
- }
- /**
- * 查hash值
- *
- * @param key
- * @return
- */
- public Map hEntries(Object key) {
- return hashOperations.entries(key);
- }
- /**
- * 查hash值
- *
- * @param key
- * @param hKey
- * @return
- */
- public Object hGet(Object key, Object hKey) {
- return hashOperations.get(key, hKey);
- }
- }
|