/**获取Jedis
* @return
*/
public Jedis getJedis(){
Properties pros = getPprVue();
String isopen = pros.getProperty("redis.isopen");//
String host = pros.getProperty("redis.host");//地址
String port = pros.getProperty("redis.port");//端口
String pass = pros.getProperty("redis.pass");//密码
if("yes".equals(isopen)){
Jedis jedis = new Jedis(host,Integer.parseInt(port));
// jedis.auth(pass);
return jedis;
}else{
return null;
}
}
/**读取redis.properties 配置文件
* @return
* @throws IOException
*/
public Properties getPprVue(){
InputStream inputStream = DbFH.class.getClassLoader().getResourceAsStream("redis.properties");
Properties p = new Properties();
try {
p.load(inputStream);
inputStream.close();
} catch (IOException e) {
//读取配置文件出错
e.printStackTrace();
}
return p;
}
redis.properties
# Redis 参数配置
redis.isopen:yes
redis.host:11.31.142.115
redis.port:6379
redis.pass:w
redis.maxIdle:300
redis.maxActive:600
redis.maxWait:1000
redis.testOnBorrow:true
/**
* 获取reids里所有的数据
* @param prefix
* @return
*/
public Map<String,Object> getRedisData(String prefix)throws Exception{
// prefix="WxCmd";
Map<String,Object> map=new HashMap<String, Object>();
Jedis redis=redisDaoImpl.getJedis();
// 获取所有key
String keyword="WxCmd:";
if(null!=prefix && prefix!=""){
keyword+="*"+prefix;
}
Set s = redis.keys(keyword+"*");
Iterator it = s.iterator();
while (it.hasNext()) {
String key = (String) it.next();
//获取redis存储的类型
String type = redis.type(key);
Object value=new Object();
if(type.equals("hash")){
value=redis.hvals(key);
}else if(type.equals("string")){
value=redis.get(key);
}else{
...
}
map.put(key,value);
}
return map;
}
/**
* 获取reids里所有的数据
* @param prefix
* @return
*/
public Map<String,Map<String,String>> getRedisDataByKey(String prefix)throws Exception{
Map<String,Map<String,String>> result = new HashMap<String,Map<String,String>>();
Jedis redis=redisDaoImpl.getJedis();
Set<String> keys = redis.keys(prefix+"*");
if(null!=keys && keys.size()>0){
//获取管道
Pipeline pipeline = redis.pipelined();
Map<String,Response<Map<String,String>>> responses = new HashMap<String,Response<Map<String,String>>>(keys.size());
Long start = System.currentTimeMillis();
for(String key : keys) {
responses.put(key, pipeline.hgetAll(key));
}
pipeline.sync();
for(String k : responses.keySet()) {
result.put(k, responses.get(k).get());
}
Long end = System.currentTimeMillis();
System.err.println("result size:[" + result.size() + "] ..");
System.err.println("hgetAll with pipeline used [" + (end - start) / 1000 + "] seconds ..");
}
return result;
}
/**
* 根据key获取所有的数据
* @param
* @return
*/
public Map<String,Map<String,String>> getRedisDataByListKey(List<String> keyList)throws Exception{
Map<String,Map<String,String>> result = new HashMap<String,Map<String,String>>();
List<String> keys=keyList;
Jedis redis=redisDaoImpl.getJedis();
if(null!=keys && keys.size()>0){
//获取管道
Pipeline pipeline = redis.pipelined();
Map<String,Response<Map<String,String>>> responses = new HashMap<String,Response<Map<String,String>>>(keys.size());
for(String key : keys) {
responses.put(key, pipeline.hgetAll(key));
}
pipeline.sync();
for(String k : responses.keySet()) {
result.put(k, responses.get(k).get());
}
}
return result;
}