@Component
public class RedisCacheService {
@Autowired
public RedisTemplate redisTemplate;
public <T> long setCacheList(String key, List<T> dataList) {
Long count = this.redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0L : count;
}
public <T> List<T> getCacheList(String key) {
return this.redisTemplate.opsForList().range(key, 0L, -1L);
}
public <T> List<T> getCacheList(String key, long start, long end) {
return this.redisTemplate.opsForList().range(key, start, end);
}
public long countCacheList(String key) {
Long count = this.redisTemplate.opsForList().size(key);
return count == null ? 0L : count;
}
public <T> void setCacheObject(String key, T value) {
this.redisTemplate.opsForValue().set(key, value);
}
public <T> void setCacheObject(String key, T value, Long timeout, TimeUnit timeUnit) {
this.redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
public <T> Boolean setIfAbsentCacheObject(String key, T value, Long timeout, TimeUnit timeUnit) {
return this.redisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit);
}
public <T> Boolean setIfAbsentCacheObjectWithoutExpire(String key, T value) {
return this.redisTemplate.opsForValue().setIfAbsent(key, value);
}
public boolean expire(String key, long timeout) {
return this.expire(key, timeout, TimeUnit.SECONDS);
}
public boolean expire(String key, long timeout, TimeUnit unit) {
return this.redisTemplate.expire(key, timeout, unit);
}
public <T> T getCacheObject(String key) {
ValueOperations<String, T> operation = this.redisTemplate.opsForValue();
return operation.get(key);
}
public boolean deleteObject(String key) {
return this.redisTemplate.delete(key);
}
public long deleteObject(Collection collection) {
return this.redisTemplate.delete(collection);
}
/** @deprecated */
public <T> long setCacheSet(String key, Set<T> dataSet) {
Long count = this.redisTemplate.opsForSet().add(key, new Object[]{dataSet});
return count == null ? 0L : count;
}
public <T> long setCacheSet(String key, T... dataSet) {
Long count = this.redisTemplate.opsForSet().add(key, dataSet);
return count == null ? 0L : count;
}
public <T> Set<T> getCacheSet(String key) {
return this.redisTemplate.opsForSet().members(key);
}
public <T> long removeCacheSet(String key, T... dataSet) {
Long count = this.redisTemplate.opsForSet().remove(key, dataSet);
return count == null ? 0L : count;
}
public <T> boolean isMemberCacheSet(String key, T value) {
return this.redisTemplate.opsForSet().isMember(key, value);
}
public long countCacheSet(String key) {
Long count = this.redisTemplate.opsForSet().size(key);
return count == null ? 0L : count;
}
public <T> boolean setCacheZSet(String key, T value, double score) {
return this.redisTemplate.opsForZSet().add(key, value, score);
}
public <T> Set<T> getCacheZSet(String key, int min, int max) {
return this.getCacheZSet(key, min, max, true);
}
public <T> Set<T> getCacheZSet(String key, int min, int max, boolean isAsc) {
ZSetOperations zSetOps = this.redisTemplate.opsForZSet();
new LinkedHashSet();
Set dataZSet;
if (isAsc) {
dataZSet = zSetOps.range(key, (long)min, (long)max);
} else {
dataZSet = zSetOps.reverseRange(key, (long)min, (long)max);
}
return dataZSet;
}
public <T> Set<T> getCacheZSetByScore(String key, double min, double max) {
return this.redisTemplate.opsForZSet().rangeByScore(key, min, max);
}
public <T> Set<T> getCacheZSetByScore(String key, double min, double max, long offset, long count) {
return this.redisTemplate.opsForZSet().rangeByScore(key, min, max, offset, count);
}
public <T> boolean isMemberCacheZSet(String key, T value) {
Long rank = this.redisTemplate.opsForZSet().rank(key, value);
return rank != null;
}
public long countCacheZSet(String key) {
Long count = this.redisTemplate.opsForZSet().zCard(key);
return count == null ? 0L : count;
}
public <T> long removeCacheZSet(String key, T... dataZSet) {
Long count = this.redisTemplate.opsForZSet().remove(key, dataZSet);
return count == null ? 0L : count;
}
public <T> long removeRangeByScore(String key, double start, double end) {
Long count = this.redisTemplate.opsForZSet().removeRangeByScore(key, start, end);
return count == null ? 0L : count;
}
public <T> long removeRangeCacheZSet(String key, long start, long end) {
Long count = this.redisTemplate.opsForZSet().removeRange(key, start, end);
return count == null ? 0L : count;
}
public <T> Double scoreCacheZSet(String key, T value) {
return this.redisTemplate.opsForZSet().score(key, value);
}
public <T> void setCacheMap(String key, Map<String, T> dataMap) {
if (dataMap != null) {
this.redisTemplate.opsForHash().putAll(key, dataMap);
}
}
public <T> Map<String, T> getCacheMap(String key) {
return this.redisTemplate.opsForHash().entries(key);
}
public <T> void setCacheMapValue(String key, String hKey, T value) {
this.redisTemplate.opsForHash().put(key, hKey, value);
}
public <T> boolean hasMapKey(String key, String hKey) {
return this.redisTemplate.opsForHash().hasKey(key, hKey);
}
public <T> T getCacheMapValue(String key, String hKey) {
HashOperations<String, String, T> opsForHash = this.redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
public <T> List<T> getMultiCacheMapValue(String key, Collection<Object> hKeys) {
return this.redisTemplate.opsForHash().multiGet(key, hKeys);
}
public long removeCacheMapValue(String key, Object hKey) {
return this.redisTemplate.opsForHash().delete(key, new Object[]{hKey});
}
public Collection<String> keys(String pattern) {
return this.redisTemplate.keys(pattern);
}
public Boolean isExist(String key) {
return this.redisTemplate.hasKey(key);
}
public Long incrBy(String key, long count) {
return this.redisTemplate.opsForValue().increment(key, count);
}
public boolean lock(String key, String requestId, long seconds) {
Object execute = this.redisTemplate.execute((connection) -> {
return connection.set(this.rawKey(key), this.rawValue(requestId), Expiration.seconds(seconds), SetOption.ifAbsent());
});
return Convert.toBool(execute);
}
public boolean unlock(String key, String requestId) {
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
Object execute = this.redisTemplate.execute(new DefaultRedisScript(script, Boolean.class), ListUtil.toList(new String[]{key}), new Object[]{requestId});
return Convert.toBool(execute);
}
private byte[] rawKey(Object key) {
Assert.notNull(key, "non null key required");
RedisSerializer keySerializer = this.redisTemplate.getKeySerializer();
return keySerializer == null && key instanceof byte[] ? (byte[])((byte[])key) : keySerializer.serialize(key);
}
private byte[] rawValue(Object value) {
RedisSerializer valueSerializer = this.redisTemplate.getValueSerializer();
return valueSerializer == null && value instanceof byte[] ? (byte[])((byte[])value) : valueSerializer.serialize(value);
}
}