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

yingxian_Fei發表於2017-06-20

本文介紹在spring+redis組合時,使用redis的RedisAtomicLong生成自增的ID值。

1、自增ID生成類

RedisSequenceFactory是一個簡單封裝類,用於使用redisTemplate生成自增ID值。程式碼如下:

package cn.landsem.cache.redis;

import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;

public class RedisSequenceFactory {
	@Autowired
	RedisTemplate<String, Serializable> mRedisTemp;
	
	/** 
	* @Title: set 
	* @Description: set cache.
	* @param key
	* @param value
	* @param expireTime      
	*/  
	public void set(String key,int value,Date expireTime) {
		RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());
		counter.set(value);
		counter.expireAt(expireTime);		
	}
	
	/** 
	* @Title: set 
	* @Description: set cache.
	* @param key
	* @param value
	* @param timeout
	* @param unit      
	*/  
	public void set(String key,int value,long timeout,TimeUnit unit) {
		RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());
		counter.set(value);
		counter.expire(timeout, unit);
	}
	
	/** 
	* @Title: generate 
	* @Description: Atomically increments by one the current value.
	* @param key
	* @return      
	*/  
	public long generate(String key) {
		RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());
		return counter.incrementAndGet();
	}	
	
	/** 
	* @Title: generate 
	* @Description: Atomically increments by one the current value.
	* @param key
	* @return      
	*/  
	public long generate(String key,Date expireTime) {
		RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());
		counter.expireAt(expireTime);
		return counter.incrementAndGet();	      
	}		
	
	/** 
	* @Title: generate 
	* @Description: Atomically adds the given value to the current value.
	* @param key
	* @param increment
	* @return      
	*/  
	public long generate(String key,int increment) {
		RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());
		return counter.addAndGet(increment);		      
	}
	
	/** 
	* @Title: generate 
	* @Description: Atomically adds the given value to the current value.
	* @param key
	* @param increment
	* @param expireTime
	* @return      
	*/  
	public long generate(String key,int increment,Date expireTime) {
		RedisAtomicLong counter = new RedisAtomicLong(key, mRedisTemp.getConnectionFactory());
		counter.expireAt(expireTime);
		return counter.addAndGet(increment);		      
	}	
}

2、RedisTemplate配置

RedisTemplate在基於java的配置類中進行配置,主要程式碼如下:

	/**
	 * @Title: getDefaultRedisTemplate
	 * @Description: Get a default redis cache template.
	 * @return
	 */
	@Bean
	public RedisTemplate<String, Serializable> getDefaultRedisTemplate(
			RedisConnectionFactory cf,RedisSerializer<?> rs) {
		RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<String, Serializable>();
		redisTemplate.setConnectionFactory(cf);
		redisTemplate.setDefaultSerializer(rs);//Use jboss serialization
		redisTemplate.setKeySerializer(new StringRedisSerializer());//Use String serialization.		
		return redisTemplate;
	}	

3、使用測試

如下為生成一個自增ID,該自增ID快取的key為“hello”,並且該快取在當天23:59:59:999時會自動過期,過期後會重置為0。主要的原始碼如下:

    /** 
    * @Title: getTodayEndTime 
    * @Description: Get the cache expire time.
    * @return      
    */  
    private static Date getTodayEndTime() {  
        Calendar todayEnd = Calendar.getInstance();  
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);  
        todayEnd.set(Calendar.MINUTE, 59);  
        todayEnd.set(Calendar.SECOND, 59);  
        todayEnd.set(Calendar.MILLISECOND, 999);  
        return todayEnd.getTime();  
    } 

  /**
  * The message sequence key.
  */
  public static final String sMsgSequenceKeyFormat = "hello";

  @Autowired
  private RedisSequenceFactory mRedisSequenceFactory; 

   public void test()  {
      long v = mRedisSequenceFactory.generate(sMsgSequenceKeyFormat,getTodayEndTime())
   }


相關文章