You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.8 KiB
124 lines
3.8 KiB
package com.fr.plugin.db.redis.help.client; |
|
|
|
import com.fanruan.api.log.LogKit; |
|
import com.fr.json.JSON; |
|
import com.fr.json.JSONFactory; |
|
import com.fr.plugin.db.redis.core.accessor.EmbedRedis; |
|
import com.fr.plugin.db.redis.help.RedisClient; |
|
import com.fr.plugin.db.redis.help.ScriptBridge; |
|
|
|
import java.util.Collection; |
|
import java.util.List; |
|
import java.util.Map; |
|
import java.util.Set; |
|
import java.util.TreeMap; |
|
|
|
/** |
|
* @author richie |
|
* @version 10.0 |
|
* Created by richie on 2019-08-26 |
|
*/ |
|
public abstract class BaseRedisClient<A> implements RedisClient<A> { |
|
|
|
EmbedRedis jedis; |
|
|
|
BaseRedisClient(EmbedRedis jedis) { |
|
this.jedis = jedis; |
|
} |
|
|
|
@ScriptBridge |
|
public String get(String key) { |
|
long start = System.currentTimeMillis(); |
|
try { |
|
return jedis.get(key); |
|
} finally { |
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); |
|
} |
|
} |
|
|
|
@ScriptBridge |
|
public String hget(String key, String field) { |
|
long start = System.currentTimeMillis(); |
|
try { |
|
return jedis.hget(key, field); |
|
} finally { |
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); |
|
} |
|
} |
|
|
|
@Override |
|
@ScriptBridge |
|
public String hkeys(String key) { |
|
long start = System.currentTimeMillis(); |
|
Set<String> set = jedis.hkeys(key); |
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); |
|
return object2JSONString(set); |
|
} |
|
|
|
@Override |
|
@ScriptBridge |
|
public String hmget(String key, String... fields) { |
|
long start = System.currentTimeMillis(); |
|
List<String> list = jedis.hmget(key, fields); |
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); |
|
return object2JSONString(list); |
|
} |
|
|
|
@Override |
|
@ScriptBridge |
|
public String hgetAll(String key) { |
|
long start = System.currentTimeMillis(); |
|
Map<String, String> map = jedis.hgetAll(key); |
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); |
|
return object2JSONString(map); |
|
} |
|
|
|
@Override |
|
@ScriptBridge |
|
public String keys(String pattern) { |
|
long start = System.currentTimeMillis(); |
|
Set<String> set = jedis.keys(pattern); |
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); |
|
return object2JSONString(set); |
|
} |
|
|
|
@Override |
|
@ScriptBridge |
|
public String lrange(String key, long start, long end) { |
|
long start0 = System.currentTimeMillis(); |
|
List<String> list = jedis.lrange(key, start, end); |
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start0); |
|
return object2JSONString(list); |
|
} |
|
|
|
@Override |
|
@ScriptBridge |
|
public String smembers(String key) { |
|
long start = System.currentTimeMillis(); |
|
Set<String> set = jedis.smembers(key); |
|
LogKit.info("Fetch data from redis spend time {} ms.", System.currentTimeMillis() - start); |
|
return object2JSONString(set); |
|
} |
|
|
|
/** |
|
* 将集合输出为JSON格式的字符串,便于JS和Java互传数据 |
|
* |
|
* @param collection 集合 |
|
* @return 字符串 |
|
*/ |
|
String object2JSONString(Collection<String> collection) { |
|
return JSONFactory.createJSON(collection).toString(); |
|
} |
|
|
|
/** |
|
* 将集合输出为JSON格式的字符串,便于JS和Java互传数据 |
|
* |
|
* @param map 集合 |
|
* @return 字符串 |
|
*/ |
|
String object2JSONString(Map<String, String> map) { |
|
Map<String, Object> newMap = new TreeMap<String, Object>(); |
|
newMap.putAll(map); |
|
return JSONFactory.createJSON(JSON.OBJECT, newMap).toString(); |
|
} |
|
}
|
|
|