1. 创建Spring Boot项目

首先,创建一个新的Spring Boot项目。你可以使用Spring Initializr(https://start.spring.io/)来生成项目结构。选择以下依赖:

  • Spring Web
  • Spring Data Redis
  • Lombok(可选,用于简化代码)


2. 配置application.yml

在你的 application.yml文件中添加Redis配置:

1

2

3

4

5

6

7

8


spring:

cache:

type: GENERIC

redis:

host: ${sy.redis.ip}

password:

port: ${sy.redis.port}

database: ${sy.redis.database}



3. 创建缓存配置类

创建一个配置类来手动配置基于Redis的缓存管理器:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22


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)) // 设置缓存过期时间

.disableCachingNullValues()

.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

return RedisCacheManager.builder(redisConnectionFactory)

.cacheDefaults(cacheConfiguration)

.build();

}

}



4. 创建服务类

创建一个服务类来使用Redis的 INCR方法生成每天的序号

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23


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;

// 执行 increment 操作

Long applicantNumber = redisTemplate.opsForValue().increment(key);

// 设置过期时间为2天,不设置默认永久

redisTemplate.expire(key, 2, TimeUnit.DAYS);

//redisTemplate.expire(key) // 查询key的过期时间。

//-1: 表示键存在但没有设置过期时间。

//-2: 表示键不存在。

//返回秒:如上面返回值是 172780,这意味着该键将在大约 172780 秒(约 48 小时)后过期。

return applicantNumber;

}

}



5. 创建控制器

创建一个控制器来暴露获取每天序号的API:

1

2

3

4

5

6

7

8

9

10

11

12

13


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. 启动类

确保你的启动类包含 @EnableCaching注解以启用缓存功能:

1

2

3

4

5

6

7

8

9

10


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);

}

}


到此这篇关于Redis increment 函数处理并发序列号的文章就介绍完了。