import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.zip.CRC32;
import com.google.gson.Gson;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
public class RedisDistributedUtil {
/***----redis库设置-----***/
//
public static final int SELECTDB_SEQUENCE = 1; //序列
public static final int SELECTDB_ROUTE = 4; //路由
public static final int SELECTDB_COMPANY = 5; //企业
public static final int SELECTDB_IMAGE_VALIDCODE = 6; //验证码
public static final int SELECTDB_SEQUENCE_CLEAR = 8; //会清空的业务序列库
public static final int SELECTDB_AD = 9; //广告
public static final int SELECTDB_RECOMMENDATION = 10; //产品推荐
public static final int SELECTDB_JEDISLOCK = 11; //事务控制锁
public static final int SELECTDB_USERINFO = 12; //平台用户信息
public static final int SELECTDB_RSAINFO = 13; //RSA
//redis过期时间
public static final int EXPIRESECONDS_TOKEN = 25*60;
public static final int EXPIRESECONDS_IMAGE_VALIDCODE = 25*60;
public static final int EXPIRESECONDS_SESSION = 30*60;
public static final String AD_FIGURE = "AD_FIGURE";// 轮播
public static final String RECOMMENDATION_TRAVEL = "RECOMMENDATION_TRAVEL";//组团社推荐产品
public static final String RECOMMENDATION_PRODUCT = "RECOMMENDATION_PRODUCT";//推荐产品
private static String charset = "utf-8";
private String key;
private int db;
private JedisSentinelPool jedisSentinelPool;
/**
* 构构函数
*
*
*
*/
public RedisDistributedUtil(String redisKey,int selectDB) {
this.key= redisKey;
this.db= selectDB;
}
/**
* 删除KEY
* @param key
* @param jedisGroup
* @throws Exception
*/
public void deleteObject(String key) throws Exception
{
Jedis jedis=null;
try{
jedis = getJedis();
jedis.del(key);
}
catch(Exception e){
e.printStackTrace();
throw e;
}
finally{
//关闭连接
try {
releaseJedis(jedis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 插入对象
* @param key
* @param insertObj
* @param jedisGroup
* @throws Exception
*/
public void insertObject(String key,Object insertObj) throws Exception
{
Jedis jedis=null;
try{
jedis = getJedis();
Gson gson = new Gson();
jedis.set(key, gson.toJson(insertObj));
}
catch(Exception e){
e.printStackTrace();
throw e;
}
finally{
//关闭连接
try {
releaseJedis(jedis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 插入对象
* @param key
* @param insertObj
* @param jedisGroup
* @param expireSeconds
* @throws Exception
*/
public void insertObject(String key,Object insertObj, int expireSeconds) throws Exception
{
Jedis jedis=null;
try{
jedis = getJedis();
Gson gson = new Gson();
jedis.setex(key, expireSeconds, gson.toJson(insertObj));
}
catch(Exception e){
e.printStackTrace();
throw e;
}
finally{
//关闭连接
try {
releaseJedis(jedis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 插入对象
* @param key
* @param insertObj
* @param jedisGroup
* @param expireSeconds
* @throws Exception
*/
public Object getObject(String key,Class objClass) throws Exception
{
Jedis jedis = null;
Object object = null;
try{
jedis = getJedis();
String sObj = jedis.get(key);
//根据key在redis里找不到对象
if(sObj!=null&&!sObj.equals("{}"))
{
Gson gson = new Gson();
object = gson.fromJson(sObj, objClass);
}
}
catch(Exception e){
e.printStackTrace();
throw e;
}
finally{
//关闭连接
try {
releaseJedis(jedis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return object;
}
public Object getObject(String key,Type type)
{
Jedis jedis = null;
Object object = null;
try{
jedis = getJedis();
String sObj = jedis.get(key);
//根据key在redis里找不到对象
if(sObj!=null&&!sObj.equals("{}"))
{
Gson gson = new Gson();
object = gson.fromJson(sObj, type);
}
}
catch(Exception e){
e.printStackTrace();
throw e;
}
finally{
//关闭连接
try {
releaseJedis(jedis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return object;
}
/**
* 更新redis中key的超时时间
*
* @param key
* @param jedisGroup
* @param expireSeconds
* @throws Exception
*/
public void resetExpireSeconds(String key,int expireSeconds) throws Exception {
Jedis jedis=null;
try{
jedis = getJedis();
jedis.expire(key, expireSeconds);
}
catch(Exception ex){
ex.printStackTrace();
throw ex;
}
finally{
//关闭连接
try {
releaseJedis(jedis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 获取redis
*
* @param key
* @param jedisGroup
* @return
*/
private JedisSentinelPool getJedisSentinelPool() throws Exception{
JedisSentinelPool jedisSentinelPool = null;
List<RedisCluster> clustersList = null;
try {
clustersList = RedisConfig.redisGroupList;
if(clustersList!=null && clustersList.size()>0){
int arrayLength = clustersList.size();
// 根据key值算出应使用的连接池
int clusterNo = getRedisNo(this.key, arrayLength);
jedisSentinelPool = RedisDistributedFactory.redisPoolMap.get(clusterNo);
}
else{
throw new Exception("没有在xml文件找到有用的redis服务配置");
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return jedisSentinelPool;
}
/**
* 获取redis
* @param key
* @param jedisGroup
* @return
*/
private Jedis getJedis()
{
Jedis jedis = null;
try{
jedisSentinelPool = getJedisSentinelPool();
jedis = jedisSentinelPool.getResource();
jedis.select(this.db);
}
catch(Exception e){
e.printStackTrace();
}
return jedis;
}
/**
* 释放连接池jedis
*
* @param jedis
*/
private void releaseJedis(Jedis jedis) throws Exception{
try {
if (this.jedisSentinelPool != null) {
jedisSentinelPool.returnResourceObject(jedis);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 根据key值算出hash值
*
* @param k
* @return
*/
private long hash(String key) {
// 统一的hash算法
CRC32 crc32 = new CRC32();
try {
crc32.update(key.getBytes(charset));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return crc32.getValue();
}
/**
* 获取redis服务器库编号
*
* @param hashKey
* @return
*/
private int getRedisNo(String key, int arraySize) {
long hashKey = hash(key);
// 算出redis的使用库号
int redisNo = (int) (hashKey % arraySize);
return redisNo;
}
}