Redis序列



import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;


import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;


public class RedisSequenceUtil 
{

public static String SEQUENCE_PUBLIC_KEY ="SEQUENCE_PUBLIC_KEY";

public static String SEQUENCE_PICTRUE_KEY ="SEQUENCE_PICTRUE";

public static String SEQUENCE_TABLENO_KEY ="SEQUENCE_TABLENO";

public static String SEQUENCE_ORDER_KEY ="SEQUENCE_ORDER";

public static String SEQUENCE_BILL_KEY ="SEQUENCE_BILL";

public static String SEQUENCE_ACCOUNTNO_KEY ="SEQUENCE_ACCOUNTNO";

public static String SEQUENCE_TRANSCODE_KEY ="SEQUENCE_TRANSCODE";

//用户注册
public static String SEQUENCE_REGIST_KEY ="SEQUENCE_REGIST";
//需求管理
public static String SEQUENCE_DEMAND_KEY ="SEQUENCE_DEMAND";
//报价
public static String SEQUENCE_QUOTE_KEY ="SEQUENCE_QUOTE";
//上传文件
public static String UPLOAD_FILE_KEY ="UPLOAD_FILE";
//投诉
public static String COMPLAINT_KEY ="COMPLAINT";
//平台分发地接序列ID
public static String RECEPTION_DEMAND_KEY ="RECEPTION_DEMAND";
//产品
public static String PRODUCT_KEY ="PRODUCT_KEY";
//子产品
public static String SUBPRODUCT_KEY ="SUBPRODUCT_KEY";
//产品行程
public static String PRODUCT_TRIP_KEY ="PRODUCT_TRIP_KEY";
// //子产品行程
// public static String SUBPRODUCT_TRIP_KEY ="SUBPRODUCT_TRIP_KEY";
//子产品价格
public static String SUBPRODUCT_PRICE_KEY ="SUBPRODUCT_PRICE_KEY";

//交通方式
public static String TRAFFIC_KEY = "TRAFFIC_KEY";

//大交通产品订单预定关联表
public static String ORDER_RELATIVE_BIGTRAFFICE = "ORDER_RELATIVE_BIGTRAFFICE_KEY";
//大交通产品订单预定价格明细
public static String ORDER_PRICE = "ORDER_PRICE_KEY";



//图片库
//public static String PRODUCT_LIBRARY_KEY ="PRODUCT_LIBRARY";
//产品库存
public static String PRODUCT_INVENTORY_KEY ="PRODUCT_INVENTORY_KEY";
//产品库存价格
public static String PRODUCT_INVENTORY_PRICE_KEY ="PRODUCT_INVENTORY_PRICE";
//产品库存价格
public static String PRODUCT_INVENTORY_NUMBER_KEY ="PRODUCT_INVENTORY_NUMBER";
//主产品价格
public static String PRODUCT_PRICE_KEY = "PRODUCT_PRICE_KEY";
//订单日志
public static String SEQUENCE_ORDERLOGS_KEY ="SEQUENCE_ORDERLOGS";
//订单修改
public static String SEQUENCE_ORDERCHANGE_KEY ="SEQUENCE_ORDERCHANGE";
//导游信息
public static String SEQUENCE_GUIDEINFO_KEY ="SEQUENCE_GUIDEINFO";
//酒店 信息
public static String SEQUENCE_HOTELINFO_KEY ="SEQUENCE_HOTELINFO";
//购物流水信息
public static String SEQUENCE_SHOPWATERINFO_KEY ="SEQUENCE_SHOPWATERINFOINFO";
//航班信息
public static String SEQUENCE_FLIGHT_KEY ="SEQUENCE_FLIGHT";
//人员信息
public static String SEQUENCE_CUSTOMER_KEY ="SEQUENCE_CUSTOMER";
//游轮信息
public static String SEQUENCE_SHIP_KEY ="SEQUENCE_SHIP";
//订单实时结算收款
public static String SEQUENCE_RECEIVERECORD_KEY = "SEQUENCE_RECEIVERECORD";
//订单实时结算流水
public static String SEQUENCE_RECEIPTWATER_KEY = "SEQUENCE_RECEIPTWATER";
//组团社行程主表序列
public static String SEQUENCE_TRIP_KEY = "SEQUENCE_TRIP_KEY";
//组团社行程其他信息表序列
public static String SEQUENCE_TRIPITEM_KEY = "SEQUENCE_TRIPITEM_KEY";
//组团社行程图片表序列
public static String SEQUENCE_TRIPIMAGE_KEY = "SEQUENCE_TRIPIMAGE_KEY";
//平台订单审核(mge_order_check)
public static String SEQUENCE_ORDER_CHECK_KEY = "SEQUENCE_ORDER_CHECK";
//质保金及账户余额日志信息(mge_retention_log)
    public static String SEQUENCE_RETENTIONLOG_KEY ="SEQUENCE_RETENTIONLOG";

