redis实现订单自动过期功能的源码分享


本文摘自PHP中文网,作者V,侵删。

文章背景

我们的目的是在用户下单后,规定指定时间后自动将订单设置为“已过期”,不能再发起支付。

(学习视频分享:redis视频教程)

思路:

结合Redis的订阅、发布和键空间通知机制(Keyspace Notifications)进行实现。

配置redis.confg

notify-keyspace-events选项默认是不启用,改为notify-keyspace-events “Ex”。重启生效,索引位i的库,每当有过期的元素被删除时,向**keyspace@:expired**频道发送通知。
E表示键事件通知,所有通知以__keyevent@__:expired为前缀;
x表示过期事件,每当有过期被删除时发送。

与SpringBoot进行集成

1、注册JedisConnectionFactory

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.RedisPassword;

import org.springframework.data.redis.connection.RedisStandaloneConfiguration;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

 

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

 

@Configuration

public class RedisConfig {

     

    @Value("${redis.pool.maxTotal}")

    private Integer maxTotal;

     

    @Value("${redis.pool.minIdle}")

    private Integer minIdle;

     

    @Value("${redis.pool.maxIdle}")

    private Integer maxIdle;

     

    @Value("${redis.pool.maxWaitMillis}")

    private Integer maxWaitMillis;

     

    @Value("${redis.url}")

    private String redisUrl;

     

    @Value("${redis.port}")

    private Integer redisPort;

     

    @Value("${redis.timeout}")

    private Integer redisTimeout;

     

    @Value("${redis.password}")

    private String redisPassword;

     

    @Value("${redis.db.payment}")

    private Integer paymentDataBase;

     

    private JedisPoolConfig jedisPoolConfig() {

        JedisPoolConfig config = new JedisPoolConfig();

        config.setMaxTotal(maxTotal);

        config.setMinIdle(minIdle);

        config.setMaxIdle(maxIdle);

        config.setMaxWaitMillis(maxWaitMillis);

        return config;

    }

     

    @Bean

    public JedisPool jedisPool() {

        JedisPoolConfig config = this.jedisPoolConfig();

        JedisPool jedisPool = new JedisPool(config, redisUrl, redisPort, redisTimeout, redisPassword);

        return jedisPool;

    }

     

    @Bean(name = "jedisConnectionFactory")

    public JedisConnectionFactory jedisConnectionFactory() {

        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();

        redisStandaloneConfiguration.setDatabase(paymentDataBase);

        redisStandaloneConfiguration.setHostName(redisUrl);

        redisStandaloneConfiguration.setPassword(RedisPassword.of(redisPassword));

        redisStandaloneConfiguration.setPort(redisPort);

 

        return new JedisConnectionFactory(redisStandaloneConfiguration);

    }

}

2、注册监听器

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import org.springframework.data.redis.connection.Message;

import org.springframework.data.redis.connection.MessageListener;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

 

@Service(value ="paymentListener")

public class PaymentListener implements MessageListener {

 

    @Override

    @Transactional

    public void onMessage(Message message, byte[] pattern) {

        // 过期事件处理流程

    }

 

}

3、配置订阅对象

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import org.springframework.data.redis.listener.PatternTopic;

import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

 

@Configuration

@AutoConfigureAfter(value = RedisConfig.class)

public class PaymentListenerConfig {

     

    @Autowired

    @Qualifier(value = "paymentListener")

    private PaymentListener paymentListener;

     

    @Autowired

    @Qualifier(value = "paymentListener")

    private JedisConnectionFactory connectionFactory;

     

    @Value("${redis.db.payment}")

    private Integer paymentDataBase;

     

    @Bean

    RedisMessageListenerContainer redisMessageListenerContainer(MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();

        container.setConnectionFactory(connectionFactory);

        // 监听paymentDataBase 库的过期事件

        String subscribeChannel = "__keyevent@" + paymentDataBase + "__:expired";

        container.addMessageListener(listenerAdapter, new PatternTopic(subscribeChannel));

        return container;

    }

     

    @Bean

    MessageListenerAdapter listenerAdapter() {

        return new MessageListenerAdapter(paymentListener);

    }

}

paymentDataBase 库元素过期后就会跳入PaymentListener 的onMessage(Message message, byte[] pattern)方法。

相关推荐:redis数据库教程

以上就是redis实现订单自动过期功能的源码分享的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

Redis主从复制介绍及原理详解

Redis除了做缓存还能做什么

为什么要使用Redis作为缓存

cmd怎么启动Redis

21个使用Redis时必须注意的要点(总结)

spring中Redis怎么用

Redis缓存清理的方法

Redis持久化的方式以及区别

如何在docker容器中成功启动Redis并进入

Redis的事务操作的命令与执行操作(代码)

更多相关阅读请进入《Redis》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...