在之前的項目中需要用到一個自動增長的主鍵,該主鍵需要包含字母,所以沒有辦法用到數據庫的自增主鍵。樓主要高手的指導下,發現Redis的RedisAtomicLong類可以解決這個麻煩。而且redis為單線程,不存在線程安全問題
那么,就讓樓主來介紹一下RedisAtomicLong類吧~
RedisAtomicLong類的構造方法如下:
- 構造方法一:
public RedisAtomicLong(java.lang.String redisCounter, RedisConnectionFactory factory)
該實例對應的自動增長的主鍵的key的名字為為redisCounter,如果redis中存在key的name為redisCounter的鍵值對,那么,則取其值;否則,將redisCounter對應的key值設置為0;
- 構造方法二:
public RedisAtomicLong(java.lang.String redisCounter, RedisConnectionFactory factory, long initialValue)
創建一個新的RedisAtomicLong實例,該實例對應的自動增長的主鍵的key的名字為為redisCounter,並將key name為redisCounter的值設置為initialValue;
RedisAtomicLong類有以下幾個主要的方法:
- 方法一:
public long get();//返回當前的值
- 方法二:
public void set(long newValue);//設置當前實例的值為newValue
- 方法三:
public long incrementAndGet();//將當前實例的key值加一並且返回
那么,我們如何獲得一個RedisAtomicLong實例呢?樓主提供以下兩個方法:
在獲取實例之前,我們需要設置好jedis的配置。
在application.xml文件中,加入以下配置:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxTotal}" /> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> </bean> <!-- jedis服務器配置 --> <bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis.ip}" /> <constructor-arg index="1" value="${redis.port}" type="int" /> </bean> <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.ip}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="jedisPoolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnFactory"/> <property name="keySerializer" ref="keySerializer"/> <property name="enableTransactionSupport" value="false"/> </bean> <!-- redis 序列化--> <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
方法一:直接在配置文件中配置
<!-- someKey為設置的自增長主鍵的key的名字--> <bean id="redisAtomicLong" class="org.springframework.data.redis.support.atomic.RedisAtomicLong"> <constructor-arg name="redisCounter" value="someKey"></constructor-arg> <constructor-arg name="factory" ref="jedisConnFactory"></constructor-arg> </bean>
在需要用到redisAtomicLong實例的類里面加入下面這段代碼即可
@Resource private RedisAtomicLong redisAtomicLong;
方法二:在代碼中直接獲得
RedisAtomicLong redisAtomicLong = new RedisAtomicLong("someKey",redisTemplate.getConnectionFactory());
好了,獲得redisAtomicLong實例之后如何來獲得自動增長的值呢?
// 第一次,設置初始值 long original = 0L; // 獲取 code 值 original = redisAtomicLong.get(); System.out.println("*****************original:"+original); // 第一次,設置初始值 if (original == 0L) { redisAtomicLong.set(5L); } //獲得加1后的值 long now = redisAtomicLong.incrementAndGet(); System.out.println("*****************now:"+now); 輸出值: *****************original:0 *****************now:6
有人或許會問,如果我想要同時有兩個自增長的主鍵怎么辦?下面的這段代碼就可以解決這個問題~
RedisAtomicLong atomicLong1 = new RedisAtomicLong("somekey1", redisTemplate.getConnectionFactory(),3L);//創建實例的時候就設置初始值為3 RedisAtomicLong atomicLong2 = new RedisAtomicLong("somekey2", redisTemplate.getConnectionFactory(),5L);//創建實例的時候就設置初始值為5 long now1 = atomicLong1.incrementAndGet(); long now2 = atomicLong2.incrementAndGet(); System.out.println("*****************now:"+now1); System.out.println("*****************now:"+now2); 輸出值: *****************now:6 *****************now:7