Redis中Lua脚本的常见场景

 更新时间:2025年10月20日 08:31:22   作者:辞暮尔尔-烟火年年  
Redis 的 Lua 脚本可以极大提升操作的原子性和效率,特别适用于需要多个 Redis 命令组合执行的场景,本文就来详细的介绍一下Redis中Lua脚本的常见场景,感兴趣的可以了解一下

Redis 的 Lua 脚本可以极大提升操作的原子性和效率,特别适用于需要多个 Redis 命令组合执行的场景。以下是一些常见的使用场景,并结合代码进行详细说明。

1. 分布式锁

Redis 的 Lua 脚本常用于实现分布式锁,以确保多个客户端在并发访问时的互斥性。

示例:分布式锁的获取与释放

-- 获取锁
local lock_key = KEYS[1]
local lock_value = ARGV[1]
local ttl = tonumber(ARGV[2])

if redis.call("SETNX", lock_key, lock_value) == 1 then
    redis.call("PEXPIRE", lock_key, ttl)
    return 1
else
    return 0
end
redis-cli EVAL "local lock_key = KEYS[1]; local lock_value = ARGV[1]; local ttl = tonumber(ARGV[2]); if redis.call('SETNX', lock_key, lock_value) == 1 then redis.call('PEXPIRE', lock_key, ttl); return 1; else return 0; end" 1 mylock lock_value 30000
-- 释放锁
local lock_key = KEYS[1]
local lock_value = ARGV[1]

if redis.call("GET", lock_key) == lock_value then
    redis.call("DEL", lock_key)
    return 1
else
    return 0
end
redis-cli EVAL "local lock_key = KEYS[1]; local lock_value = ARGV[1]; if redis.call('GET', lock_key) == lock_value then redis.call('DEL', lock_key); return 1; else return 0; end" 1 mylock lock_value

2. 计数器

实现自增、自减等计数器功能。

示例:原子性的自增操作

local key = KEYS[1]
local increment = tonumber(ARGV[1])

local current = redis.call("GET", key)
if current == false then
    current = 0
else
    current = tonumber(current)
end

local new_value = current + increment
redis.call("SET", key, new_value)
return new_value
redis-cli EVAL "local key = KEYS[1]; local increment = tonumber(ARGV[1]); local current = redis.call('GET', key); if current == false then current = 0; else current = tonumber(current); end; local new_value = current + increment; redis.call('SET', key, new_value); return new_value;" 1 mycounter 1

3. 事务性操作

Lua 脚本可以确保多条命令的原子性,避免使用事务的复杂性。

示例:转账操作

local from_account = KEYS[1]
local to_account = KEYS[2]
local amount = tonumber(ARGV[1])

local from_balance = tonumber(redis.call("GET", from_account))
local to_balance = tonumber(redis.call("GET", to_account))

if from_balance >= amount then
    redis.call("DECRBY", from_account, amount)
    redis.call("INCRBY", to_account, amount)
    return 1
else
    return 0
end
redis-cli EVAL "local from_account = KEYS[1]; local to_account = KEYS[2]; local amount = tonumber(ARGV[1]); local from_balance = tonumber(redis.call('GET', from_account)); local to_balance = tonumber(redis.call('GET', to_account)); if from_balance >= amount then redis.call('DECRBY', from_account, amount); redis.call('INCRBY', to_account, amount); return 1; else return 0; end" 2 account1 account2 100

4. 排行榜

操作有序集合(sorted sets)实现排行榜功能。

示例:获取排行榜前 N 名

local key = KEYS[1]
local limit = tonumber(ARGV[1])

return redis.call("ZRANGE", key, 0, limit - 1, "WITHSCORES")
redis-cli EVAL "local key = KEYS[1]; local limit = tonumber(ARGV[1]); return redis.call('ZRANGE', key, 0, limit - 1, 'WITHSCORES');" 1 leaderboard 10

5. 队列操作

通过列表(list)实现任务队列。

示例:推送和弹出任务

-- 推送任务到队列
local queue_key = KEYS[1]
local task = ARGV[1]

redis.call("RPUSH", queue_key, task)
return redis.call("LLEN", queue_key)
redis-cli EVAL "local queue_key = KEYS[1]; local task = ARGV[1]; redis.call('RPUSH', queue_key, task); return redis.call('LLEN', queue_key);" 1 task_queue "task1"
-- 弹出任务
local queue_key = KEYS[1]

