mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 20:22:07 +08:00
add rclient
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package com.yomahub.liteflow.parser.redis.mode;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import org.redisson.api.RMap;
|
||||
import org.redisson.api.RMapCache;
|
||||
import org.redisson.api.RScript;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.api.map.event.MapEntryListener;
|
||||
import org.redisson.client.codec.StringCodec;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Redisson 客户端封装类.
|
||||
*
|
||||
* @author hxinyu
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public class RClient {
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
private Map<String, String> map = new HashMap<>();
|
||||
|
||||
public RClient(RedissonClient redissonClient) {
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get hashmap of the key
|
||||
*
|
||||
* @param key
|
||||
* @return hashmap
|
||||
*/
|
||||
public Map<String, String> getMap(String key) {
|
||||
RMapCache<String, String> mapCache = redissonClient.getMapCache(key);
|
||||
Set<String> mapFieldSet = mapCache.keySet();
|
||||
if (CollectionUtil.isEmpty(mapFieldSet)) {
|
||||
return map;
|
||||
}
|
||||
for (String field : mapFieldSet) {
|
||||
String value = mapCache.get(field);
|
||||
map.put(field, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* add listener of the key
|
||||
*
|
||||
* @param key
|
||||
* @param listener
|
||||
* @return listener id
|
||||
*/
|
||||
public int addListener(String key, MapEntryListener listener) {
|
||||
RMapCache<Object, Object> mapCache = redissonClient.getMapCache(key);
|
||||
return mapCache.addListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* get all keys of hash
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Set<String> hkeys(String key) {
|
||||
RMap<String, String> map = redissonClient.getMap(key, new StringCodec());
|
||||
return map.readAllKeySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* gey value of the key
|
||||
*
|
||||
* @param key
|
||||
* @param field
|
||||
* @return
|
||||
*/
|
||||
public String hget(String key, String field) {
|
||||
RMap<String, String> map = redissonClient.getMap(key, new StringCodec());
|
||||
return map.get(field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads Lua script into Redis scripts cache and returns its SHA-1 digest
|
||||
* @param luaScript
|
||||
* @return shaDigest
|
||||
*/
|
||||
public String scriptLoad(String luaScript) {
|
||||
RScript script = redissonClient.getScript(new StringCodec());
|
||||
return script.scriptLoad(luaScript);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes Lua script stored in Redis scripts cache by SHA-1 digest
|
||||
* @param shaDigest
|
||||
* @param args
|
||||
* @return
|
||||
*/
|
||||
public String evalSha(String shaDigest, String... args){
|
||||
RScript script = redissonClient.getScript(new StringCodec());
|
||||
return script.evalSha(RScript.Mode.READ_ONLY, shaDigest, RScript.ReturnType.VALUE,
|
||||
Arrays.asList(args)).toString();
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package com.yomahub.liteflow.parser.redis.mode.subscribe;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import org.redisson.api.RMapCache;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.api.map.event.MapEntryListener;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Redisson 客户端封装类.
|
||||
*
|
||||
* @author hxinyu
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public class RClient {
|
||||
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
private Map<String, String> map = new HashMap<>();
|
||||
|
||||
public RClient(RedissonClient redissonClient) {
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get hashmap of the key
|
||||
*
|
||||
* @param key
|
||||
* @return hashmap
|
||||
*/
|
||||
public Map<String, String> getMap(String key) {
|
||||
RMapCache<String, String> mapCache = redissonClient.getMapCache(key);
|
||||
Set<String> mapFieldSet = mapCache.keySet();
|
||||
if (CollectionUtil.isEmpty(mapFieldSet)) {
|
||||
return map;
|
||||
}
|
||||
for (String field : mapFieldSet) {
|
||||
String value = mapCache.get(field);
|
||||
map.put(field, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* add listener of the key
|
||||
* @param key
|
||||
* @param listener
|
||||
* @return listener id
|
||||
*/
|
||||
public int addListener(String key, MapEntryListener listener) {
|
||||
RMapCache<Object, Object> mapCache = redissonClient.getMapCache(key);
|
||||
return mapCache.addListener(listener);
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,11 @@ import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.parser.redis.exception.RedisException;
|
||||
import com.yomahub.liteflow.parser.redis.mode.RClient;
|
||||
import com.yomahub.liteflow.parser.redis.mode.RedisParserHelper;
|
||||
import com.yomahub.liteflow.parser.redis.vo.RedisParserVO;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RMapCache;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.api.map.event.EntryCreatedListener;
|
||||
import org.redisson.api.map.event.EntryRemovedListener;
|
||||
import org.redisson.api.map.event.EntryUpdatedListener;
|
||||
@@ -22,7 +21,6 @@ import org.redisson.config.Config;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Redis Pub/Sub机制实现类
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.yomahub.liteflow.test.redis;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.parser.redis.mode.subscribe.RClient;
|
||||
import com.yomahub.liteflow.parser.redis.mode.RClient;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
|
||||
Reference in New Issue
Block a user