redis庫設置


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;
}


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