Redis的应用
import com.alibaba.fastjson.JSONObject;
import com.cngrain.entity.TbMemberOperator;
import com.cngrain.entity.TbSysUser;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.util.Pool;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class RedisUtil {
public static boolean isCluster = false;
public static String serverInfo = null;
private static Pool<Jedis> pool = null;
private static List<String> serverList = new ArrayList<String>();
static {
try {
Properties prop = new Properties();
InputStream in = RedisUtil.class.getClassLoader().getResourceAsStream("redis.properties");
prop.load(in);
in.close();
String mode = prop.getProperty("mode");
if("cluster".equals(mode)){
isCluster = true;
}else{
isCluster= false;
}
String server = prop.getProperty("servers");
System.out.println("mode:"+mode+".servers"+server);
serverInfo = server;
String[] servers = server.split(",");
for(String str : servers){
serverList.add(str.trim());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static Pool<Jedis> getPool(){
if(null == pool){
if(isCluster){
Set<String> sentinels = new HashSet<String>(16);
if(serverList.size()>0){
for(String server : serverList){
sentinels.add(server);
}
}
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setBlockWhenExhausted(true);
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
config.setJmxEnabled(true);
config.setLifo(true);
config.setMaxTotal(1500);
config.setMaxWaitMillis(-1);
config.setMaxIdle(60);
config.setMinIdle(0);
config.setMinEvictableIdleTimeMillis(1800000);
config.setNumTestsPerEvictionRun(3);
config.setSoftMinEvictableIdleTimeMillis(1800000);
config.setTestOnBorrow(false);
config.setTestWhileIdle(false);
config.setTimeBetweenEvictionRunsMillis(-1);
pool = new JedisSentinelPool("redismaster", sentinels, config, 100000);
}else{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(10);
config.setMaxTotal(30);
config.setMaxWaitMillis(3*1000);
String server = serverList.get(0);
String[] temp = server.split(":");
int port = Integer.parseInt(temp[1]);
pool = new JedisPool(config, temp[0], port);
}
}
return pool;
}
public static String get(String key){
String value = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return value;
}
public static byte[] get(byte[] key) {
byte[] value = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return value;
}
public static void set(String key,String value) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void set(byte[] key, byte[] value) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void set(byte[] key, byte[] value, int time) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.set(key, value);
jedis.expire(key, time);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void hset(byte[] key, byte[] field, byte[] value) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.hset(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void hset(String key, String field, String value) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.hset(key, field, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void mhset(String key, Map<String, String> hash) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.hmset(key, hash);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static long llen(String key) {
Jedis jedis = null;
long length=0;
try {
pool = getPool();
jedis = pool.getResource();
length= jedis.llen(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return length;
}
public static long llen(byte[] key) {
long len = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.llen(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return len;
}
public static String hget(String key, String field) {
String value = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.hget(key, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return value;
}
public static Long hdel(String key, String field) {
long value = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.hdel(key, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return value;
}
public static Map<String, String> hgetAll(String key) {
Map<String, String> value = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.hgetAll(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return value;
}
public static byte[] hget(byte[] key, byte[] field) {
byte[] value = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.hget(key, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return value;
}
public static void hdel(byte[] key, byte[] field) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.hdel(key, field);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void lpush(byte[] key, byte[] value) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.lpush(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void lpush(String key, String value) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.lpush(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void rpush(byte[] key, byte[] value) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.rpush(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void rpush(String key,String value) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.rpush(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static String rpoplpush(String key,String destination) {
Jedis jedis = null;
String outValue="";
try {
pool = getPool();
jedis = pool.getResource();
outValue = jedis.rpoplpush(key, destination);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return outValue;
}
public static void rpoplpush(byte[] key, byte[] destination) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.rpoplpush(key, destination);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static List<byte[]> lpopList(byte[] key) {
List<byte[]> list = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
list = jedis.lrange(key, 0, -1);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return list;
}
public static byte[] rpop(byte[] key) {
byte[] bytes = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
bytes = jedis.rpop(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return bytes;
}
public static String rpop(String key) {
String result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.rpop(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static List<String> brpop(String key) {
List<String> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.brpop(0,key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static void hmset(Object key, Map<String,String> hash) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.hmset(key.toString(), hash);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void hmset(Object key, Map<String,String> hash, int time) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.hmset(key.toString(), hash);
jedis.expire(key.toString(), time);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static List<String> hmget(Object key, String... fields) {
List<String> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.hmget(key.toString(), fields);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static Set<String> hkeys(String key) {
Set<String> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.hkeys(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static Set<String> keys(String pattern) {
Set<String> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.keys(pattern);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static List<byte[]> lrange(byte[] key, int from, int to) {
List<byte[]> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.lrange(key, from, to);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static List<String> lrange(String key, int from, int to) {
List<String> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.lrange(key, from, to);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static Map<byte[], byte[]> hgetAll(byte[] key) {
Map<byte[], byte[]> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.hgetAll(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static void del(byte[] key) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void del(String key) {
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static void flushAll(){
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.flushAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
}
public static long ttl(String key) {
long time = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
time = jedis.ttl(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return time;
}
public static long expire(String key, int seconds) {
long len = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
len = jedis.expire(key, seconds);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return len;
}
public static long setnx(String key, String value) {
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.setnx(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static boolean sismember(String setkey, String key) {
boolean result = false;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.sismember(setkey, key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long sadd(String setkey, String memeber) {
long result = 0 ;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.sadd(setkey, memeber);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long srem(String setkey, String memeber) {
long result = 0 ;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.srem(setkey, memeber);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static Set<String> smembers(String setKey){
Set<String> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.smembers(setKey);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static String info() {
String result = "";
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.info();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long zadd(String key, double score, String element) {
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.zadd(key, score, element);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long zrem(String key, String element) {
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.zrem(key,element);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long zadd(String key,Map<String,Double> scoreMembers) {
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.zadd(key, scoreMembers);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long zrem(String key,List<String> elements) {
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
for (String element : elements) {
result = jedis.zrem(key, element);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long incr(String key) {
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.incr(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long decr(String key){
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.decr(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long lrem(String key, long count, String value){
Jedis jedis = null;
long lrem = 0;
try {
pool = getPool();
jedis = pool.getResource();
lrem = jedis.lrem(key, count, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return lrem;
}
public static long zremByScore(String key,String score) {
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.zremrangeByScore(key, score, score);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static boolean exists(String key) {
boolean result = false;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static Set<String> zrange(String key,long start,long end) {
Set<String> result = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.zrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static long zcount(String key) {
long result = 0;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
result = jedis.zcount(key, "-inf", "+inf");
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis != null){ jedis.close();}
}
return result;
}
public static TbSysUser tokenToUser(String token) {
if(token==null||token.isEmpty()) {
return null;
}
JSONObject jsStr = JSONObject.parseObject(get(token+"_info"));
return JSONObject.toJavaObject(jsStr, TbSysUser.class);
}
public static TbMemberOperator tokenToMember(String token) {
if(token==null||token.isEmpty()) {
return null;
}
JSONObject jsStr = JSONObject.parseObject(get(token+"_info"));
return JSONObject.toJavaObject(jsStr, TbMemberOperator.class);
}
public static String tokenToType(String token) {
if(token==null||token.isEmpty()) {
return null;
}
return get(token+"_type");
}
public static void main(String[] args) {
System.out.println(tokenToUser("").getPassword());
}
}