定时删除(过期)

本文介绍了几种定时删除过期数据的方法,包括动态参数定时器、ThreadLocal和@Schedule注解,但推荐使用Redis的过期通知功能。通过创建RedisListenerConfig配置类和RedisKeyExpirationListener监听类,当Redis中的key过期时,可以自动触发服务删除对应的数据,避免内存泄漏和操作限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定时删除(过期)

  1. 动态参数定时器,可自定义cron,实现定时执行job。(使用体验:差)
    可通过定时器每隔一段时间从数据库表查“过期时间”字段,跟当前时间对比,如果相同就删除
    在这里插入图片描述

  2. threadlocal(使用体验:极差)
    容易使用不当导致内存泄漏。

  3. @schedule(使用体验:一般)

    @schedule注解注释的方法无法携带参数,限制太多
    4.redis过期通知(强烈推荐!)

给key加上过期时间,通过监听过期键去实现service功能,类似于中间件

1、创建配置类RedisListenerConfig

// An highlighted block
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisListenerConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}

2、创建监听类RedisKeyExpirationListener

// An highlighted block
import cn.hutool.core.util.StrUtil;
import com.leayun.cn.constant.RedisPrefixConstant;
import com.leayun.cn.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

     @Autowired
     RedisTemplate redisTemplate;
     @Autowired
     FlightService flightService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
    @Override
    public void onMessage(Message message, byte[] pattern) {

        // 由于通知收到的是redis key,value已经过期,无法收到,所以需要在key上标记业务数据。
        // 获取到失效的 key,进行取消订单业务处理
           RedisSerializer<String> serializer = redisTemplate.getValueSerializer();
                // 获取到失效的 key
                String orderNo = message.toString();
                //hutool的StrUtil类的startWith方法,判断前缀是否和你redis存的key相同
                if (StrUtil.startWith(orderNo,RedisPrefixConstant.FLIGHT_EXPIRATION_DATE)) {
                //由于我的key是固定字段:id,中间有:分开,所以需要根据:切割字段
                    String[] data = orderNo.split(":");
                    //获取切割后的第二个字段id,实现删除功能
                    flightService.deleteById(Long.valueOf(data[1]));
                    System.out.println("delete success!!!");
                }
    }
}
### Python `map` 函数使用教程 #### 定义与功能 `map()` 是 Python 中的一个内置函数,能够将一个给定的函数应用于一个或多个可迭代对象(如列表、元组等)中的每一个元素,并返回一个新的迭代器,该迭代器包含了应用函数后的结果[^1]。 #### 语法结构 `map()` 的基本语法如下: ```python map(function, iterable) ``` 其中: - `function`: 要应用到每个元素上的函数。 - `iterable`: 可迭代的对象,比如列表、元组或其他支持迭代的数据类型[^3]。 #### 实际案例展示 下面是一些具体的例子来说明如何使用 `map()` 函数: ##### 将列表内的整数转换成字符串形式 如果有一个由数字组成的列表 `[1, 2, 3, 4]` 并想要将其转化为字符串类型的列表,则可以这样做: ```python numbers = [1, 2, 3, 4] str_numbers = list(map(str, numbers)) print(str_numbers) # 输出 ['1', '2', '3', '4'] ``` 这里 `str` 是内建的字符串化方法,而 `list()` 则是用来把 `map` 返回的结果转为列表显示出来[^4]。 ##### 对两个列表对应位置的数值求平方根并相加 假设存在两组数据分别存储于不同的列表中,现在需要计算每一对对应的平方根之和: ```python import math def sqrt_sum(x, y): return math.sqrt(x) + math.sqrt(y) a = [9, 16, 25, 36] b = [4, 81, 100, 144] result = list(map(sqrt_sum, a, b)) print(result) # 输出大约为 [3.742..., 11.0, 15.0, 19.0] ``` 在这个例子中定义了一个名为 `sqrt_sum` 的辅助函数来进行操作;同时传递了两个参数作为输入给 `map()` 函数处理[^2]。 #### 总结 通过上述介绍可以看出,`map()` 提供了一种优雅的方式来遍历容器并对内部项执行相同的操作。这不仅让代码看起来更整洁易懂,而且有助于提升程序性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值