当前位置: 代码网 > it编程>数据库>Redis > Redis increment 函数处理并发序列号案例

Redis increment 函数处理并发序列号案例

2024年08月29日 Redis 我要评论
1. 创建spring boot项目首先,创建一个新的spring boot项目。你可以使用spring initializr(https://start.spring.io/)来生成项目结构。选择以

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 序列号内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com