add 过期 RedisCache 新增 RedisUtils 工具类 更灵巧便于使用

master
疯狂的狮子li 4 years ago
parent c5d46591fe
commit 97cffa048e

@ -16,9 +16,12 @@ import java.util.function.Consumer;
* spring redis
*
* @author shenxinquan
* @see com.ruoyi.common.utils.RedisUtils
* @deprecated 3.2.0
**/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
@Deprecated
public class RedisCache {
@Autowired

@ -0,0 +1,258 @@
package com.ruoyi.common.utils;
import com.google.common.collect.Lists;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.redisson.api.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* redis
*
* @author Lion Li
* @version 3.1.0
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings(value = {"unchecked", "rawtypes"})
public class RedisUtils {
private static RedissonClient client = SpringUtils.getBean(RedissonClient.class);
/**
*
*
* @param channelKey key
* @param msg
* @param consumer
*/
public static <T> void publish(String channelKey, T msg, Consumer<T> consumer) {
RTopic topic = client.getTopic(channelKey);
topic.publish(msg);
consumer.accept(msg);
}
public static <T> void publish(String channelKey, T msg) {
RTopic topic = client.getTopic(channelKey);
topic.publish(msg);
}
/**
*
*
* @param channelKey key
* @param clazz
* @param consumer
*/
public static <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) {
RTopic topic = client.getTopic(channelKey);
topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
}
/**
* IntegerString
*
* @param key
* @param value
*/
public static <T> void setCacheObject(final String key, final T value) {
client.getBucket(key).set(value);
}
/**
* IntegerString
*
* @param key
* @param value
* @param timeout
* @param timeUnit
*/
public static <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
RBucket<T> result = client.getBucket(key);
result.set(value);
result.expire(timeout, timeUnit);
}
/**
*
*
* @param key Redis
* @param timeout
* @return true=false=
*/
public static boolean expire(final String key, final long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
}
/**
*
*
* @param key Redis
* @param timeout
* @param unit
* @return true=false=
*/
public static boolean expire(final String key, final long timeout, final TimeUnit unit) {
RBucket rBucket = client.getBucket(key);
return rBucket.expire(timeout, unit);
}
/**
*
*
* @param key
* @return
*/
public static <T> T getCacheObject(final String key) {
RBucket<T> rBucket = client.getBucket(key);
return rBucket.get();
}
/**
*
*
* @param key
*/
public static boolean deleteObject(final String key) {
return client.getBucket(key).delete();
}
/* */
/**
*
*
* @param collection
* @return
*/
public static void deleteObject(final Collection collection) {
RBatch batch = client.createBatch();
collection.forEach(t -> {
batch.getBucket(t.toString()).deleteAsync();
});
batch.execute();
}
/**
* List
*
* @param key
* @param dataList List
* @return
*/
public static <T> boolean setCacheList(final String key, final List<T> dataList) {
RList<T> rList = client.getList(key);
return rList.addAll(dataList);
}
/**
* list
*
* @param key
* @return
*/
public static <T> List<T> getCacheList(final String key) {
RList<T> rList = client.getList(key);
return rList.readAll();
}
/**
* Set
*
* @param key
* @param dataSet
* @return
*/
public static <T> boolean setCacheSet(final String key, final Set<T> dataSet) {
RSet<T> rSet = client.getSet(key);
return rSet.addAll(dataSet);
}
/**
* set
*
* @param key
* @return
*/
public static <T> Set<T> getCacheSet(final String key) {
RSet<T> rSet = client.getSet(key);
return rSet.readAll();
}
/**
* Map
*
* @param key
* @param dataMap
*/
public static <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
if (dataMap != null) {
RMap<String, T> rMap = client.getMap(key);
rMap.putAll(dataMap);
}
}
/**
* Map
*
* @param key
* @return
*/
public static <T> Map<String, T> getCacheMap(final String key) {
RMap<String, T> rMap = client.getMap(key);
return rMap.getAll(rMap.keySet());
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @param value
*/
public static <T> void setCacheMapValue(final String key, final String hKey, final T value) {
RMap<String, T> rMap = client.getMap(key);
rMap.put(hKey, value);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @return Hash
*/
public static <T> T getCacheMapValue(final String key, final String hKey) {
RMap<String, T> rMap = client.getMap(key);
return rMap.get(hKey);
}
/**
* Hash
*
* @param key Redis
* @param hKeys Hash
* @return Hash
*/
public static <K, V> Map<K, V> getMultiCacheMapValue(final String key, final Set<K> hKeys) {
RMap<K, V> rMap = client.getMap(key);
return rMap.getAll(hKeys);
}
/**
*
*
* @param pattern
* @return
*/
public static Collection<String> keys(final String pattern) {
Iterable<String> iterable = client.getKeys().getKeysByPattern(pattern);
return Lists.newArrayList(iterable);
}
}
Loading…
Cancel
Save