自己寫的一個jedis操作模板類

jedis是java的redis客戶端程序,在spring下面有專門的jar包提供了一個現成的模板類。不過,自己爲了練習,需要一個超級簡單的對redis操作的工具類。於是,就隨便寫了一個。

 

功能很簡單,通過這個可以對redis進行一些常規操作,用於添加一些測試數據等,並可以熟悉一下java的模板設計模式。

 

代碼不是想取代什麼框架,不是重複造輪子,就是練習,高手可以直接繞道,初學者可以學習如下東西:

1.讀取properties文件

2.使用內部類方式創建的單例類,天生支持多線程

3.jedis的基本使用方法

4.模板模式的使用

 

最終的使用示例:

RedisTemplate util = new RedisTemplate();
/**
 * 在一個事務中提交多個redis操作
 */
util.consoleWithTrancation(new RedisTransactionCallback() {
	@Override
	public void execute(Transaction t) {
		t.set("nname", "1111");
		t.set("nname1", "222");
		t.set("nname2", "333");
	}
});

/**
 * 在一個管道中處理多個redis操作.
 */
util.consoleWithPipe(new RedisPipelineCallback() {
	@Override
	public void execute(Pipeline p) {
		p.set("nn2ame", "1111");
		p.set("nn2ame1", "222");
		p.set("nn2ame2", "333");
	}
});

/**
 * 進行一個redis操作.
 */
util.console(new RedisCallback() {
	@Override
	public void execute(Jedis j) {
		j.set("haha", "還是是多少多少看");
	}
});

 

核心的jedis操作封裝類,包括創建一個jedispool對象:

package brightmoon.redis;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import brightmoon.util.Util;

/**
 * redis處理工具類. 包含讀取配置文件,初始化jedispool,關閉連接等步驟.
 * 
 * @author lsq
 * 
 */
public class RedisUtil {
	protected Log log = LogFactory.getLog(this.getClass().getName());
	private Properties pros;

	private RedisUtil() {
		init();
		initialPool();
	}

	private static class SingletonHolder {
		private static RedisUtil instance = new RedisUtil();
	}

	public static RedisUtil getInstance() {
		return SingletonHolder.instance;
	}

	private String host;
	private String port;
	private String maxActive;
	private String maxIdle;
	private String testOnBorrow;
	private String maxWait;
	private JedisPool jedisPool;

	private void init() {
		InputStream in = null;
		try {
			pros = new Properties();
			in = this.getClass().getResourceAsStream("/redis.properties");
			pros.load(in);

			host = pros.getProperty("host");
			port = pros.getProperty("port");
			maxActive = Util.notBlank(pros.getProperty("maxActive"), "20");
			maxWait = Util.notBlank(pros.getProperty("maxWait"), "5");
			maxIdle = Util.notBlank(pros.getProperty("maxIdle"), "1000");
			testOnBorrow = Util.notBlank(pros.getProperty("testOnBorrow"),
					"false");
		} catch (Exception ex) {
			log.error("沒有找到redis配置文件", ex);
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

	private void initialPool() {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(Integer.parseInt(maxActive));
		config.setMaxIdle(Integer.parseInt(maxIdle));
		config.setMaxWait(Integer.parseInt(maxWait));
		config.setTestOnBorrow("true".equals(testOnBorrow));
		jedisPool = new JedisPool(config, host, Integer.parseInt(port));
	}

	public Jedis getJedis() {
		return jedisPool.getResource();
	}

	/**
	 * 釋放被損壞的jedis.
	 * 
	 * @param jd
	 */
	public void releaseBrokenJedis(Jedis jd) {
		jedisPool.returnBrokenResource(jd);
		jd = null;
	}

	/**
	 * 從連接池中釋放jedis
	 * 
	 * @param jd
	 */
	public void releaseJedis(Jedis jd) {
		jedisPool.returnResource(jd);
		jd = null;
	}
}

 

代碼詳細見附件!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章