亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用redis獲取自增序列號(hào)實(shí)現(xiàn)方式

 更新時(shí)間:2023年12月07日 09:58:43   作者:留守的小柯基  
這篇文章主要介紹了使用redis獲取自增序列號(hào)實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

前言

Redis作為時(shí)下熱門的緩存數(shù)據(jù)庫,由于單線程、直接存取與內(nèi)存中,所以速度很快很高效。redis的使用場(chǎng)景也非常多樣化,常見的是作為優(yōu)秀的緩存中間件,減輕數(shù)據(jù)庫壓力。

近期在項(xiàng)目中使用了redis獲取有序的序列號(hào),作為業(yè)務(wù)單號(hào),非常常見,手里的項(xiàng)目有springBoot也有SpringMvc ,所以在此做下總結(jié)

項(xiàng)目實(shí)例

1.SpringBoot

1.pom文件引入

springBoot有對(duì)redis的封裝插件,直接按以下引入即可:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.yml或者properties配置redis連接的相關(guān)信息

#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=3000
spring.redis.password=shit

3.添加redis配置類,可以實(shí)例化redisTemplate,設(shè)置緩存失效時(shí)間等,以及redis連接池等,這里從簡了

@Configuration
public class RedisConfig {
	 
	 @Bean
	 public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
		  StringRedisTemplate template = new StringRedisTemplate(factory);
		  //序列化方式 
		  Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		  ObjectMapper objectMapper = new ObjectMapper();
		  objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		  objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		  jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
		  template.setValueSerializer(jackson2JsonRedisSerializer);
		  template.afterPropertiesSet();
		  return template;
	 }
	 
	 
}

4.按照key獲取遞增的序列號(hào)

@Configuration
public class RedisConfig {
	 
	 @Bean
	 public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
		  StringRedisTemplate template = new StringRedisTemplate(factory);
		  //序列化方式 
		  Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		  ObjectMapper objectMapper = new ObjectMapper();
		  objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		  objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		  jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
		  template.setValueSerializer(jackson2JsonRedisSerializer);
		  template.afterPropertiesSet();
		  return template;
	 }
	 
	 
}

2.springMVC

springMVC通常引入jedis包來進(jìn)行redis相關(guān)的操作,和上訴spring-boot-starter-data-redis相比,jedis封裝度更低,更加原生態(tài),操作方式和一些方法更接近于redis命令操作。

1.pom文件引入

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.2.1</version>
        </dependency>

2.redis連接配置

redis.host=127.0.0.1
redis.port=6379
redis.maxWait=1000000
redis.password=shit
redis.maxIdle=300
redis.maxTotal=60000

3.在spring配置文件中實(shí)例化對(duì)象

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/> 
        <property name="maxActive" value="${redis.maxTotal}"/> 
        <property name="testOnBorrow" value="true"/> 
    </bean>

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
        <constructor-arg name="host" value="${redis.host}"/>
        <constructor-arg name="port" value="${redis.port}" type="int"/>
        <constructor-arg name="timeout" value="${redis.maxWait}"/>
        <constructor-arg name="password" value="${redis.password}"/>
    </bean>

4.編寫一個(gè)redis操作的工具類,

@Component
public class JedisUtil {
	 protected final static Logger logger = Logger.getLogger(JedisUtil.class);
	 
	 private static JedisPool jedisPool;
	 
	 @Autowired(required = true)
	 public void setJedisPool(JedisPool jedisPool) {
		  JedisUtil.jedisPool = jedisPool;
	 }
	
     /**
	  * 獲取自增的序列號(hào)
	  *
	  * @param key redis主鍵
	  * @return
	  */
	 public static String getIncreNum(String key) {
		  
		  String value = null;
		  Jedis jedis = null;
		  try {
			   jedis = jedisPool.getResource();
			   value = jedis.incrBy(key, 1).toString();
		  } catch (Exception e) {
			   logger.warn("getList " + key + " = " + value);
		  } finally {
			   jedisPool.returnResource(jedis);
		  }
		  return value;
	 }
}

總結(jié)

以上未考慮到redis集群的情況,常規(guī)生成序列號(hào)是夠用了,每次獲取序列號(hào)的時(shí)間也在1毫秒左右,非常高效,同時(shí)列出其他生成連續(xù)號(hào)段的方法。

