|
|
|
@ -0,0 +1,136 @@ |
|
|
|
package com.inspect.simulator.utils.redis; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.data.redis.core.BoundSetOperations; |
|
|
|
import org.springframework.data.redis.core.HashOperations; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.data.redis.core.ValueOperations; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@Component |
|
|
|
public class RedisService { |
|
|
|
private final Logger logger = LoggerFactory.getLogger(RedisService.class); |
|
|
|
|
|
|
|
public final RedisTemplate redisTemplate; |
|
|
|
|
|
|
|
public RedisService(RedisTemplate redisTemplate) { |
|
|
|
this.redisTemplate = redisTemplate; |
|
|
|
} |
|
|
|
|
|
|
|
public <T> void setCacheObject(String key, T value) { |
|
|
|
redisTemplate.opsForValue().set(key, value); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> void setCacheObjectOfTask(String key, String taskCode, T value) { |
|
|
|
String redisKey = key + taskCode; |
|
|
|
redisTemplate.opsForValue().set(redisKey, value); |
|
|
|
logger.info("RedisService.setCacheObjectOfTask: {}", redisKey); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> void setCacheObject(String key, T value, Long timeout, TimeUnit timeUnit) { |
|
|
|
redisTemplate.opsForValue().set(key, value, timeout, timeUnit); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> void setCacheObjectOfTask(String key, String taskCode, T value, Long timeout, TimeUnit timeUnit) { |
|
|
|
String redisKey = key + taskCode; |
|
|
|
redisTemplate.opsForValue().set(redisKey, value, timeout, timeUnit); |
|
|
|
logger.info("RedisService.setCacheObjectOfTaskWithTimeout: {}", redisKey); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean expire(String key, long timeout) { |
|
|
|
return expire(key, timeout, TimeUnit.SECONDS); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean expire(String key, long timeout, TimeUnit unit) { |
|
|
|
return redisTemplate.expire(key, timeout, unit); |
|
|
|
} |
|
|
|
|
|
|
|
public long getExpire(String key) { |
|
|
|
return redisTemplate.getExpire(key); |
|
|
|
} |
|
|
|
|
|
|
|
public Boolean hasKey(String key) { |
|
|
|
return redisTemplate.hasKey(key); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> T getCacheObject(String key) { |
|
|
|
ValueOperations<String, T> operation = redisTemplate.opsForValue(); |
|
|
|
return operation.get(key); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> T getCacheObjectOfTask(String key, String taskCode) { |
|
|
|
String redisKey = key + taskCode; |
|
|
|
ValueOperations<String, T> operation = redisTemplate.opsForValue(); |
|
|
|
logger.info("RedisService.getCacheObjectOfTask: {}", redisKey); |
|
|
|
return operation.get(redisKey); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean deleteObject(String key) { |
|
|
|
return redisTemplate.delete(key).booleanValue(); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean deleteObjectOfTask(String key, String taskCode) { |
|
|
|
String redisKey = key + taskCode; |
|
|
|
logger.info("RedisService.deleteObjectOfTask: {}", redisKey); |
|
|
|
return redisTemplate.delete(redisKey).booleanValue(); |
|
|
|
} |
|
|
|
|
|
|
|
public long deleteObject(Collection collection) { |
|
|
|
return redisTemplate.delete(collection).longValue(); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> long setCacheList(String key, List<T> dataList) { |
|
|
|
Long count = redisTemplate.opsForList().rightPushAll(key, dataList); |
|
|
|
return count == null ? 0L : count; |
|
|
|
} |
|
|
|
|
|
|
|
public <T> List<T> getCacheList(String key) { |
|
|
|
return redisTemplate.opsForList().range(key, 0L, -1L); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) { |
|
|
|
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); |
|
|
|
Iterator it = dataSet.iterator(); |
|
|
|
|
|
|
|
while(it.hasNext()) { |
|
|
|
setOperation.add((T[]) new Object[]{it.next()}); |
|
|
|
} |
|
|
|
|
|
|
|
return setOperation; |
|
|
|
} |
|
|
|
|
|
|
|
public <T> Set<T> getCacheSet(String key) { |
|
|
|
return redisTemplate.opsForSet().members(key); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> void setCacheMap(String key, Map<String, T> dataMap) { |
|
|
|
if(dataMap != null) { |
|
|
|
redisTemplate.opsForHash().putAll(key, dataMap); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public <T> Map<String, T> getCacheMap(String key) { |
|
|
|
return redisTemplate.opsForHash().entries(key); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> void setCacheMapValue(String key, String hKey, T value) { |
|
|
|
redisTemplate.opsForHash().put(key, hKey, value); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> T getCacheMapValue(String key, String hKey) { |
|
|
|
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash(); |
|
|
|
return opsForHash.get(key, hKey); |
|
|
|
} |
|
|
|
|
|
|
|
public <T> List<T> getMultiCacheMapValue(String key, Collection<Object> hKeys) { |
|
|
|
return redisTemplate.opsForHash().multiGet(key, hKeys); |
|
|
|
} |
|
|
|
|
|
|
|
public Collection<String> keys(String pattern) { |
|
|
|
return redisTemplate.keys(pattern); |
|
|
|
} |
|
|
|
} |