1. Redis built-in Lua Script
Redis支持內建持久化的Lua腳本的執行並返回結果。使用"SCRIPT LOAD"命令將得到此腳本的SHA1 ID.
redis-cli SCRIPT LOAD "$(cat ./myexample.lua)
使用"EVALSHA"命令傳入SHA1,KEY和參數便能執行此腳本並返回結果。
“SCRIPT LOAD”詳見http://redis.io/commands/script-load“Load a script into the scripts cache, without executing it. After the specified command is loaded into the script cache it will be callable using EVALSHA with the correct SHA1 digest of the script, exactly like after the first successful invocation of EVAL.“EVALSHA”詳見http://redis.io/commands/evalsha
The script is guaranteed to stay in the script cache forever (unless SCRIPT FLUSH is called).
The command works in the same way even if the script was already present in the script cache.
Please refer to the EVAL documentation for detailed information about Redis Lua scripting.
Return value
Bulk string reply This command returns the SHA1 digest of the script added into the script cache.”
“Evaluates a script cached on the server side by its SHA1 digest. Scripts are cached on the server side using the SCRIPT LOAD command. The command is otherwise identical to EVAL.”例子:
[root@local redis-3.0.3]# redis-cli -h 10.64.70.10 SCRIPT LOAD "$(cat /tmp/example.lua)"
"05ee2d487091f65689075426914f42336d1695f5"
[root@local redis-3.0.3]# redis-cli -h 10.64.70.10
10.64.70.10:6379> SCRIPT EXISTS 05ee2d487091f65689075426914f42336d1695f5
1) (integer) 1
10.64.70.10:6379> evalsha 05ee2d487091f65689075426914f42336d1695f5 3 domain:hi.com:oratelimit domain:hi.com:otraffic domain:hi.com:osetting 1 1447115363 5cbcf04a-8742-11e5-ae2e-000c290753a5
"1"
本例子中evalsha傳入3個KEY:domain:hi.com:oratelimit domain:hi.com:otraffic domain:hi.com:osetting以及3個參數:1 1447115363 5cbcf04a-8742-11e5-ae2e-000c290753a5,最終返回結果“1”.
2. Redis Perl接口
目前Redis已經有完善的編程接口可用,詳見http://search.cpan.org/~dams/Redis/
Centos 6.3安裝包:yum -y install perl-Redis
以Redis Sentinel看守的1 Master 2 Slave的部署爲例:
連接/斷開redis master的示例代碼#!/usr/bin/perl
use Redis::Sentinel;
$redis_master = undef;
@sentinel_servers = ( '10.64.70.10:26379', '10.64.70.20:26379', '10.64.70.30:26379' );
sub conn_redis()
{
my $sentinel = undef;
foreach my $s (@sentinel_servers) {
$sentinel = Redis::Sentinel->new(
server => $s,
sentinels_cnx_timeout => 0.1,
sentinels_read_timeout => 1,
sentinels_write_timeout => 1,
);
if ($sentinel) {
my @redis_list = $sentinel->get_masters;
foreach my $h (@redis_list) {
if ( $h->{'role-reported'} eq 'master' ) {
my $redis_server = $h->{ip}.':'.$h->{port};
$redis_master = Redis->new(
server => $redis_server,
cnx_timeout => 0.1,
read_timeout => 1,
write_timeout => 1,
);
if ($redis_master) {
$redis_ok = 1;
return 1;
}
}
}
}
}
$redis_ok = 0;
return 0;
}
sub disconn_redis()
{
if ($redis_master) {
$redis_master->quit();
}
}
sync配置到Redis的示例代碼sub sync_to_redis() {
debug_print((caller(0))[3].">\n") if ($debug);
if ($redis_master) {
try {
eval {
if (@delete_records) {
foreach my $k (@delete_records) {
$redis_master->del("domain:$k->[1]:osetting");
}
}
if (@new_records) {
foreach my $k (@new_records) {
$redis_master->hmset( "domain:$k->[1]:osetting", "window", $k->[3], "threshold", $k->[4], "interval", $k->[5] );
}
}
if (@update_records) {
foreach my $k (@update_records) {
$redis_master->hmset( "domain:$k->[1]:osetting", "window", $k->[3], "threshold", $k->[4], "interval", $k->[5] );
}
}
};
if ($@) {
my $err = $@;
#notify_alert("Warning: Failed to sync setting to Redis", "$host: Failed to sync setting to Redis, Exception: $err");
debug_print((caller(0))[3] . "> Failed to sync setting to Redis, Exception: $err\n");
disconn_redis();
return 0;
}
}
catch Error with {
my $ex = shift;
notify_alert( "Warning: Redis Problem", "$host: $ex->{-line} $ex->{-text}" );
return 0;
}
}
return 1;
}
調用built-in lua script的示例代碼 my $rc = $redis_master->evalsha( $lua_sha1, 3, $key1, $key2, $key3, $dir, $epoch, $uuid );