    //产品出发城市价格
public static String SEQUENCE_CITYPRICE_KEY ="SEQUENCE_CITYPRICE";

//产品分发信息
public static String SEQUENCE_DISTRIBUTE_KEY ="SEQUENCE_DISTRIBUTE";

//游客类型地接社关联
public static String SEQUENCE_TRAVELERRECEPTION_KEY ="SEQUENCE_TRAVELERRECEPTION";

    //游客类型价格设置
  public static String SEQUENCE_PRICE_KEY ="SEQUENCE_PRICE";
 
  //产品出发城市设置时间
  public static String SEQUENCE_SETTIME_KEY ="SEQUENCE_SETTIME";
 
//本地序列缓存组集合
private static Map<String, ConcurrentLinkedQueue<String> > squenceQueueMap = Collections.synchronizedMap(new HashMap<String, ConcurrentLinkedQueue<String>>());

    //redis序列缓存数
private final static  long redisSequenceSize = 100;

private static  JedisSentinelPool jedisSentinelPool;

/**
* 获取序列的值(String类型)
* @param sequenceKey
* @return
* @throws Exception 
*/
public synchronized static String getSequenceValue(String sequenceKey) throws Exception
{
String returnSeq = null;
//从本地缓存中获取序列集合

ConcurrentLinkedQueue<String> queue = squenceQueueMap.get(sequenceKey);
//如果集合对象存在
if(queue != null)
{
if(queue.isEmpty())
{
//从redis获取新的序列组,放到本地缓存
getMutiSequenceFromJedis(sequenceKey, queue);
}

}
else//集合对象不存在,说明有是一个新的序列,重新初始一个缓存序列
{
queue = new ConcurrentLinkedQueue<String>();
squenceQueueMap.put(sequenceKey, queue);
//从redis获取新的序列组,放到本地缓存
getMutiSequenceFromJedis(sequenceKey, queue);
}
returnSeq = queue.poll();
return returnSeq;
}


/**
* 获取序列的值(Long类型)
* @param sequenceKey
* @return
* @throws Exception 
* @throws NumberFormatException 
*/
public  static Long getSequenceValueToLong(String sequenceKey) throws  Exception{
return Long.parseLong(getSequenceValue(sequenceKey));
}



/**
* 从redis获取新的序列组,放到本地缓存
* @param sequenceKey
* @param vector
* @throws Exception 
*/
private static void getMutiSequenceFromJedis(String sequenceKey,ConcurrentLinkedQueue<String> queue) throws Exception
{

Jedis jedis  = getJedis();
try
{
String luaScript = "local firstNum=redis.call('INCR',KEYS[1]);local endNum=firstNum+("+String.valueOf(redisSequenceSize)+"-1);redis.call('SET',KEYS[1],endNum);return firstNum..'-'..endNum;";
//获得的值格式为 1-100  
Object mutiSeq = jedis.eval(luaScript.toString(),1,sequenceKey);
//System.out.println("sequenceNo"+ String.valueOf(mutiSeq));
String [] sequenceNo = String.valueOf(mutiSeq).split("-");
for(Long m = Long.valueOf(sequenceNo[0]);m<=Long.valueOf(sequenceNo[1]);m++)
{
queue.add(String.valueOf(m));
}
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
releaseJedis(jedis);
}
}





/**
* 从redis获取单个序列
* @param sequenceKey
* @throws Exception 
*/
public  static Long getSingleSequenceFromJedis(String sequenceKey) throws Exception
{

Jedis jedis  = getJedis();

Long lreturnSequence = 0l;
try
{
//获得的值格式为 1-100  
lreturnSequence = jedis.incr(sequenceKey);


}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
releaseJedis(jedis);
}

return lreturnSequence;
}




/**
* 获取jedis对象
* @return
*/
    private static Jedis getJedis()
    {
    Jedis jedis=null;
    try{
    if(jedisSentinelPool==null){
       jedisSentinelPool = getJedisSentinelPool();
    }
    jedis = jedisSentinelPool.getResource();
    jedis.select(RedisDistributedUtil.SELECTDB_SEQUENCE);
    }
    catch(Exception ex){
    ex.printStackTrace();
    }
        return jedis;
    }
    
    
    /**
* 获取redis

* @param key
* @param jedisGroup
* @return
*/
private static  JedisSentinelPool getJedisSentinelPool() throws Exception{
JedisSentinelPool jedisSentinelPool = null;
List<RedisCluster> clustersList = null;
try {
clustersList = RedisConfig.redisGroupList;
    if(clustersList!=null && clustersList.size()>0){
jedisSentinelPool = RedisDistributedFactory.redisPoolMap.get(0);
    }
    else{
    throw new Exception("没有在xml文件找到有用的redis服务配置");
    }


} catch (Exception e) {
e.printStackTrace();
throw e;
}
return jedisSentinelPool;
}


    
    /**
     * 释放jedis
     * @param jedisPool
     * @return
     */
    private static void releaseJedis(Jedis jedis)
    {
    jedisSentinelPool.returnResourceObject(jedis);
    }  





}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值