lua script

Lua is a small scripting language written in standard C. It is designed to be embedded in applications, so as to provide flexible extensions and customization functions for applications. The combination of Lua and redis can also deal with high concurrency and other services.

1.Lua syntax
Lua is a case-sensitive language.
There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, table. The Nil type has only one value, nil, its main purpose Used to identify the difference between table identification and any other value.
– and –[[]]– are comments

1.1 Control statement
if else statement
if a==1 and b==1 then
elseif c>1 or d~=1 then
else
end

while循环
a=0
while a<= 100 do end for循环 for a = 0,100,2 do end until循环 a = 2 repeat print(a) until a>100

1、2函数
aaa = function(a) return a^2 end
function aaa(a)
if a < 2 then return 1 end return aaa(n - 2) + aaa(n - 1) end

1、3Table
aaa = {name=”hello”,age=11,handsome=True}
aaa.name = “hello”
aaa.age = 11
aaa.handsome = True

1.4 Global variable
_G.aaa
_G[“aaa”]
for k, v in pairs(t) do
print(k, v)
end

1. The module 5
directly uses require("model_name") to load other lua files

official website Chinese documents http://manual.luaer.cn/

2. Redis application Lua
document address https://www.github.com/phpredis/phpredis
Since redis eval and lua run in queues, they can withstand high concurrency problems, and requests can also be combined into one, reducing communication between servers.
Columns such as:
$unique_redis_rand = <<<_ --get how many times the lottery has been drawn local now_num = tonumber(redis.call("hget","lotterycount_$event_id","$rand_con_id")) --get the number of lottery draws in a day local now_num_day = tonumber(redis.call("hget","lotterycount_$event_id","$rand_con_id_day")) -- get the number of personal draws local now_num_user = tonumber(redis.call("hget","lotterycount_$event_id"," $userid")) -- judge whether to start if not now_num then now_num = 0 end -- if there is no one-day limit, the default is 0 if not now_num_day then now_num_day = 0 end -- judge whether the lottery exceeds the total limit if now_num >= $rand_con_total then
return 0
– judge whether the lottery exceeds the daily limit
elseif now_num>



return 0
– determine whether the user is still eligible
elseif now_num_user <= 0 return 0 else – subtract the number of users – this can also be added to the redis queue to take out the gift package and put it in the user attribute, or other operations return {1, redis .call("hincrby","lotterycount_$event_id","$userid","-1")} end _; $return = $redis->eval($unique_redis_rand);

//Anti-IP brush Lua script form
$date_mobile = "13200000000";
$date_json = "hello word";
$user_ip = '127.0.0.1';
$date_cname = 'test';

$key_ip = ‘gus_ban_’.$user_ip;
$key_mobile = ‘gus_mobile_’.$date_cname;
$key_con = ‘gus_link’;
//lua start
$lua_cont = <<<_ local ip_num = redis.call("incr",KEYS[1]) if ip_num >= 100 then
return 2
end
if ip_num == 1 then
redis.call(“expire”,KEYS[1],”60″)
end
local mobile_not = redis.call(“sismember”,KEYS[2],ARGV[1])
if mobile_not == 0 then
redis.call(“sadd”,KEYS[2],ARGV[1])
redis.call(“lpush”,KEYS[3],ARGV[2])
return 1
else
return 3
end
_
;
$return = Redis::eval($lua_cont,3,$key_ip,$key_mobile,$key_con,$date_mobile,$date_json);
//lua end

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326028330&siteId=291194637