【问题标题】:How can I run redis lua scripts in windows如何在 Windows 中运行 redis lua 脚本
【发布时间】:2016-08-10 12:28:18
【问题描述】:

我遇到了this 文章,关于如何针对 redis 运行 lua 脚本。但是这篇文章是针对那些在 *nix 上运行的。如何在 windows 环境下执行 redis lua 脚本?

【问题讨论】:

    标签: windows lua redis


    【解决方案1】:

    请先阅读:

    在这样做并与这个脚本斗争了将近一周之后,我决定尝试使用其中一个 java 库来编写脚本。我已经创建了一个包含该项目的公共仓库。好处是您不仅限于约 8000 个字符的输入变量,而且运行速度要快得多。我将把批处理脚本留给那些绝对需要这样做的人,但我强烈建议使用 java 代码:

    Redis Scripting Project

    实际答案:

    使用批处理文件,我能够复制那篇文章中的 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 参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-04
      • 2017-11-24
      • 2016-06-19
      • 2016-03-29
      • 2013-05-17
      • 2013-04-28
      • 2017-09-26
      • 1970-01-01
      相关资源
      最近更新 更多