1.取mysql自增主鍵id,每次插入前查詢當(dāng)前庫max(id) ,優(yōu)點(diǎn):簡單,缺點(diǎn):增加數(shù)據(jù)庫查詢次數(shù),高并發(fā)考慮maxid與實(shí)際值不一致情況

2.創(chuàng)建序列號(hào)的表,存儲(chǔ)序列值,通過添加版本號(hào)字段或加鎖控制寫入 優(yōu)點(diǎn):準(zhǔn)確 缺點(diǎn):效率低

好了,這些僅為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Redis數(shù)據(jù)庫的應(yīng)用場(chǎng)景介紹

    Redis數(shù)據(jù)庫的應(yīng)用場(chǎng)景介紹

    這篇文章主要介紹了Redis數(shù)據(jù)庫的應(yīng)用場(chǎng)景介紹,本文講解了MySql+Memcached架構(gòu)的問題、Redis常用數(shù)據(jù)類型、Redis數(shù)據(jù)類型應(yīng)用和實(shí)現(xiàn)方式、Redis實(shí)際應(yīng)用場(chǎng)景等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • 詳解Redis如何保證接口的冪等性

    詳解Redis如何保證接口的冪等性

    如何防止接口中同樣的數(shù)據(jù)提交,以及如何保證消息不被重復(fù)消費(fèi),這些都是shigen在學(xué)習(xí)的過程中遇到的問題,今天,趁著在學(xué)習(xí)redis的間隙,我寫了一篇文章進(jìn)行簡單的實(shí)現(xiàn),需要的朋友可以參考下
    2023-11-11
  • 聊一聊Redis與MySQL雙寫一致性如何保證

    聊一聊Redis與MySQL雙寫一致性如何保證

    一致性就是數(shù)據(jù)保持一致,在分布式系統(tǒng)中,可以理解為多個(gè)節(jié)點(diǎn)中數(shù)據(jù)的值是一致的。本文給大家分享Redis與MySQL雙寫一致性該如何保證,感興趣的朋友一起看看吧
    2021-06-06
  • Redis如何使用lua腳本實(shí)例教程

    Redis如何使用lua腳本實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于Redis如何使用lua腳本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • redis事務(wù)執(zhí)行常用命令及watch監(jiān)視詳解

    redis事務(wù)執(zhí)行常用命令及watch監(jiān)視詳解

    這篇文章主要為大家介紹了redis事務(wù)執(zhí)行常用命令及watch監(jiān)視詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 緩存替換策略及應(yīng)用(以Redis、InnoDB為例)

    緩存替換策略及應(yīng)用(以Redis、InnoDB為例)

    本文以Redis、InnoDB為例給大家講解緩存替換策略及應(yīng)用,本文給大家提到五種置換策略,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-07-07
  • 基于Redis實(shí)現(xiàn)延時(shí)隊(duì)列的優(yōu)化方案小結(jié)

    基于Redis實(shí)現(xiàn)延時(shí)隊(duì)列的優(yōu)化方案小結(jié)

    本文主要介紹了基于Redis實(shí)現(xiàn)延時(shí)隊(duì)列的優(yōu)化方案小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • redis刪除指定key的實(shí)現(xiàn)步驟

    redis刪除指定key的實(shí)現(xiàn)步驟

    本文主要介紹了redis刪除指定key的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Redis Cluster 字段模糊匹配及刪除

    Redis Cluster 字段模糊匹配及刪除

    在數(shù)據(jù)庫內(nèi)我們可以通過like關(guān)鍵字、%、*或者REGEX關(guān)鍵字進(jìn)行模糊匹配。而在Redis內(nèi)我們?nèi)绾芜M(jìn)行模糊匹配呢?本文就來介紹一下
    2021-05-05
  • 淺談一下如何保證Redis緩存與數(shù)據(jù)庫的一致性

    淺談一下如何保證Redis緩存與數(shù)據(jù)庫的一致性

    這篇文章主要介紹了一下如何保證Redis緩存與數(shù)據(jù)庫的一致性,今天這篇文章就帶你詳細(xì)了解一下四種同步策略,需要的朋友可以參考下
    2023-03-03

最新評(píng)論