如何在Windows系统中运行Redis Lua脚本

3

我看到了这篇关于如何在redis上运行lua脚本的文章。但是这篇文章主要面向*nix用户。那么我该如何在Windows环境下执行redis lua脚本呢?

1个回答

1

先阅读:

经过将近一周的尝试和调试,我决定尝试使用其中一个Java库代替脚本来完成编程。我创建了一个公共存储库,并将该项目放在里面。优点是您不受限于 ~8000 字符的输入变量,并且它运行得更快。我将保留批处理脚本,以供那些绝对需要以这种方式进行的人使用,但我强烈建议改用Java代码:

Redis脚本项目

实际答案:

使用批处理文件,我能够复制那篇文章中的bash脚本。

@echo off
setlocal EnableDelayedExpansion

echo Starting removal of keys from redis. 
echo KeyMatch: %1 
echo Field: %2
echo Script: %3
echo Host: %4
echo Port: %5

REM set the cursor to 0 to begin iterating over matching keys
set cursor=0

:loop
REM call redis scan and output the result to temp.txt
call redis-cli -h %4 -p %5 scan !cursor! match %1 count 180 > temp.txt

REM set the first line of the temp file to the new cursor variable
set /p cursor=<temp.txt

REM outer loop variables
set /A i=0
set keyString=

REM loop through the text file to build the key string
for /F "usebackq delims=" %%a in ("temp.txt") do (
    set /A i+=1
    REM if we are not on the first line save the key to a space delimted string
    if NOT !i! == 1 (
        call set keyString=!keyString! %%a
    )
)

rem if there is only one line in the file skip the script execution
if !i! LEQ 1 (
    goto :checkCursor
)

rem check that the length of keyString will not likely violate the 8192 character limit to command line calls
ECHO !keyString!> strlength.txt
FOR %%? IN (strlength.txt) DO ( SET /A strlength=%%~z? - 2 )
if !strlength! GTR 8000 (
    echo.
    echo.
    echo ****Error processing script. Key string is too long. Reduce the count in call to scan.****
    echo.
    echo.
    GOTO :end
)

REM call the script with the keys from the scan task, output to result.txt to prevent writing to the command line each iteration.   
call redis-cli -h %4 -p %5 --eval %3 !keyString:~1! , %2 > result.txt

REM output '.' to the commandline to signify progress
<nul set /p=.

:checkCursor
if not !cursor!==0 (
    goto :loop
)

:end
set fileToDelete=temp.txt
if exist !fileToDelete! del /F !fileToDelete!
set fileToDelete=result.txt
if exist !fileToDelete! del /F !fileToDelete!
set fileToDelete=strlength.txt
if exist !fileToDelete! del /F !fileToDelete!

echo Completed script execution
endlocal

你可以从命令行调用此脚本,例如:
batchScriptName keyMatch field luaScriptName.lua host port
batchScriptName myKey* us luaScriptName.lua localhost 6379

如果您的批处理脚本不在路径上,则需要从文件所在的目录中调用命令。对于lua脚本,您还需要提供完整的文件路径引用或从lua脚本所在的目录调用批处理脚本。此脚本设置为使用redis中的哈希值。如果您需要更改它,您可能需要更改此行:
call redis-cli -h %4 -p %5 --eval %3 !keyString:~1! , %2 > result.txt

'%2'将字段值传递到Lua脚本的ARGV数组中,如果不需要,可以将其删除。您也可以根据需要添加其他ARGV参数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接