刚学习redis,所以想写了一个redis底层帮助类,思想如下:
/**
* (1)先配置redis.properties里面写配置相关信息
(2)写一个RedisUtil里面满足如下条件
a:能连接redis用到单例, 并且是从线程池里拿
b:有各种自己所需要的方法(这个根据自己的需要来添加)
*
* */
<pre name="code" class="java"> /**<pre name="code" class="java">redis.properties
*/
redis.pool.maxActive=1024
redis.pool.maxIdle=200
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
#IP
redis.ip=10.11.20.140
#Port
redis.port=6379
<pre name="code" class="java">/**<pre name="code" class="java"><pre name="code" class="java">RedisUtil
*/
<pre name="code" class="java">package com.yc.dao;
import java.util.List;
import java.util.ResourceBundle;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisUtil {
/**
* (1)先配置redis.properties里面写配置相关信息
(2)写一个RedisUtil里面满足如下条件
a:能连接redis用到单例, 并且是从线程池里拿
b:有各种自己所需要的方法(这个根据自己的需要来添加)
*
* */
/***************池化使用jedis********************************/
private static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxActive")));//最大连接数
config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));//最大等待时间
// 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")));
pool = new JedisPool(config, bundle.getString("redis.ip"),
Integer.valueOf(bundle.getString("redis.port")));
}
/***
* 从池中获取一个Jedis对象
*/
public Jedis getJedis() {
return pool.getResource();
}
/**
* 从连接池中释放jedis
*
* @param jd
*/
public void releaseJedis(Jedis jd) {
pool.returnResource(jd);
jd = null;
}
public String setString(String key, String value) {
Jedis jedis = this.pool.getResource();
String status = jedis.set(key, value);
this.pool.returnResource(jedis);
return status;
}
public String getString(String key) {
Jedis jedis = this.pool.getResource();
String value = jedis.get(key);
this.pool.returnResource(jedis);
return value;
}
/**
* This Stirng的批量操作
* @param pairs
*/
public String set(byte[] key, byte[] value) {
Jedis jedis = this.pool.getResource();
String status = jedis.set(key, value);
this.pool.returnResource(jedis);
return status;
}
public byte[] get(byte[] key) {
Jedis jedis = this.pool.getResource();
byte[] value = jedis.get(key);
this.pool.returnResource(jedis);
return value;
}
/**
* 删除操作
* */
public Long del(String key) {
Jedis jedis = this.pool.getResource();
Long status = jedis.del(key);
this.pool.returnResource(jedis);
return status;
}
}
//这是最早的版本,我还会更新