redis jedis

这只是一个学习备忘,

http://xmong.iteye.com/blog/1841444

http://snowolf.iteye.com/blog/1633196

http://yychao.iteye.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 = "127.0.0.1";  
    //端口号  
    static int port = 6379;   
      
    public static void main(String[] args) {  
        //根据redis主机和端口号实例化Jedis对象  
        Jedis jedis = new Jedis(host, port);  
        //添加key-value对象,如果key对象存在就覆盖该对象  
        jedis.set("name", "xmong");  
        //查取key的value值,如果key不存在返回null  
        String value = jedis.get("name");  
        System.out.println(value);  
        //删除key-value对象,如果key不存在则忽略此操作  
        jedis.del("name");  
        //判断key是否存在,不存在返回false存在返回true  
        jedis.exists("name");  
    }  
}


池使用

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("redis");  
	    if (bundle == null) {  
	        throw new IllegalArgumentException("[redis.properties] is not found!");  
	    } 
	    //创建jedis池配置实例
	    JedisPoolConfig config = new JedisPoolConfig(); 
	    //设置池配置项值
	    config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));  
	    config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));  
	    config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));  
	    config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));  
	    config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));  
	    //根据配置实例化jedis池
	    pool = new JedisPool(config, bundle.getString("redis.ip"), 
	    		Integer.valueOf(bundle.getString("redis.port")));
	}
	
	/**
	 * 测试jedis池方法
	 */
	public static void test1(){
		//从jedis池中获取一个jedis实例
		Jedis jedis = pool.getResource();
				
		//获取jedis实例后可以对redis服务进行一系列的操作
		jedis.set("name", "xmong");
		System.out.println(jedis.get("name"));
		jedis.del("name");
		System.out.println(jedis.exists("name"));
		
		//释放对象池,即获取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("redis");
			if (bundle == null) {
				throw new IllegalArgumentException(
						"[redis.properties] is not found!");
			}
			// 创建jedis池配置实例
			JedisPoolConfig config = new JedisPoolConfig();
			// 设置池配置项值
			String address = bundle.getString("redis.ip");
			int port = Integer.valueOf(bundle.getString("redis.port"));
			LOGGER.info("Redis server info: " + address + ":" + port);

			String strDbIndex = bundle.getString("redis.db_index");
			if (strDbIndex != null) {
				DEFAULT_DB_INDEX = Integer.valueOf(strDbIndex);
			}

			String strMaxActive = bundle.getString("redis.pool.maxActive");
			if (strMaxActive != null) {
				config.setMaxActive(Integer.valueOf(strMaxActive));
			}

			String strMaxIdle = bundle.getString("redis.pool.maxIdle");
			if (strMaxIdle != null) {
				config.setMaxIdle(Integer.valueOf(strMaxIdle));
			}

			String strMaxWait = bundle.getString("redis.pool.maxWait");
			if (strMaxWait != null) {
				config.setMaxWait(Long.valueOf(strMaxWait));
			}

			String strTestOnBorrow = bundle
					.getString("redis.pool.testOnBorrow");
			if (strTestOnBorrow != null) {
				config.setTestOnBorrow(Boolean.valueOf(strTestOnBorrow));
			}

			String strTestOnReturn = bundle
					.getString("redis.pool.testOnReturn");
			if (strTestOnReturn != null) {
				config.setTestOnReturn(Boolean.valueOf(strTestOnReturn));
			}

			String strTimeout = bundle.getString("redis.pool.timeout");
			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("name", "xmong");
		System.out.println(jedis.get("name"));
		jedis.del("name");
		System.out.println(jedis.exists("name"));
		JedisUtil.returnResource(jedis);
	}















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值