redis lua原理分析

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
云原生多模数据库 Lindorm,多引擎 多规格 0-4节点
简介:

redis-2.6支持通过EVAL命令来执行lua脚本,对lua脚本的支持扩展了redis的应用场景,redis支持路脚本需要做2件事

  1. redis能执行lua脚本
  2. 在lua脚本里能执行redis的命令

接下来,我将通过一个简单的实例来解析redis如何完成上述两个工作的。

构建一个简单的redis

#define DICT_SIZE 100
struct redisDict {
  char* key[DICT_SIZE];
  char* value[DICT_SIZE];
  int  idx;
};

static void setCommand(const char *key, const char *value)
{
    /* ignore memory issue for simple */
    if (dict.idx + 1 <= DICT_SIZE) {
    dict.key[dict.idx] = (char *)malloc(strlen(key) + 1);
    strcpy(dict.key[dict.idx], key);

    dict.value[dict.idx] = (char *)malloc(strlen(value) + 1);
    strcpy(dict.value[dict.idx], value);

    dict.idx += 1;
  }
}

static const char *getCommand(const char *key)
{
  int j;
  for (j = 0; j <= dict.idx; j++) {
    if (strcmp(dict.key[j], key) == 0) {
      return dict.value[j];
    }
  }
  return "KeyNotFound";
}

上述代码实现了一个伪redis,支持setCommand、getCommand。

C调用lua脚本

具体例子参考http://lua-users.org/wiki/SimpleLuaApiExample

/*
 * All Lua contexts are held in this structure. We work with it almost
 * all the time.
 */
lua_State *L = luaL_newstate();

luaL_openlibs(L); /* Load Lua libraries */

/* Load the file containing the script we are going to run */
status = luaL_loadfile(L, "script.lua");

/* Ask Lua to run our little script */
result = lua_pcall(L, 0, LUA_MULTRET, 0);

上述代码片段中,其中script.lua是一个lua脚本。redis里稍有不同,redis里的脚本是通过EVAL命令传递到服务器端,redis将脚本拼成一个lua函数,然后调用loadbuffer,而这里从文件执行脚本调用的loadfile。

lua调用C函数

下面的lua代码里,调用的是redis的setCommand和getCommand。

redis.call("set", "foo", "bar");

return redis.call("get", "foo");

要想lua脚本能调用C代码,需要现在lua环境注册对应的C函数,参考redis的scriptingInit函数。

static int call(lua_State *L)
{
  int argc = lua_gettop(L);
  const char *cmd = lua_tostring(L, 1);
  const char *key = lua_tostring(L, 2);
  if (strcmp(cmd, "set") == 0) {
    assert(argc == 3);
    const char *value = lua_tostring(L, 3);
    setCommand(key, value);
   return 0;
  } else if (strcmp(cmd, "get") == 0) {
    assert(argc == 2);
    lua_pushstring(L, getCommand(key));
    return 1;
  }

  lua_pushstring(L, "Invalid Command");
  return 1;
}

static void scriptingInit()
{

  L = luaL_newstate();

  luaL_openlibs(L);

  /* Register the redis commands table and fields */
  lua_newtable(L);

  /* redis.call */
  lua_pushstring(L, "call");
  lua_pushcfunction(L, call);
  lua_settable(L, -3);

  /* Finally set the table as 'redis' global var. */
  lua_setglobal(L, "redis");
}

完整示例代码

 1
 2
 
         
redis . call ( " set" , " foo" , " bar" );
return redis . call ( " get" , " foo" );
 来自CODE的代码片
script.lua

   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
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 
         
/* gcc -o test_lua lua.c -llua -lm -ldl */
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#define DICT_SIZE 100
struct redisDict {
char * key [ DICT_SIZE ];
char * value [ DICT_SIZE ];
int idx ;
};

struct redisDict dict ;
lua_State * L ;

static void setCommand ( const char * key , const char * value )
{
/* ignore memory issue for simple */
if ( dict . idx + 1 <= DICT_SIZE ) {
dict . key [ dict . idx ] = ( char * ) malloc ( strlen ( key ) + 1 );
strcpy ( dict . key [ dict . idx ], key );

dict . value [ dict . idx ] = ( char * ) malloc ( strlen ( value ) + 1 );
strcpy ( dict . value [ dict . idx ], value );

dict . idx += 1 ;
}
}

static const char * getCommand ( const char * key )
{
int j ;
for ( j = 0 ; j <= dict . idx ; j ++ ) {
if ( strcmp ( dict . key [ j ], key ) == 0 ) {
return dict . value [ j ];
}
}
return "KeyNotFound" ;
}

static int call ( lua_State * L )
{
int argc = lua_gettop ( L );
const char * cmd = lua_tostring ( L , 1 );
const char * key = lua_tostring ( L , 2 );
if ( strcmp ( cmd , "set" ) == 0 ) {
assert ( argc == 3 );
const char * value = lua_tostring ( L , 3 );
setCommand ( key , value );
return 0 ;
} else if ( strcmp ( cmd , "get" ) == 0 ) {
assert ( argc == 2 );
lua_pushstring ( L , getCommand ( key ));
return 1 ;
}

lua_pushstring ( L , "Invalid Command" );
return 1 ;
}

