分享

【redis】使用redis RedisAtomicLong生成自增的ID值

 WindySky 2017-10-10

本文介绍在spring+redis组合时,使用redis的RedisAtomicLong生成自增的ID值。

1、自增ID生成类

RedisSequenceFactory是一个简单封装类,用于使用redisTemplate生成自增ID值。代码如下:

  1. package cn.landsem.cache.redis;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.data.redis.core.RedisTemplate;  
  9. import org.springframework.data.redis.support.atomic.RedisAtomicLong;  
  10.   
  11. public class RedisSequenceFactory {  
  12.     @Autowired  
  13.     RedisTemplate<String, Serializable> mRedisTemp;  
  14.       
  15.     /**  
  16.     * @Title: set  
  17.     * @Description: set cache. 
  18.     * @param key 
  19.     * @param value 
  20.     * @param expireTime       
  21.     */    
  22.     public void set(String key,int value,Date expireTime) {  
  23.         RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());  
  24.         counter.set(value);  
  25.         counter.expireAt(expireTime);         
  26.     }  
  27.       
  28.     /**  
  29.     * @Title: set  
  30.     * @Description: set cache. 
  31.     * @param key 
  32.     * @param value 
  33.     * @param timeout 
  34.     * @param unit       
  35.     */    
  36.     public void set(String key,int value,long timeout,TimeUnit unit) {  
  37.         RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());  
  38.         counter.set(value);  
  39.         counter.expire(timeout, unit);  
  40.     }  
  41.       
  42.     /**  
  43.     * @Title: generate  
  44.     * @Description: Atomically increments by one the current value. 
  45.     * @param key 
  46.     * @return       
  47.     */    
  48.     public long generate(String key) {  
  49.         RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());  
  50.         return counter.incrementAndGet();  
  51.     }     
  52.       
  53.     /**  
  54.     * @Title: generate  
  55.     * @Description: Atomically increments by one the current value. 
  56.     * @param key 
  57.     * @return       
  58.     */    
  59.     public long generate(String key,Date expireTime) {  
  60.         RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());  
  61.         counter.expireAt(expireTime);  
  62.         return counter.incrementAndGet();           
  63.     }         
  64.       
  65.     /**  
  66.     * @Title: generate  
  67.     * @Description: Atomically adds the given value to the current value. 
  68.     * @param key 
  69.     * @param increment 
  70.     * @return       
  71.     */    
  72.     public long generate(String key,int increment) {  
  73.         RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());  
  74.         return counter.addAndGet(increment);                
  75.     }  
  76.       
  77.     /**  
  78.     * @Title: generate  
  79.     * @Description: Atomically adds the given value to the current value. 
  80.     * @param key 
  81.     * @param increment 
  82.     * @param expireTime 
  83.     * @return       
  84.     */    
  85.     public long generate(String key,int increment,Date expireTime) {  
  86.         RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());  
  87.         counter.expireAt(expireTime);  
  88.         return counter.addAndGet(increment);                
  89.     }     
  90. }  

2、RedisTemplate配置

RedisTemplate在基于java的配置类中进行配置,主要代码如下:

  1. /**  
  2.  * @Title: getDefaultRedisTemplate  
  3.  * @Description: Get a default redis cache template.  
  4.  * @return  
  5.  */  
  6. @Bean  
  7. public RedisTemplate<String, Serializable> getDefaultRedisTemplate(  
  8.         RedisConnectionFactory cf,RedisSerializer<?> rs) {  
  9.     RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<String, Serializable>();  
  10.     redisTemplate.setConnectionFactory(cf);  
  11.     redisTemplate.setDefaultSerializer(rs);//Use jboss serialization  
  12.     redisTemplate.setKeySerializer(new StringRedisSerializer());//Use String serialization.       
  13.     return redisTemplate;  
  14. }     

3、使用测试

如下为生成一个自增ID,该自增ID缓存的key为“hello”,并且该缓存在当天23:59:59:999时会自动过期,过期后会重置为0。主要的源代码如下:

  1.     /**  
  2.     * @Title: getTodayEndTime  
  3.     * @Description: Get the cache expire time. 
  4.     * @return       
  5.     */    
  6.     private static Date getTodayEndTime() {    
  7.         Calendar todayEnd = Calendar.getInstance();    
  8.         todayEnd.set(Calendar.HOUR_OF_DAY, 23);    
  9.         todayEnd.set(Calendar.MINUTE, 59);    
  10.         todayEnd.set(Calendar.SECOND, 59);    
  11.         todayEnd.set(Calendar.MILLISECOND, 999);    
  12.         return todayEnd.getTime();    
  13.     }   
  14.   
  15.   /** 
  16.   * The message sequence key. 
  17.   */  
  18.   public static final String sMsgSequenceKeyFormat = "hello";  
  19.   
  20.   @Autowired  
  21.   private RedisSequenceFactory mRedisSequenceFactory;   
  22.   
  23.    public void test()  {  
  24.       long v = mRedisSequenceFactory.generate(sMsgSequenceKeyFormat,getTodayEndTime())  
  25.    }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多