-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from chenshun00/master
提高jedis版本,使用连接池,优化了配置
- Loading branch information
Showing
6 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
a439461
There was a problem hiding this comment.
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)