Redis increment 函數(shù)處理并發(fā)序列號(hào)案例
1. 創(chuàng)建Spring Boot項(xiàng)目
首先,創(chuàng)建一個(gè)新的Spring Boot項(xiàng)目。你可以使用Spring Initializr(https://start.spring.io/)來(lái)生成項(xiàng)目結(jié)構(gòu)。選擇以下依賴:
- Spring Web
- Spring Data Redis
- Lombok(可選,用于簡(jiǎn)化代碼)
2. 配置application.yml
在你的 ?application.yml?文件中添加Redis配置:
spring:
cache:
type: GENERIC
redis:
host: ${sy.redis.ip}
password:
port: ${sy.redis.port}
database: ${sy.redis.database}3. 創(chuàng)建緩存配置類
創(chuàng)建一個(gè)配置類來(lái)手動(dòng)配置基于Redis的緩存管理器:
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import java.time.Duration;
@Configuration
public class CacheConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10)) // 設(shè)置緩存過(guò)期時(shí)間
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(cacheConfiguration)
.build();
}
}4. 創(chuàng)建服務(wù)類
創(chuàng)建一個(gè)服務(wù)類來(lái)使用Redis的 ?INCR?方法生成每天的序號(hào)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@Service
public class SequenceService {
@Autowired
private StringRedisTemplate redisTemplate;
public long getDailySequence() {
String dateStr = DateUtils.format(new Date(), "yyyy-MM-dd");
String key = "dailySequence_" + dateStr;
// 執(zhí)行 increment 操作
Long applicantNumber = redisTemplate.opsForValue().increment(key);
// 設(shè)置過(guò)期時(shí)間為2天,不設(shè)置默認(rèn)永久
redisTemplate.expire(key, 2, TimeUnit.DAYS);
//redisTemplate.expire(key) // 查詢key的過(guò)期時(shí)間。
//-1: 表示鍵存在但沒(méi)有設(shè)置過(guò)期時(shí)間。
//-2: 表示鍵不存在。
//返回秒:如上面返回值是 172780,這意味著該鍵將在大約 172780 秒(約 48 小時(shí))后過(guò)期。
return applicantNumber;
}
}5. 創(chuàng)建控制器
創(chuàng)建一個(gè)控制器來(lái)暴露獲取每天序號(hào)的API:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SequenceController {
@Autowired
private SequenceService sequenceService;
@GetMapping("/daily-sequence")
public String getDailySequence() {
long sequence = sequenceService.getDailySequence();
return "Daily sequence: " + sequence;
}
}6. 啟動(dòng)類
確保你的啟動(dòng)類包含 ?@EnableCaching?注解以啟用緩存功能:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class RedisIncrApplication {
public static void main(String[] args) {
SpringApplication.run(RedisIncrApplication.class, args);
}
}到此這篇關(guān)于Redis increment 函數(shù)處理并發(fā)序列號(hào)的文章就介紹到這了,更多相關(guān)Redis increment 序列號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Redis實(shí)現(xiàn)分布式單號(hào)及分布式ID(自定義規(guī)則生成)
一些業(yè)務(wù)背景下,業(yè)務(wù)要求單號(hào)需要有區(qū)分不同的前綴,那么在分布式的架構(gòu)下如何自定義單號(hào)而且還能保證唯一呢?本文就來(lái)詳細(xì)的介紹一下2021-09-09
Redis數(shù)據(jù)遷移RedisShake的實(shí)現(xiàn)方法
本文主要介紹了Redis數(shù)據(jù)遷移RedisShake的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
使用百度地圖api通過(guò)redis實(shí)現(xiàn)地標(biāo)存儲(chǔ)及范圍坐標(biāo)點(diǎn)查詢功能
這篇文章主要介紹了使用百度地圖api通過(guò)redis實(shí)現(xiàn)地標(biāo)存儲(chǔ)及范圍坐標(biāo)點(diǎn)查詢功能,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
Redis之RedisTemplate配置方式(序列和反序列化)
這篇文章主要介紹了Redis之RedisTemplate配置方式(序列和反序列化),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

