driver-server/src/main/java/com/jwl/driver/server/redis/RedisCache.java

298 lines
7.7 KiB
Java
Raw Normal View History

2023-08-12 01:56:25 +08:00
package com.jwl.driver.server.redis;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
/**
* spring redis
*
* @author ruoyi
**/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
@Slf4j
public class RedisCache {
2023-08-15 01:37:50 +08:00
2023-08-12 01:56:25 +08:00
@Autowired
public RedisTemplate redisTemplate;
@Autowired
private RedisTemplate<String, String> stringRedisTemplate;
public boolean hasKey(final String key) {
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
}
/**
* IntegerString
*
* @param key
* @param value
*/
public <T> void setCacheObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* IntegerString
*
* @param key
* @param value
* @param timeout
* @param timeUnit
*/
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* IntegerString
*
* @param key
* @param value
* @param timeout
*/
public <T> void setCacheObject(final String key, final T value, final long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/**
*
*
* @param key Redis
* @param timeout
* @return true=false=
*/
public 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 boolean expire(final String key, final long timeout, final TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
/**
*
*
* @param key
* @return
*/
public <T> T getCacheObject(final String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
*
*
* @param key
*/
public boolean deleteObject(final String key) {
return redisTemplate.delete(key);
}
/**
*
*
* @param collection
* @return
*/
public long deleteObject(final Collection collection) {
return redisTemplate.delete(collection);
}
/**
*
*
* @param key
*/
public void deleteObjectLike(final String key) {
Set<String> keys = redisTemplate.keys(key + "*");
redisTemplate.delete(keys);
}
/**
* List
*
* @param key
* @param dataList List
* @return
*/
public <T> long setCacheList(final String key, final List<T> dataList) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* list
*
* @param key
* @return
*/
public <T> List<T> getCacheList(final String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
/**
* Set
*
* @param key
* @param dataSet
* @return
*/
public BoundSetOperations<String, String> setCacheSet(final String key, final Set<String> dataSet) {
BoundSetOperations<String, String> setOperation = stringRedisTemplate.boundSetOps(key);
Iterator<String> it = dataSet.iterator();
while (it.hasNext()) {
setOperation.add(it.next());
}
return setOperation;
}
/**
* set
*
* @param key
* @return
*/
public Set<String> getCacheSet(final String key) {
return stringRedisTemplate.opsForSet().members(key);
}
public Long addCacheSet(final String key, String... value) {
SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
return setOperations.add(key, value);
}
public Long removeCacheSet(final String key, Object... value) {
return stringRedisTemplate.opsForSet().remove(key, value);
}
/**
* Map
*
* @param key
* @param dataMap
*/
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* Map
*
* @param key
* @return
*/
public <T> Map<String, T> getCacheMap(final String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @param value
*/
public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @return Hash
*/
public <T> T getCacheMapValue(final String key, final String hKey) {
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/**
* Hash
*
* @param key
* @param hkey
*/
public void delCacheMapValue(final String key, final String hkey) {
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.delete(key, hkey);
}
/**
* Hash
*
* @param key Redis
* @param hKeys Hash
* @return Hash
*/
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/**
*
*
* @param pattern
* @return
*/
public Collection<String> keys(final String pattern) {
return redisTemplate.keys(pattern);
}
/**
* key
*
* @param key
* @return
*/
public Long getExpire(final String key) {
return redisTemplate.getExpire(key);
}
/**
* value
*/
public Long addKeyValue(String key, Long addValue) {
if (addValue == null || addValue == 0) {
addValue = 1L;
}
return redisTemplate.getConnectionFactory().getConnection().incrBy(
redisTemplate.getKeySerializer().serialize(key), addValue
);
}
/**
* value
*/
public Long increment(final String key, final Integer value, final Long timeout, final TimeUnit timeUnit) {
Long count = redisTemplate.opsForValue().increment(key, value);
redisTemplate.expire(key, timeout, timeUnit);
return count;
}
}