1. 创建spring boot项目
首先,创建一个新的spring boot项目。你可以使用spring initializr(https://start.spring.io/)来生成项目结构。选择以下依赖:
- spring web
- spring data redis
- lombok(可选,用于简化代码)
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. 创建缓存配置类
创建一个配置类来手动配置基于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)) // 设置缓存过期时间
.disablecachingnullvalues()
.serializevalueswith(redisserializationcontext.serializationpair.fromserializer(new genericjackson2jsonredisserializer()));
return rediscachemanager.builder(redisconnectionfactory)
.cachedefaults(cacheconfiguration)
.build();
}
}4. 创建服务类
创建一个服务类来使用redis的 incr方法生成每天的序号
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:
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注解以启用缓存功能:
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 函数处理并发序列号的文章就介绍到这了,更多相关redis increment 序列号内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论