JedisUtil
package com.lv.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Created by lvyanghui
* 2018/6/14 16:40
*/
public class JedisUtil {
private static JedisPool pool = null;
public static JedisPool getPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000*10);
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
//new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
pool = new JedisPool(config, "127.0.0.1", 6379, 10000);
}
return pool;
}
public synchronized static Jedis getResource() {
if (pool == null) {
pool = getPool();
}
return pool.getResource();
}
// 返还到连接池
// Deprecated
// 换成用完之后, redis.close()
/*
public static void returnResource(Jedis redis) {
if (redis != null) {
pool.returnResource(redis);
}
}
*/
public static void main(String[] args) {
Jedis redis = null;
int loop = 1;
while (loop < 20) {
try {
long start = System.currentTimeMillis();
redis = getResource();
redis.set("k1", "v1");
String ret = redis.get("k1");
long end = System.currentTimeMillis();
System.out.printf("Get ret from redis: %s with %d millis\n", ret, end-start);
} finally {
if (redis != null) {
redis.close();
}
}
loop++;
}
}
}
RedisPoolUtil
package com.lv.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Properties;
/**
* Created by lvyanghui
* 2018/6/14 16:53
*/
public class RedisPoolUtil {
private static JedisPool jedisPool = null;
private static String redisConfigFile = "redis.properties";
//把redis连接对象放到本地线程中
private static ThreadLocal local=new ThreadLocal();
//不允许通过new创建该类的实例
private RedisPoolUtil() {
}
/**
* 初始化Redis连接池
*/
public static void initialPool() {
try {
Properties props = new Properties();
//加载连接池配置文件
props.load(RedisPoolUtil.class.getClassLoader().getResourceAsStream(redisConfigFile));
// 创建jedis池配置实例
JedisPoolConfig config = new JedisPoolConfig();
// 设置池配置项值
config.setMaxTotal(Integer.valueOf(props.getProperty("jedis.pool.maxActive")));
config.setMaxIdle(Integer.valueOf(props.getProperty("jedis.pool.maxIdle")));
config.setMaxWaitMillis(Long.valueOf(props.getProperty("jedis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(props.getProperty("jedis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(props.getProperty("jedis.pool.testOnReturn")));
// 根据配置实例化jedis池
jedisPool = new JedisPool(config, props.getProperty("redis.ip"),
Integer.valueOf(props.getProperty("redis.port")),
Integer.valueOf(props.getProperty("redis.timeout")));
System.out.println("线程池被成功初始化");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获得连接
* @return Jedis
*/
public static Jedis getConn() {
//Redis对象
Jedis jedis =local.get();
if(jedis==null){
if (jedisPool == null) {
initialPool();
}
jedis = jedisPool.getResource();
local.set(jedis);
}
return jedis;
}
//新版本用close归还连接
public static void closeConn(){
//从本地线程中获取
Jedis jedis =local.get();
if(jedis!=null){
jedis.close();
}
local.set(null);
}
//关闭池
public static void closePool(){
if(jedisPool!=null){
jedisPool.close();
}
}
}
RedisTest
package com.lv.redis;
import redis.clients.jedis.Jedis;
import java.util.List;
/**
* Created by LocalUser on 2018/1/25.
*/
public class RedisTest {
public static void main(String[] args)throws Exception{
Jedis conn = RedisUtil.newInstance();
conn.flushAll();
//string
/*conn.set("userId","80574102");
conn.set("userId","00790433");
conn.expire("userId",1000);
conn.del("userId");
System.out.println(conn.get("userId"));*/
//list
/*conn.rpush("listKey", "item1");
conn.rpush("listKey", "item2");
conn.rpush("listKey", "item3");
conn.rpush("listKey", "item3");
System.out.println(conn.lindex("listKey",0));
System.out.println(conn.lrange("listKey", 0, 5));
System.out.println(conn.rpop("listKey"));
System.out.println(conn.lindex("listKey",0));
System.out.println(conn.lrange("listKey",0,5));*/
//set
/*conn.sadd("setKey","item1");
conn.sadd("setKey","item2");
conn.sadd("setKey","item3");
long sts = conn.sadd("setKey","item1");
System.out.println(sts);
System.out.println(conn.smembers("setKey"));
System.out.println(conn.sismember("setKey", "item1"));
System.out.println(conn.srem("setKey","item1"));
System.out.println(conn.smembers("setKey"));
System.out.println(conn.sismember("setKey", "item1"));*/
//hashset
/*conn.hset("hashSet","key1","item1");
conn.hset("hashSet","key2","item2");
conn.hset("hashSet","key3","item3");
long sts = conn.hset("hashSet","key1","item1");
System.out.println(sts);
System.out.println(conn.hgetAll("hashSet"));
System.out.println(conn.hget("hashSet", "key1"));
System.out.println(conn.hdel("hashSet","key1"));
System.out.println(conn.hgetAll("hashSet"));
System.out.println(conn.hget("hashSet", "key1"));*/
//numset
/*conn.zadd("numset",234,"item1");
conn.zadd("numset",345,"item2");
conn.zadd("numset",589,"item3");
long sts = conn.zadd("numset",234,"item1");
System.out.println(sts);
System.out.println(conn.zrange("numset",0,-1));
System.out.println(conn.zrangeByScore("numset", 0, 400));
System.out.println(conn.zrem("numset","item1"));
System.out.println(conn.zrange("numset",0,-1));
System.out.println(conn.zrangeByScore("numset", 0, 400));*/
/*for (int i = 0; i < 10; i++) {
User user = new User();
user.setId(i+"");
user.setName("kk" + i);
user.setAge(i+"");
user.setAddress("深圳"+i);
conn.rpush("user".getBytes(), SerializeUtil.serialize(user));
}
byte[] bytes = conn.lpop("user".getBytes());
User user0 = (User)SerializeUtil.unserialize(bytes);
System.out.println(user0.getAddress());*/
long time = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
User user = new User();
user.setId(i+"");
user.setName("kk" + i);
user.setAge(i+"");
user.setAddress("深圳"+i);
conn.rpush("user", StringUtil.BeanToString(user));
}
List resultList = conn.lrange("user",0,10000);
for (int i = 0; i < resultList.size(); i++) {
User user = StringUtil.StringToBean(resultList.get(i));
System.out.println(user.getAddress());
}
/*List