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

本文介绍了在Spring环境中,如何利用RedisAtomicLong通过RedisTemplate创建自增ID。详细讲述了RedisSequenceFactory类的封装,RedisTemplate的配置以及具体的使用测试案例,其中ID以“hello”为键,并设置在每天23:59:59:999自动过期并重置为0。

本文介绍在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())
   }


评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值