|
|
@@ -0,0 +1,216 @@ |
|
|
|
package cn.com.taiji.invw.manager; |
|
|
|
|
|
|
|
import cn.com.taiji.common.entity.BaseEntity; |
|
|
|
import cn.com.taiji.common.manager.AbstractManager; |
|
|
|
import cn.com.taiji.common.model.dao.Pagination; |
|
|
|
import cn.com.taiji.common.pub.StringTools; |
|
|
|
import cn.com.taiji.common.pub.json.JsonTools; |
|
|
|
import cn.com.taiji.core.manager.cache.RedisManager; |
|
|
|
import org.apache.commons.collections4.map.HashedMap; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.data.redis.core.Cursor; |
|
|
|
import org.springframework.data.redis.core.HashOperations; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.data.redis.core.ScanOptions.ScanOptionsBuilder; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.util.*; |
|
|
|
import java.util.Map.Entry; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.function.Supplier; |
|
|
|
|
|
|
|
/** |
|
|
|
* hash操作的方法以hash开头,如:hashGet<br> |
|
|
|
* |
|
|
|
* @author lijun <br> |
|
|
|
* Create Time:2018年12月9日 下午1:43:54<br> |
|
|
|
* mail:756915505@qq.com |
|
|
|
* @since 1.0 |
|
|
|
* @version 1.0 |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
public class RedisWrapManager extends AbstractManager implements RedisManager { |
|
|
|
|
|
|
|
@Autowired(required = false) |
|
|
|
private RedisTemplate<String, String> redisTemplate; |
|
|
|
|
|
|
|
public Set<String> key(String pattern) { |
|
|
|
if ("*".equals(pattern)) { |
|
|
|
throw new RuntimeException("禁止全扫描key"); |
|
|
|
} |
|
|
|
return redisTemplate.keys(pattern); |
|
|
|
} |
|
|
|
|
|
|
|
public Boolean expire(String key, final long timeout, final TimeUnit unit) { |
|
|
|
return redisTemplate.expire(key, timeout, unit); |
|
|
|
} |
|
|
|
|
|
|
|
public String get(String key) { |
|
|
|
return redisTemplate.opsForValue().get(key); |
|
|
|
} |
|
|
|
|
|
|
|
public String getAndSet(String key, String value) { |
|
|
|
return redisTemplate.opsForValue().getAndSet(key, value); |
|
|
|
} |
|
|
|
|
|
|
|
public void set(String key, String value, long timeout, TimeUnit unit) { |
|
|
|
redisTemplate.opsForValue().set(key, value, timeout, unit); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean setIfAbsent(String key, String value, long timeout, TimeUnit unit) { |
|
|
|
return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit); |
|
|
|
} |
|
|
|
|
|
|
|
public long increment(String key, long delta) { |
|
|
|
return redisTemplate.opsForValue().increment(key, delta); |
|
|
|
} |
|
|
|
|
|
|
|
public Boolean delete(String key) { |
|
|
|
return redisTemplate.delete(key); |
|
|
|
} |
|
|
|
|
|
|
|
public long listRightPush(String key, String value) { |
|
|
|
return redisTemplate.opsForList().rightPush(key, value); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 返回存储在 key 的列表里指定范围内的元素。 start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。 |
|
|
|
* 偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推 |
|
|
|
*/ |
|
|
|
public List<String> listRange(String key, long start, long end) { |
|
|
|
return redisTemplate.opsForList().range(key, start, end); |
|
|
|
} |
|
|
|
|
|
|
|
public Long listSize(String key) { |
|
|
|
return redisTemplate.opsForList().size(key); |
|
|
|
} |
|
|
|
|
|
|
|
public Boolean hashHasKey(String key, Object hashKey) { |
|
|
|
return opsForHash().hasKey(key, hashKey); |
|
|
|
} |
|
|
|
|
|
|
|
public boolean hasKey(String key) { |
|
|
|
return redisTemplate.hasKey(key); |
|
|
|
} |
|
|
|
|
|
|
|
public Object hashGet(String key, Object hashKey) { |
|
|
|
return opsForHash().get(key, hashKey); |
|
|
|
} |
|
|
|
|
|
|
|
public void hashPut(String key, Object hashKey, Object value) { |
|
|
|
opsForHash().put(key, hashKey, value); |
|
|
|
} |
|
|
|
|
|
|
|
public Boolean hashPutIfAbsent(String key, Object hashKey, Object value) { |
|
|
|
return opsForHash().putIfAbsent(key, hashKey, value); |
|
|
|
} |
|
|
|
|
|
|
|
public void hashPutAll(String key, Map<? extends Object, ? extends Object> m) { |
|
|
|
opsForHash().putAll(key, m); |
|
|
|
} |
|
|
|
|
|
|
|
public Long hashIncrement(String key, Object hashKey, long delta) { |
|
|
|
return opsForHash().increment(key, hashKey, delta); |
|
|
|
} |
|
|
|
|
|
|
|
public Long hashDelete(String key, Object... hashKeys) { |
|
|
|
return opsForHash().delete(key, hashKeys); |
|
|
|
} |
|
|
|
|
|
|
|
public Map<Object, Object> hashScan(String key, String pattern) { |
|
|
|
ScanOptionsBuilder b = new ScanOptionsBuilder(); |
|
|
|
Cursor<Entry<Object, Object>> s = opsForHash().scan(key, b.match(pattern).build()); |
|
|
|
Map<Object, Object> map = new HashMap<>(); |
|
|
|
s.forEachRemaining(e -> map.put(e.getKey(), e.getValue())); |
|
|
|
return map; |
|
|
|
} |
|
|
|
|
|
|
|
public String findStr(String key, int timeout, Supplier<String> valueSupplier) { |
|
|
|
String str = get(key); |
|
|
|
if (!StringTools.hasText(str)) { |
|
|
|
str = valueSupplier.get(); |
|
|
|
set(key, str, timeout); |
|
|
|
} |
|
|
|
return str; |
|
|
|
} |
|
|
|
|
|
|
|
public <T extends BaseEntity> T findObj(Class<T> clazz, String key, int timeout, Supplier<T> valueSupplier) { |
|
|
|
String jsonStr = (String)get(key); |
|
|
|
try { |
|
|
|
if (!StringTools.hasText(jsonStr)) { |
|
|
|
T obj = valueSupplier.get(); |
|
|
|
jsonStr = JsonTools.toJsonStr(obj); |
|
|
|
set(key, jsonStr, timeout); |
|
|
|
} |
|
|
|
return JsonTools.json2Object(jsonStr, clazz); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public <T extends BaseEntity> T findObj(Class<T> clazz, String key) { |
|
|
|
String jsonStr = redisTemplate.opsForValue().get(key); |
|
|
|
try { |
|
|
|
return JsonTools.json2Object(jsonStr, clazz); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public <T> Map<String, T> findMap(Class<T> clazz, String key, int timeout, Supplier<Map<String, T>> valueSupplier) { |
|
|
|
String jsonStr = get(key); |
|
|
|
try { |
|
|
|
if (!StringTools.hasText(jsonStr)) { |
|
|
|
Map<String, T> map = valueSupplier.get(); |
|
|
|
jsonStr = JsonTools.toJsonStr(map); |
|
|
|
set(key, jsonStr, timeout); |
|
|
|
} |
|
|
|
return JsonTools.json2Map(jsonStr, String.class, clazz); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return new HashedMap<>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public <T> List<T> findList(Class<T> clazz, String key, int timeout, Supplier<Collection<T>> valueSupplier) { |
|
|
|
String jsonStr = get(key); |
|
|
|
try { |
|
|
|
if (!StringTools.hasText(jsonStr)) { |
|
|
|
Collection<T> c = valueSupplier.get(); |
|
|
|
jsonStr = JsonTools.toJsonStr(c); |
|
|
|
set(key, jsonStr, timeout); |
|
|
|
} |
|
|
|
return JsonTools.json2List(jsonStr, clazz); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return new ArrayList<>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public <T extends BaseEntity> Pagination findPagn(Class<T> clazz, String key, int timeout, |
|
|
|
Supplier<Pagination> valueSupplier) { |
|
|
|
String jsonStr = get(key); |
|
|
|
try { |
|
|
|
if (!StringTools.hasText(jsonStr)) { |
|
|
|
Pagination c = valueSupplier.get(); |
|
|
|
jsonStr = JsonTools.toJsonStr(c); |
|
|
|
set(key, jsonStr, timeout); |
|
|
|
} |
|
|
|
return JsonTools.json2Pagn(jsonStr, clazz); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return new Pagination().setResult(new ArrayList<>()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private <HK, HV> HashOperations<String, HK, HV> opsForHash() { |
|
|
|
return redisTemplate.opsForHash(); |
|
|
|
} |
|
|
|
|
|
|
|
private void set(String key, String value, int timeout) { |
|
|
|
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); |
|
|
|
} |
|
|
|
} |