业务中碰到的需求(抽象描述一下):针对不一样的用户可以实现不一样时间的间隔循环任务。好比在用户注册成功24小时后给用户推送相关短信等相似需求。javascript
使用crontab?过重,且基本不现实,不可能给每个用户在服务器上生成一个定时任务。java
定时轮询?IO频繁且效率过低node
想到常常的使用的redis能够设置缓存时间,应该会有过时的事件通知吧,查了一下文档,果真有相关配置,叫作“键空间事件通知”。具体说明可参考官方文档。redis
redis / nodeJs / koa缓存
核心代码
const { saveClient, subClient } = require('./db/redis') // 存储实例和订阅实例须要为两个不一样的实例
const processor = require('./service/task')
const config = require('./config/index')
const innerDistributedLockKey = '&&__&&' // 内部使用的分布式锁的key的特征值
const innerDistributedLockKeyReg = new RegExp(`^${innerDistributedLockKey}`)
saveClient.on('ready', async () => {
saveClient.config('SET', 'notify-keyspace-events', 'Ex') // 存储实例设置为推送键过时事件
console.log('redis init success')
})
subClient.on('ready', () => { // 服务重启后依旧能够初始化全部processor
subClient.subscribe(`__keyevent@${config.redis.sub.db}__:expired`) // 订阅实例负责订阅消息
subClient.on('message', async (cahnnel, expiredKey) => {
// 分布式锁的key不作监听处理
if (expiredKey.match(innerDistributedLockKeyReg)) return
// 简易分布式锁,拿到锁的实例消费event
const cackeKey = `${innerDistributedLockKey}-${expiredKey}`
const lock = await saveClient.set(cackeKey, 2, 'ex', 5, 'nx') // 这里的用法能够实现简易的分布式锁
if (lock === 'OK') {
await saveClient.del(cackeKey)
for (let key in processor) {
processor[key](expiredKey) // processor对应的是接收到相关键过时通知后执行的业务逻辑,好比推送短信,而后在相关processor中再次set一个定时过时的key
}
}
})
console.log('subClient init success')
})
复制代码
servide/task (processor)
exports.sendMessage = async function sendMessage(expiredKey, subClient) {
// 只处理相关业务的过时事件
if (expiredKey.match(/^send_message/)) {
const [prefix, userId, type] = expiredKey.split('-')
let user = getUser(userId)
if (user.phone) {
push(message) // 伪代码
resetRedisKey(expiredKey, ttl) // 从新把key设置为一段时间后过时,过时后会再次触发本逻辑
}
}
}
复制代码
所以须要权衡使用redis的过时机制实现的定时任务的使用场景。服务器
感谢阅读,转载请注明出处。 喜欢的朋友能够关注个人公众号:雨茗良记,每周会按期更新文章哦,包括但不限于技术。 我是雨茗良记,一个爱作饭的程序猿😜 网络