|
|
@@ -0,0 +1,302 @@
|
|
|
+package com.ljsd.redis;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class RedisApp {
|
|
|
+ private final static String Delimiter_colon = ":";
|
|
|
+ private RedisUtil redisUtil = new RedisUtil();
|
|
|
+ private Gson gson = new Gson();
|
|
|
+ private String areaId = "0";
|
|
|
+
|
|
|
+ public RedisApp() {
|
|
|
+ redisUtil.init();
|
|
|
+ }
|
|
|
+
|
|
|
+ private <K, T> String getKey(String type, K key, boolean withServerId) {
|
|
|
+ if (withServerId) {
|
|
|
+ return areaId + Delimiter_colon + type + Delimiter_colon + String.valueOf(key);
|
|
|
+ } else {
|
|
|
+ return type + Delimiter_colon + String.valueOf(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /***************************************普通键值对操作***************************************/
|
|
|
+ //设置键值
|
|
|
+ public <K, T> void set(final String type, final K key, T value, final int seconds, boolean withServerId) throws Exception {
|
|
|
+ String strValue = gson.toJson(value);
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ if (seconds <= 0) {
|
|
|
+ redisUtil.set(fullKey, strValue);
|
|
|
+ } else {
|
|
|
+ redisUtil.setex(fullKey, seconds, strValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> void set(final String type, final String key, T value, final int seconds) throws Exception {
|
|
|
+ set(type, key, value, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //取得键值
|
|
|
+ public <K, T> T get(final String type, final K key, final Class<T> clazz, final int seconds,
|
|
|
+ boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ String strValue = redisUtil.get(fullKey);
|
|
|
+ if (strValue == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return gson.fromJson(strValue,clazz);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> T get(final String type, final K key, final Class<T> clazz, final int seconds) throws Exception {
|
|
|
+ return get(type, key, clazz, seconds, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ //取得键值
|
|
|
+ public <K, T> void del(final String type, final K key, final boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ redisUtil.del(fullKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> void del(final String type, final K key) throws Exception {
|
|
|
+ del(type, key, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K> boolean exists(final String type, final K key, final boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ return redisUtil.exists(fullKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K> boolean exists(final String type, final K key) throws Exception {
|
|
|
+ return exists(type, key, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置过期时间
|
|
|
+ public <K, T> void expire(final String type, final K key, final int seconds, boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ redisUtil.expire(fullKey, seconds);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void expire(final String type, final String key, final int seconds) throws Exception {
|
|
|
+ expire(type, key, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /***************************************以下是map操作***************************************/
|
|
|
+ //转换map对象类型
|
|
|
+ private <K, T> Map<String, String> converStringMap(Map<K, T> mapValues) {
|
|
|
+ Map<String, String> map = new HashMap<String, String>();
|
|
|
+ for (Map.Entry<K, T> entry : mapValues.entrySet()) {
|
|
|
+ String strKey = String.valueOf(entry.getKey());
|
|
|
+ String strValue = gson.toJson(entry.getValue());
|
|
|
+ map.put(strKey, strValue);
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置map的
|
|
|
+ public <K, T> void hmset(final String type, final K key, final Map<K, T> hash, int seconds,
|
|
|
+ boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ Map<String, String> strHash = converStringMap(hash);
|
|
|
+ redisUtil.hmset(fullKey, strHash, seconds);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> void hmset(final String type, final K key, final Map<K, T> hash, final int seconds) throws Exception {
|
|
|
+ hmset(type, key, hash, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //取得map下的子key
|
|
|
+ public <K> Set<String> hkeys(final String type, final K key, final int seconds, boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ return redisUtil.hkeys(fullKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K> Set<String> hkeys(final String type, final K key, final int seconds) throws Exception {
|
|
|
+ return hkeys(type, key, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置redis map一个键值 下的子key下的值
|
|
|
+ public <K, T> void hset(final String type, final K key, final String field, final T value, int seconds,
|
|
|
+ boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ String strValue = gson.toJson(value);
|
|
|
+ redisUtil.hset(fullKey, field, strValue, seconds);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> void hset(final String type, final K key, final String field, final T value, int seconds) throws Exception {
|
|
|
+ hset(type, key, field, value, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //转换map对象类型
|
|
|
+ private <T> Map<String, T> converClassMap(final Map<String, String> mapValues, final Class<T> valueClazz) {
|
|
|
+ Map<String, T> map = new HashMap<>();
|
|
|
+ for (Map.Entry<String, String> entry : mapValues.entrySet()) {
|
|
|
+ T value = gson.fromJson(entry.getValue(), valueClazz);
|
|
|
+ map.put(entry.getKey(), value);
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ //取redis map全部数据的直接方法
|
|
|
+ public <T> Map<String, T> hgetAll(final String type, final String key,
|
|
|
+ final Class<T> valueClazz, int seconds,
|
|
|
+ boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ Map<String, String> mapValues = redisUtil.hgetAll(fullKey);
|
|
|
+ return converClassMap(mapValues, valueClazz);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> Map<String, T> hgetAll(final String type, final String key,
|
|
|
+ final Class<T> valueClazz, int seconds) throws Exception {
|
|
|
+ return hgetAll(type, key, valueClazz, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取redis map下一个key的值的直接方法
|
|
|
+ public <K, SK, T> T hmget(final String type, final K key, final SK field, final Class<T> clazz, int seconds,
|
|
|
+ boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ String strField = String.valueOf(field);
|
|
|
+ List<String> listStrs = redisUtil.hmget(fullKey, strField);
|
|
|
+ if (listStrs == null || listStrs.size() <= 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return gson.fromJson(listStrs.get(0), clazz);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, SK, T> T hmget(final String type, final K key, final SK field, final Class<T> clazz, int seconds) throws Exception {
|
|
|
+ return hmget(type, key, field, clazz, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除子key
|
|
|
+ public <K, SK> void hdel(final String type, final K key, final SK field, boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ String strField = String.valueOf(field);
|
|
|
+ redisUtil.hdel(fullKey, strField);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void hdel(final String type, final String key, final String field) throws Exception {
|
|
|
+ hdel(type, key, field, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /***************************************以下是list操作***************************************/
|
|
|
+ //添加list数据
|
|
|
+ public <K, T> long lpush(final String type, final K key, final int seconds, boolean withServerId,
|
|
|
+ final T... strings) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ String[] strValues = new String[strings.length];
|
|
|
+ for (int i = 0; i < strings.length; i++) {
|
|
|
+ strValues[i] = gson.toJson(strings[i]);
|
|
|
+ }
|
|
|
+ return redisUtil.lpush(fullKey, seconds, strValues);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> long lpush(final String type, final K key, final int seconds, final T... strings) throws Exception {
|
|
|
+ return lpush(type, key, seconds, true, strings);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //从左面保留
|
|
|
+ public <K, T> void ltrimLeft(final String type, final K key, final int start, final int end) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, true);
|
|
|
+ redisUtil.ltrim(fullKey, start, end);
|
|
|
+ }
|
|
|
+
|
|
|
+ //移除最早的元素
|
|
|
+ public <K, T> T rpop(final String type, final K key, final Class<T> clazz, boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ String strValue = redisUtil.rpop(fullKey);
|
|
|
+ return gson.fromJson(strValue, clazz);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> T rpop(final String type, final K key, final Class<T> clazz) throws Exception {
|
|
|
+ return rpop(type, key, clazz);
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加一个新的元素,如果list超过了count,则删除最早的
|
|
|
+ public <K, T> long lpushRpop(final String type, final String key,
|
|
|
+ final T value, final int count, final int seconds, boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ long len = redisUtil.llen(fullKey);
|
|
|
+ if (count > 0 && len >= count) {
|
|
|
+ for (int i = 0; i < len - count + 1; i++) {
|
|
|
+ redisUtil.rpop(fullKey);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String strValue = gson.toJson(value);
|
|
|
+ return redisUtil.lpush(fullKey, seconds, strValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> long lpushRpop(final String type, final String key, final T value,
|
|
|
+ final int count, final int seconds) throws Exception {
|
|
|
+ return lpushRpop(type, key, value, count, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> List<T> converClassList(final List<String> listValues, final Class<T> clazz) {
|
|
|
+ List<T> list = new ArrayList<>();
|
|
|
+ for (String strValue : listValues) {
|
|
|
+ T value = gson.fromJson(strValue, clazz);
|
|
|
+ list.add(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ //从头开始,向未取数据
|
|
|
+ public <K, T> List<T> lrange(final String type, final K key, final Class<T> clazz,
|
|
|
+ final long start, final long end,
|
|
|
+ final int seconds, boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ List<String> listValues = redisUtil.lrange(fullKey, start, end);
|
|
|
+ return converClassList(listValues, clazz);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> List<T> lrange(final String type, final K key, final Class<T> clazz,
|
|
|
+ final long start, final long end, final int seconds) throws Exception {
|
|
|
+ return lrange(type, key, clazz, start, end, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //取得list的全部数据
|
|
|
+ public <K, T> List<T> lgetAll(final String type, final K key, final Class<T> clazz, final int seconds,
|
|
|
+ boolean withServerId) throws Exception {
|
|
|
+ return lrange(type, key, clazz, 0, -1, seconds, withServerId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public <K, T> List<T> lgetAll(final String type, final K key, final Class<T> clazz, final int seconds) throws Exception {
|
|
|
+ return lgetAll(type, key, clazz, seconds, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //自增
|
|
|
+ public Long incr(final String type, final String key, final boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ return redisUtil.incr(fullKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Long incr(final String type, final String key) throws Exception {
|
|
|
+ return incr(type, key, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //redis分布式锁,加锁
|
|
|
+ public boolean tryGetDistributedLock(final String type, final String key, final String requestId,
|
|
|
+ final int expireTime, final boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ return redisUtil.tryGetDistributedLock(fullKey, requestId, expireTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean tryGetDistributedLock(final String type, final String key, final String requestId,
|
|
|
+ final int expireTime) throws Exception {
|
|
|
+// String fullKey = getKey(type, key, true);
|
|
|
+ return tryGetDistributedLock(type, key, requestId, expireTime, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //redis分布式锁,解锁
|
|
|
+ public boolean releaseDistributedLock(final String type, final String key, final String requestId,
|
|
|
+ final boolean withServerId) throws Exception {
|
|
|
+ String fullKey = getKey(type, key, withServerId);
|
|
|
+ return redisUtil.releaseDistributedLock(fullKey, requestId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean releaseDistributedLock(final String type, final String key, final String requestId) throws Exception {
|
|
|
+ return releaseDistributedLock(type, key, requestId, true);
|
|
|
+ }
|
|
|
+}
|