当前位置: 首页 > 工具软件 > perl-redis > 使用案例 >

Redis编程--Perl接口及内建Lua脚本应用

郎德馨
2023-12-01

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.
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.”
“EVALSHA”详见http://redis.io/commands/evalsha
“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 );


 类似资料: