package com.weixuan.utils;
import redis.clients.jedis.Jedis;
/**
* Create by fengtang
* 2015/7/30
* JavaRedis
*/
public final class JedisUtils {
/**
* 创建一个jedis链接.
*
* @return 返回当前创建的redis链接对象
*/
public static Jedis getJedisConnection() {
Jedis jedis = new Jedis("localhost", 6379);
return jedis;
}
}
//测试
package com.weixuan.test;
import com.weixuan.utils.JedisPoolUtils;
import com.weixuan.utils.JedisUtils;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* Create by fengtang
* 2015/7/30
* JavaRedis
*/
public class JedisTest {
@Test
public void testJedisUtils() {
System.out.println("testJedisUtils " + JedisUtils.getJedisConnection());
}
}
**注意,版本Jedis版本是2.1 ,common pool 的版本是1.5
高版本中有些参数的设置不一样,比如setMaxActive**
package com.weixuan.utils;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ResourceBundle;
/**
* Create by fengtang
* 2015/7/30
* JavaRedis
*/
public class JedisPoolUtils {
public static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("jedis");
JedisPoolConfig config = new JedisPoolConfig();
if (bundle == null) {
throw new IllegalArgumentException("[jedis.properties] is not found!");
}
config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
}
public static JedisPool getPool() {
return pool;
}
}
//测试
package com.weixuan.test;
import com.weixuan.utils.JedisPoolUtils;
import com.weixuan.utils.JedisUtils;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* Create by fengtang
* 2015/7/30
* JavaRedis
*/
public class JedisTest {
@Test
public void testJedisPoolUtils() throws Exception {
JedisPool pool = JedisPoolUtils.getPool();
System.out.println("testJedisPoolUtils ---pool ------------" + pool);
Jedis jedis = pool.getResource();
System.out.println("testJedisPoolUtils --- jedis ------------" + jedis);
}
}
高版本中测试
* Jedis 版本2.7 common pool 版本2.4*
将
config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
换成
config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxTotal")));
配置文件也需要改
简单的使用
package com.weixuan.redis;
import com.weixuan.utils.JedisPoolUtils;
import redis.clients.jedis.Jedis;
/**
* Create by fengtang
* 2015/7/30
* JavaRedis3
*/
public class Main {
private Jedis jedis = JedisPoolUtils.getPool().getResource();
public void testInsert() {
jedis.set("testKey1", "testValue1");
jedis.set("testKey2", "testValue2");
jedis.set("testKey3", "testValue3");
jedis.set("testKey4", "testValue4");
jedis.set("testKey5", "testValue5");
jedis.set("testKey6", "testValue6");
jedis.set("testKey7", "testValue7");
jedis.set("testKey8", "testValue8");
jedis.set("testKey9", "testValue9");
jedis.set("testKey0", "testValue0");
}
public String testGet(String keyName) {
return jedis.get(keyName);
}
public static void main(String[] args) {
//new Main().testInsert();
System.out.println(new Main().testGet("testKey0"));
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/weiyongxuan/article/details/47151783