static void scriptingRun ( const char * filename )
{
int status = luaL_loadfile ( L , filename );
if ( status ) {
fprintf ( stderr , "Couldn't load file: %s \n " , lua_tostring ( L , - 1 ));
exit ( 1 );
}

/* Ask Lua to run our little script */
int result = lua_pcall ( L , 0 , LUA_MULTRET , 0 );
if ( result ) {
fprintf ( stderr , "Failed to run script: %s \n " , lua_tostring ( L , - 1 ));
exit ( 1 );
}

/* Get the returned value at the top of the stack (index -1) */
const char * value = lua_tostring ( L , - 1 );

printf ( "%s \n " , value );

lua_pop ( L , 1 ); /* Take the returned value out of the stack */
lua_close ( L ); /* Cya, Lua */
}

static void scriptingInit ()
{
/*
* All Lua contexts are held in this structure. We work with it almost
* all the time.
*/
L = luaL_newstate ();

/* Load Lua libraries */
luaL_openlibs ( L );

/* Register the redis commands table and fields */
lua_newtable ( L );

/* redis.call */
lua_pushstring ( L , "call" );
lua_pushcfunction ( L , call );
lua_settable ( L , - 3 );

/* Finally set the table as 'redis' global var. */
lua_setglobal ( L , "redis" );
}

int main ( void )
{
scriptingInit ();
scriptingRun ( "script.lua" );
return 0 ;
}
 来自CODE的代码片
lua.c

相关文章
|
存储 缓存 NoSQL
Redis 服务器全方位介绍:从入门到核心原理
Redis是一款高性能内存键值数据库,支持字符串、哈希、列表等多种数据结构,广泛用于缓存、会话存储、排行榜及消息队列。其单线程事件循环架构保障高并发与低延迟,结合RDB和AOF持久化机制兼顾性能与数据安全。通过主从复制、哨兵及集群模式实现高可用与横向扩展,适用于现代应用的多样化场景。合理配置与优化可显著提升系统性能与稳定性。
231 0
|
1月前
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
305 5
|
2月前
|
存储 缓存 监控
Redis分区的核心原理与应用实践
Redis分区通过将数据分散存储于多个节点,提升系统处理高并发与大规模数据的能力。本文详解分区原理、策略及应用实践,涵盖哈希、范围、一致性哈希等分片方式,分析其适用场景与性能优势,并探讨电商秒杀、物联网等典型用例,为构建高性能、可扩展的Redis集群提供参考。
154 0
|
5月前
|
缓存 NoSQL 算法
高并发秒杀系统实战(Redis+Lua分布式锁防超卖与库存扣减优化)
秒杀系统面临瞬时高并发、资源竞争和数据一致性挑战。传统方案如数据库锁或应用层锁存在性能瓶颈或分布式问题,而基于Redis的分布式锁与Lua脚本原子操作成为高效解决方案。通过Redis的`SETNX`实现分布式锁,结合Lua脚本完成库存扣减,确保操作原子性并大幅提升性能(QPS从120提升至8,200)。此外,分段库存策略、多级限流及服务降级机制进一步优化系统稳定性。最佳实践包括分层防控、黄金扣减法则与容灾设计,强调根据业务特性灵活组合技术手段以应对高并发场景。
1493 7
|
9月前
|
消息中间件 缓存 NoSQL
Redis原理—5.性能和使用总结
本文详细探讨了Redis的阻塞原因、性能优化、缓存相关问题及数据库与缓存的一致性问题。同时还列举了不同缓存操作方案下的并发情况,帮助读者理解并选择合适的缓存管理策略。最终得出结论,在实际应用中应尽量采用“先更新数据库再删除缓存”的方案,并结合异步重试机制来保证数据的一致性和系统的高性能。
Redis原理—5.性能和使用总结
|
9月前
|
NoSQL 算法 安全
Redis原理—1.Redis数据结构
本文介绍了Redis 的主要数据结构及应用。
Redis原理—1.Redis数据结构
|
9月前
|
缓存 NoSQL Redis
Redis原理—2.单机数据库的实现
本文概述了Redis数据库的核心结构和操作机制。
Redis原理—2.单机数据库的实现
|
9月前
|
存储 缓存 NoSQL
Redis原理—4.核心原理摘要
Redis 是一个基于内存的高性能NoSQL数据库,支持分布式集群和持久化。其网络通信模型采用多路复用监听与文件事件机制,通过单线程串行化处理大量并发请求,确保高效运行。本文主要简单介绍了 Redis 的核心特性。
|
9月前
|
缓存 NoSQL Redis
Redis原理—3.复制、哨兵和集群
详细介绍了Redis的复制原理、哨兵原理和集群原理。
|
9月前
|
运维 NoSQL 算法
【📕分布式锁通关指南 04】redis分布式锁的细节问题以及RedLock算法原理
本文深入探讨了基于Redis实现分布式锁时遇到的细节问题及解决方案。首先,针对锁续期问题,提出了通过独立服务、获取锁进程自己续期和异步线程三种方式,并详细介绍了如何利用Lua脚本和守护线程实现自动续期。接着,解决了锁阻塞问题,引入了带超时时间的`tryLock`机制,确保在高并发场景下不会无限等待锁。最后,作为知识扩展,讲解了RedLock算法原理及其在实际业务中的局限性。文章强调,在并发量不高的场景中手写分布式锁可行,但推荐使用更成熟的Redisson框架来实现分布式锁,以保证系统的稳定性和可靠性。
530 0
【📕分布式锁通关指南 04】redis分布式锁的细节问题以及RedLock算法原理

相关产品

  • 云数据库 Tair(兼容 Redis)