2016-04-18 9 views
3

redisに対してluaスクリプトを実行する方法については、thisの記事を参照してください。しかし、この記事は、* nix上で動作するものに対応しています。 Windows環境からredis luaスクリプトを実行するにはどうすればよいですか?Windowsでredis luaスクリプトを実行するには

答えて

1

読む:まず

は、これを行うと、ほぼ一週間のために、このスクリプトで戦った後、私の代わりにスクリプトを実行するためにJavaライブラリのいずれかを試してみて、使用することを決めました。私は、そのプロジェクトで公開リポジトリを作成しました。利点は、〜8000文字の入力変数に限定されず、はるかに高速に動作することです。私は絶対に、このようにそれを行うために必要とする人々のために、ここでバッチスクリプトを残すつもりですが、私は非常代わりにJavaコードを使用することをお勧めします:

Redis Scripting Project

実際の回答:

をバッチファイルを使って、私はその記事からbashスクリプトを複製することができました。あなたのバッチスクリプトがパス上にない場合、あなたがあなたのファイルがあるディレクトリからコマンドを呼び出す必要があります

batchScriptName keyMatch field luaScriptName.lua host port 
batchScriptName myKey* us luaScriptName.lua localhost 6379 

@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 

次のようなコマンドラインからこのスクリプトを呼び出すことができます。また、luaスクリプトでは、完全なファイルパス参照を与えるか、luaスクリプトが置かれているディレクトリからバッチスクリプトを呼び出す必要があります。

このスクリプトは、redisでハッシュ値を使用するように設定されています。あなたはそれを変更する必要がある場合、あなたはおそらく、この行を変更したくなるでしょう:

call redis-cli -h %4 -p %5 --eval %3 !keyString:~1! , %2 > result.txt 

「%2」のluaスクリプトでARGV配列にフィールド値に渡し、そうでない場合は、これを削除することができますそれが必要。必要に応じて、追加のARGVパラメータを追加することもできます。

関連する問題