sdchy 发表于 2017-12-21 07:40:42

如何用redis来生成唯一Id

  在之前的项目中需要用到一个自动增长的主键,该主键需要包含字母,所以没有办法用到数据库的自增主键。楼主要高手的指导下,发现Redis的RedisAtomicLong类可以解决这个麻烦。而且redis为单线程,不存在线程安全问题
  那么,就让楼主来介绍一下RedisAtomicLong类吧~
  RedisAtomicLong类的构造方法如下:


[*]构造方法一:
  

public RedisAtomicLong(java.lang.String redisCounter,  
RedisConnectionFactory factory)
  


[*]1
[*]2


[*]1
[*]2
  该实例对应的自动增长的主键的key的名字为为redisCounter,如果redis中存在key的name为redisCounter的键值对,那么,则取其值;否则,将redisCounter对应的key值设置为0;


[*]构造方法二:
  

public RedisAtomicLong(java.lang.String redisCounter,  
RedisConnectionFactory factory,
  
long initialValue)
  


[*]1
[*]2
[*]3


[*]1
[*]2
[*]3
  创建一个新的RedisAtomicLong实例,该实例对应的自动增长的主键的key的名字为为redisCounter,并将key name为redisCounter的值设置为initialValue;
  RedisAtomicLong类有以下几个主要的方法:


[*]方法一:
  

public long get();//返回当前的值  


[*]1


[*]1


[*]方法二:
  

public void set(long newValue);//设置当前实例的值为newValue  


[*]1


[*]1


[*]方法三:
  

public long incrementAndGet();//将当前实例的key值加一并且返回  


[*]1


[*]1
  那么,我们如何获得一个RedisAtomicLong实例呢?楼主提供以下两个方法:
  在获取实例之前,我们需要设置好jedis的配置。
  在application.xml文件中,加入以下配置:
  

<bean>  
<property name="maxTotal" value="${redis.pool.maxTotal}" />
  
<property name="maxIdle" value="${redis.pool.maxIdle}" />
  
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
  
</bean>
  

  
<!-- jedis服务器配置 -->
  
<bean>
  
<constructor-arg index="0" value="${redis.ip}" />
  
<constructor-arg index="1" value="${redis.port}" type="int" />
  
</bean>
  

  
<bean      
  
p:host-name="${redis.ip}" p:port="${redis.port}" p:password="${redis.pass}"p:pool-config-ref="jedisPoolConfig"/>
  

  

  
<bean>
  
<property name="connectionFactory" ref="jedisConnFactory"/>
  
<property name="keySerializer" ref="keySerializer"/>
  
<property name="enableTransactionSupport" value="false"/>
  
</bean>
  

  
<!-- redis 序列化-->
  
<bean
  
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  


[*]1
[*]2
[*]3
[*]4
[*]5
[*]6
[*]7
[*]8
[*]9
[*]10
[*]11
[*]12
[*]13
[*]14
[*]15
[*]16
[*]17
[*]18
[*]19
[*]20
[*]21
[*]22
[*]23
[*]24
[*]25


[*]1
[*]2
[*]3
[*]4
[*]5
[*]6
[*]7
[*]8
[*]9
[*]10
[*]11
[*]12
[*]13
[*]14
[*]15
[*]16
[*]17
[*]18
[*]19
[*]20
[*]21
[*]22
[*]23
[*]24
[*]25
  方法一:直接在配置文件中配置
  

<!-- someKey为设置的自增长主键的key的名字-->  
<bean>
  
<constructor-arg name="redisCounter" value="someKey"></constructor-arg>
  
<constructor-arg name="factory" ref="jedisConnFactory"></constructor-arg>
  
</bean>
  


[*]1
[*]2
[*]3
[*]4
[*]5


[*]1
[*]2
[*]3
[*]4
[*]5
  在需要用到redisAtomicLong实例的类里面加入下面这段代码即可
  

@Resource  
private RedisAtomicLong redisAtomicLong;
  


[*]1
[*]2


[*]1
[*]2
  方法二:在代码中直接获得
  

RedisAtomicLong redisAtomicLong = new RedisAtomicLong("someKey",redisTemplate.getConnectionFactory());  


[*]1


[*]1
  好了,获得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
  


[*]1
[*]2
[*]3
[*]4
[*]5
[*]6
[*]7
[*]8
[*]9
[*]10
[*]11
[*]12
[*]13
[*]14
[*]15
[*]16
[*]17
[*]18
[*]19
[*]20


[*]1
[*]2
[*]3
[*]4
[*]5
[*]6
[*]7
[*]8
[*]9
[*]10
[*]11
[*]12
[*]13
[*]14
[*]15
[*]16
[*]17
[*]18
[*]19
[*]20
  有人或许会问,如果我想要同时有两个自增长的主键怎么办?下面的这段代码就可以解决这个问题~
  

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
页: [1]
查看完整版本: 如何用redis来生成唯一Id