rfcd12 发表于 2015-11-12 13:15:46

redis jedis

  

  这只是一个学习备忘,
  http://xmong.iyunv.com/blog/1841444
  http://snowolf.iyunv.com/blog/1633196
  http://yychao.iyunv.com/blog/1751583
  等
  需要了解redis,jedis,JedisPool,分布式,ShardedJedis,Pipeline等。。。。
  
  接手一个别人写完的项目,操作redis数据库,首先需要再本地配置redis环境
  
  本来首先肯定是下载安装的,redis官方不支持win,我就选择在虚拟机你们安装。。。。
  但是坑爹是遇到无法链接虚拟机redis的问题,bind也注释掉了,防火墙也停了
  ping能ping通,telnet死活不通
  无计可施了。。。。。
  同事告诉我win7旗舰版是可以的,专业版不行。。。。。。坑爹的win7啊
  官网有一个分支是win下的,但是是代码,需要编译打包才能用
  直接网上找一个打包好的、带exe的吧
  加压直接可以用
  
  安装这一步确实坑大了,耽误了一上午
  
  pom.xml添加jedis的包
  <dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.1.0</version></dependency>
  先来一段测试demo
  
public class TestJedis {
//redis服务器主机
static String host = &quot;127.0.0.1&quot;;
//端口号
static int port = 6379;   
public static void main(String[] args) {
//根据redis主机和端口号实例化Jedis对象
Jedis jedis = new Jedis(host, port);
//添加key-value对象,如果key对象存在就覆盖该对象
jedis.set(&quot;name&quot;, &quot;xmong&quot;);
//查取key的value值,如果key不存在返回null
String value = jedis.get(&quot;name&quot;);
System.out.println(value);
//删除key-value对象,如果key不存在则忽略此操作
jedis.del(&quot;name&quot;);
//判断key是否存在,不存在返回false存在返回true
jedis.exists(&quot;name&quot;);
}
}



  
  池使用


  Commons-pool.jar


  我写demo的里面一起有依赖pool的
  src/main/resources下面建立redis.properties文件


  

#最大分配的对象数
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#IP
redis.ip=127.0.0.1
#Port
redis.port=6379


java  

import java.util.ResourceBundle;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* jedis池使用
* @author xmong
*
*/
public class MyJedisPool {
//jedis池
private static JedisPool pool;
//静态代码初始化池配置
static {
//加载redis配置文件
ResourceBundle bundle = ResourceBundle.getBundle(&quot;redis&quot;);
if (bundle == null) {
throw new IllegalArgumentException(&quot; is not found!&quot;);
}
//创建jedis池配置实例
JedisPoolConfig config = new JedisPoolConfig();
//设置池配置项值
config.setMaxActive(Integer.valueOf(bundle.getString(&quot;redis.pool.maxActive&quot;)));
config.setMaxIdle(Integer.valueOf(bundle.getString(&quot;redis.pool.maxIdle&quot;)));
config.setMaxWait(Long.valueOf(bundle.getString(&quot;redis.pool.maxWait&quot;)));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString(&quot;redis.pool.testOnBorrow&quot;)));
config.setTestOnReturn(Boolean.valueOf(bundle.getString(&quot;redis.pool.testOnReturn&quot;)));
//根据配置实例化jedis池
pool = new JedisPool(config, bundle.getString(&quot;redis.ip&quot;),
Integer.valueOf(bundle.getString(&quot;redis.port&quot;)));
}
/**
* 测试jedis池方法
*/
public static void test1(){
//从jedis池中获取一个jedis实例
Jedis jedis = pool.getResource();
//获取jedis实例后可以对redis服务进行一系列的操作
jedis.set(&quot;name&quot;, &quot;xmong&quot;);
System.out.println(jedis.get(&quot;name&quot;));
jedis.del(&quot;name&quot;);
System.out.println(jedis.exists(&quot;name&quot;));
//释放对象池,即获取jedis实例使用后要将对象还回去
pool.returnResource(jedis);
}
public static void main(String[] args) {
test1();//执行test1方法
}

}
  好了
  整合2篇博客的代码
  我i写了自己的Util 1.0版本
  

import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public final class JedisUtil {
private static final Logger LOGGER = Logger.getLogger(JedisUtil.class);
private static int DEFAULT_DB_INDEX = 0;
private static JedisPool jedisPool = null;
private JedisUtil() {
// private constructor
}
private static void initialPool() {
try {
ResourceBundle bundle = ResourceBundle.getBundle(&quot;redis&quot;);
if (bundle == null) {
throw new IllegalArgumentException(
&quot; is not found!&quot;);
}
// 创建jedis池配置实例
JedisPoolConfig config = new JedisPoolConfig();
// 设置池配置项值
String address = bundle.getString(&quot;redis.ip&quot;);
int port = Integer.valueOf(bundle.getString(&quot;redis.port&quot;));
LOGGER.info(&quot;Redis server info: &quot; + address + &quot;:&quot; + port);
String strDbIndex = bundle.getString(&quot;redis.db_index&quot;);
if (strDbIndex != null) {
DEFAULT_DB_INDEX = Integer.valueOf(strDbIndex);
}
String strMaxActive = bundle.getString(&quot;redis.pool.maxActive&quot;);
if (strMaxActive != null) {
config.setMaxActive(Integer.valueOf(strMaxActive));
}
String strMaxIdle = bundle.getString(&quot;redis.pool.maxIdle&quot;);
if (strMaxIdle != null) {
config.setMaxIdle(Integer.valueOf(strMaxIdle));
}
String strMaxWait = bundle.getString(&quot;redis.pool.maxWait&quot;);
if (strMaxWait != null) {
config.setMaxWait(Long.valueOf(strMaxWait));
}
String strTestOnBorrow = bundle
.getString(&quot;redis.pool.testOnBorrow&quot;);
if (strTestOnBorrow != null) {
config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
}
String strTestOnReturn = bundle
.getString(&quot;redis.pool.testOnReturn&quot;);
if (strTestOnReturn != null) {
config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
}
String strTimeout = bundle.getString(&quot;redis.pool.timeout&quot;);
int timeout = 2000;// 默认2000
if (strTimeout != null) {
timeout = Integer.valueOf(strTimeout);
}
// 根据配置实例化jedis池
jedisPool = new JedisPool(config, address, port, timeout);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
public synchronized static Jedis getJedisInstance() {
if (jedisPool == null) {
initialPool();
}
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
resource.select(DEFAULT_DB_INDEX);
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public synchronized static Jedis getJedisInstance(final int dbIndex) {
if (jedisPool == null) {
initialPool();
}
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
resource.select(dbIndex);
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}

配置文件  
  

#最大分配的对象数
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#当调用超时
redis.pool.timeout=2000
#IP
redis.ip=127.0.0.1
#Port
redis.port=6379
redis.db_index=0

  测试main
  

public static void main(String[] args) {
Jedis jedis = JedisUtil.getJedisInstance();
jedis.set(&quot;name&quot;, &quot;xmong&quot;);
System.out.println(jedis.get(&quot;name&quot;));
jedis.del(&quot;name&quot;);
System.out.println(jedis.exists(&quot;name&quot;));
JedisUtil.returnResource(jedis);
}


  
  
  
  


  


  


  


  


  


  


  


  


  


  


  


  



版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: redis jedis