我看到了这篇关于如何在redis上运行lua脚本的文章。但是这篇文章主要面向*nix用户。那么我该如何在Windows环境下执行redis lua脚本呢?
先阅读:
经过将近一周的尝试和调试,我决定尝试使用其中一个Java库代替脚本来完成编程。我创建了一个公共存储库,并将该项目放在里面。优点是您不受限于 ~8000 字符的输入变量,并且它运行得更快。我将保留批处理脚本,以供那些绝对需要以这种方式进行的人使用,但我强烈建议改用Java代码:
实际答案:
使用批处理文件,我能够复制那篇文章中的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
call redis-cli -h %4 -p %5 --eval %3 !keyString:~1! , %2 > result.txt
'%2'将字段值传递到Lua脚本的ARGV数组中,如果不需要,可以将其删除。您也可以根据需要添加其他ARGV参数。