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