local task = redis.call("LPOP", queue_key)
if not task then
    return nil
else
    return task
end
redis-cli EVAL "local queue_key = KEYS[1]; local task = redis.call('LPOP', queue_key); if not task then return nil; else return task; end" 1 task_queue

6. 限流器

实现简单的限流器,用于控制请求频率。

示例:限流脚本

local key = KEYS[1]
local limit = tonumber(ARGV[1])
local interval = tonumber(ARGV[2])

local current = tonumber(redis.call("GET", key) or "0")
if current + 1 > limit then
    return false
else
    redis.call("INCR", key)
    if current == 0 then
        redis.call("EXPIRE", key, interval)
    end
    return true
end
redis-cli EVAL "local key = KEYS[1]; local limit = tonumber(ARGV[1]); local interval = tonumber(ARGV[2]); local current = tonumber(redis.call('GET', key) or '0'); if current + 1 > limit then return false; else redis.call('INCR', key); if current == 0 then redis.call('EXPIRE', key, interval); end; return true; end" 1 rate_limit_key 10 60

总结

Redis 的 Lua 脚本强大且灵活,适用于多种场景。通过合理使用 Lua 脚本,可以确保操作的原子性、减少网络开销和提高系统性能。上述示例涵盖了常见的分布式锁、计数器、事务性操作、排行榜、队列操作和限流器等场景,为这些应用场景提供了高效、可靠的解决方案。

到此这篇关于Redis中Lua脚本的常见场景的文章就介绍到这了,更多相关Redis Lua脚本常见场景内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • redis 数据删除策略和逐出算法的问题小结

    redis 数据删除策略和逐出算法的问题小结

    这篇文章主要介绍了redis 数据删除策略和逐出算法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • 防止redis内存溢出优化方法

    防止redis内存溢出优化方法

    本文主要介绍了防止redis内存溢出优化方法,包括使用maxmemory-policy选项、设置数据过期时间和配置Redis集群等,具有一定的参考价值,感兴趣的可以了解一下
    2025-03-03
  • Redis解决缓存一致性问题

    Redis解决缓存一致性问题

    本文主要介绍了Redis 解决缓存一致性问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-10-10
  • Redis哨兵机制的使用详解

    Redis哨兵机制的使用详解

    文章讲解了Redis哨兵机制的基本原理、主库和从库自动切换的过程、如何减少误判、哨兵集群的组成和通信机制,以及哨兵在故障发生时如何选举Leader进行主从切换
    2025-01-01
  • Redis Hash冲突的10种解决方法

    Redis Hash冲突的10种解决方法

    在高并发的分布式系统中,Redis的Hash表(如Hash类型、数据库键空间)是核心数据结构,但当多个键的哈希值冲突时,性能可能骤降——这是开发者必须直面的挑战,本文将深入Redis源码逻辑,给大家介绍了Redis Hash冲突的10种解决方法,需要的朋友可以参考下
    2025-08-08
  • Redis Desktop Manager(Redis可视化工具)安装及使用图文教程

    Redis Desktop Manager(Redis可视化工具)安装及使用图文教程

    这篇文章主要介绍了Redis Desktop Manager(Redis可视化工具)安装及使用图文教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • Redis高并发缓存设计问题与性能优化

    Redis高并发缓存设计问题与性能优化

    本文详细介绍了Redis缓存设计中常见的问题及解决方案,包括缓存穿透、缓存失效(击穿)、缓存雪崩、热点缓存key重建优化、缓存与数据库双写不一致以及开发规范与性能优化,感兴趣的可以了解一下
    2024-11-11
  • Redis8.0.3编译化安装的实现

    Redis8.0.3编译化安装的实现

    本文主要介绍了Redis8.0.3编译化安装的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-08-08
  • redis命令行查看中文不乱码的方法(十六进制字符串处理)

    redis命令行查看中文不乱码的方法(十六进制字符串处理)

    这篇文章主要给大家介绍了关于redis命令行查看中文不乱码的方法,其中详细介绍了十六进制字符串处理的相关资料,文中给出了详细的示例代码,供大家参考学习,下面随着小编来一起学习学习吧。
    2017-10-10
  • Redis Set 集合的实例详解

    Redis Set 集合的实例详解

    这篇文章主要介绍了 Redis Set 集合的实例详解的相关资料,Redis的Set是string类型的无序集合。集合成员是唯一的,并且不重复,需要的朋友可以参考下
    2017-08-08

最新评论