package com.qxueyou.scc.base.service.impl;
|
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.concurrent.TimeUnit;
|
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.BoundValueOperations;
|
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.Service;
|
|
import com.qxueyou.scc.base.service.ICacheService;
|
import com.qxueyou.scc.base.util.CollectionUtils;
|
|
import javax.annotation.Resource;
|
|
/**
|
* 阿里云OCS缓存服务
|
*
|
* @author 德虎
|
*
|
*/
|
@Service
|
public class RedisCacheBean implements ICacheService {
|
|
@SuppressWarnings("unused")
|
private static final Logger log = LogManager.getLogger("RedisCacheService");
|
|
@SuppressWarnings("rawtypes")
|
@Resource
|
private RedisTemplate redisTemplate;
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public <T> T get(String key, Class<T> cls) {
|
ValueOperations<String, T> ops = redisTemplate.opsForValue();
|
return ops.get(key);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public <T> T get(String key) {
|
ValueOperations<String, T> ops = redisTemplate.opsForValue();
|
return ops.get(key);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public Map<String, Object> getBulk(String... keys) {
|
if (keys == null || keys.length == 0) {
|
return null;
|
}
|
ValueOperations<String, Object> ops = redisTemplate.opsForValue();
|
List<Object> list = ops.multiGet(CollectionUtils.newList(String.class, keys));
|
Map<String, Object> resultMap = new HashMap<String, Object>(keys.length);
|
for (int i = 0; i < keys.length; i++) {
|
resultMap.put(keys[i], list.get(i));
|
}
|
|
return resultMap;
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void set(String key, int expiredTime, Object obj) {
|
ValueOperations<String, Object> ops = redisTemplate.opsForValue();
|
ops.set(key, obj, expiredTime, TimeUnit.SECONDS);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void set(String key, Object obj) {
|
ValueOperations<String, Object> ops = redisTemplate.opsForValue();
|
ops.set(key, obj);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public boolean setIfAbsent(String key, Object obj) {
|
return redisTemplate.opsForValue().setIfAbsent(key, obj);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void set(String key, String hashKey, Object obj) {
|
HashOperations<String, String, Object> ops = redisTemplate.opsForHash();
|
ops.put(key, hashKey, obj);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public boolean add(String key, int expiredTime, Object obj) {
|
BoundValueOperations<String, Object> boundValueOperations = redisTemplate.boundValueOps(key);
|
boolean result = boundValueOperations.setIfAbsent(obj);
|
if (result) {
|
boundValueOperations.expire(expiredTime, TimeUnit.SECONDS);
|
}
|
return result;
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void delete(String key) {
|
redisTemplate.delete(key);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void append(String key, String value) {
|
ValueOperations<String, Object> ops = redisTemplate.opsForValue();
|
ops.append(key, value);
|
}
|
|
@Override
|
public <T> List<T> lstAll(String key) {
|
|
List<T> lstValue = lstRange(key, 0l, -1l);
|
|
return lstValue;
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void lstRightPush(String key, Object value) {
|
redisTemplate.opsForList().rightPush(key, value);
|
}
|
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void expire(String key,long timeout,TimeUnit unit) {
|
redisTemplate.expire(key, timeout, unit);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public <T> void lstRightPushAll(String key, Collection<T> values) {
|
if (values.isEmpty()) {
|
return;
|
}
|
|
redisTemplate.opsForList().rightPushAll(key, values);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void lstRemove(String key, Object value) {
|
redisTemplate.opsForList().remove(key, 1, value);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public String lstLeftPop(String key) {
|
String leftPop = (String) redisTemplate.opsForList().leftPop(key);
|
return leftPop;
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public <T> List<T> lstRange(String key, long start, long end) {
|
|
List<T> result = new ArrayList<T>(20);
|
result.addAll(redisTemplate.opsForList().range(key, start, end));
|
|
return result;
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void lstTrim(String key, long start, long end) {
|
redisTemplate.opsForList().trim(key, start, end);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void lstClear(String key) {
|
redisTemplate.opsForList().trim(key, 1, 0);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public Long getIncrValue(String key) {
|
return redisTemplate.opsForValue().increment(key, 0l);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public Long incr(String key) {
|
return redisTemplate.opsForValue().increment(key, 1l);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public Long decr(String key) {
|
return redisTemplate.opsForValue().increment(key, -1l);
|
}
|
|
@SuppressWarnings("unchecked")
|
@Override
|
public void putKeyForHash(Object key, Object hashKey, Object value) {
|
|
redisTemplate.opsForHash().put(key, hashKey, value);
|
|
}
|
|
}
|