Skip to content

Commit

Permalink
Merge pull request #1 from chenshun00/master
Browse files Browse the repository at this point in the history
提高jedis版本,使用连接池,优化了配置
  • Loading branch information
javagaorui5944 authored Nov 5, 2017
2 parents 7187c19 + 4900b0c commit a439461
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
<version>2.9.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/opensymphony/quartz-all -->
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/myapp/redis/JedisPoolFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.myapp.redis;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import static com.myapp.util.Config.*;

/**
*
* @author 竹
* date 2017/11/5
*/
public class JedisPoolFactory {
private static JedisPool pool = null;

public static JedisPool getInstance() {
if(null == pool) {
synchronized (JedisPoolFactory.class) {
if(null == pool ) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setTestOnBorrow(false);
config.setTestWhileIdle(false);
if (null ==auth || auth.length() == 0){
pool = new JedisPool(config, server,port,timeout);
}else {
pool = new JedisPool(config, server,port,timeout, auth);
}
}
}
}
return pool;
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/myapp/redis/JedisUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.myapp.redis;

import com.myapp.proxy.HttpProxy;
import redis.clients.jedis.Jedis;

/**
*
* @author 竹
* date 2017/11/5
*/
public class JedisUtils {

private static void release(Jedis jedis) {
jedis.close();
}

private static Jedis getJedis() {
return JedisPoolFactory.getInstance().getResource();
}

public static void setProxyIp(HttpProxy httpProxy) {
Jedis jedis = getJedis();
/*jedis.sadd("httpProxy", httpProxy.getProxy().toString());*/
jedis.sadd("httpProxy", JsonUtils.toString(httpProxy));
release(jedis);
}

}
22 changes: 22 additions & 0 deletions src/main/java/com/myapp/redis/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.myapp.redis;

import com.alibaba.fastjson.JSON;

/**
*
* @author 竹
* date 2017/11/5
*/
public class JsonUtils {

public static String toString(Object object){
return JSON.toJSONString(object);
}

public static Object toObject(String json,Class<?> clazz){
return JSON.parseObject(json,clazz);
}



}
35 changes: 35 additions & 0 deletions src/main/java/com/myapp/util/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.myapp.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* 读取配置
* @author 竹
* date 2017/11/5
*/
public class Config {
public static String server ;
public static String auth ;
public static int port;
public static int timeout;
public static int maxIdle;
public static int maxActive;

static {
InputStream resourceAsStream = Config.class.getResourceAsStream("/redis.properties");
Properties properties = new Properties();
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
server = properties.getProperty("server");
port = Integer.parseInt(properties.getProperty("port"));
auth = properties.getProperty("password");
timeout = Integer.parseInt(properties.getProperty("timeout"));
maxActive = Integer.parseInt(properties.getProperty("maxActive"));
maxIdle = Integer.parseInt(properties.getProperty("maxIdle"));
}
}
11 changes: 11 additions & 0 deletions src/main/resources/redis.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
##redis服务器 127.0.0.1:6379
server=127.0.0.1
port=6379

### 如果存在验证在下边进行设置 默认没有密码
password=
## 超时
timeout=10000
##
maxActive=300
maxIdle=100

1 comment on commit a439461

@javagaorui5944
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chenshun00 merge了,谢谢你的代码,(我还以为变量没定义呢。。0.0,sorry)

Please sign in to comment.