您的位置:首页 > 数据库 > Redis

一个Lua脚本操作Redis的简单例子

2017-08-10 17:53 826 查看
本例子的lua脚本实现 遍历 Redis中指定模式的键,然后替换每个键中与模式匹配的值,使之变为指定的值。

Redis中的键如下所示:

127.0.0.1:6379> keys *
1) "string.tmd.2"
2) "test"
3) "string.tmd.1"


Redis 键对应的值为: 很明显客户端获取到的是一个序列化了的String 值,但是avata字段http连接依然呈现规则。

get "string.tmd.1"


"{\"bad\":106,\"comment\":{\"avata\":\"http://tmd2.ghost.com/comment/images/20.png\",
\"bc\":\"\xe4\xb8\x8d\xe5\x8f\xaf\xe8\x83\xbd\xef\xbc\x81\xef\xbc\x81\xef\xbc\x81\xe6\x88\x91\xe4\xb8\x80\xe8\x88\xac\xe7\x9d\xa1\xe8\xa7\x89\xe6\x97\xb6\xe9\x83\xbd\xe4\xbc\x9a\xe6\x8a\x8a\xe6\xb0\x94\xe6\x94\xbe\xe4\xba\x86\xe7\x9a\x84\",
\"nick\":\"\xe5\x8f\xb6\xe9\x9a\x8f\xe9\x9b\xa8\xe8\x90\xbda8\",
siz\"total\":7},
\"gif\":\"2498/1498187979040_39bcc0ad-737b-4fe9-8c59-4dd6318d64f0.gif\",
good\":768,\"height\":174,\"id\":1,\"img\":\"2498/1498187979040_39bcc0ad-737b-4fe9-8c59-4dd6318d64f0.jpg\",
\"ra\":\"1/1.jpg\",
\"rn\":\"\xe5\x85\xb0\xe6\xba\xaa\",
\"src\":\"http://neihanshequ.com/p62324851262/\",
\"title\":\"\xe8\xbf\x99\xe5\xb0\xb1\xe6\x98\xaf\xe4\xbd\xa0\xe7\x9d\xa1\xe8\xa7\x89\xe6\x97\xb6\xe5\x80\x99\xe4\xbd\xa0\xe5\xa5\xb3\xe6\x9c\x8b\xe5\x8f\x8b\xe7\x9a\x84\xe5\x8f\x8d\xe5\xba\x94\xe3\x80\x82\",
\"type\":2,
\"weight\":0,
\"width\":310}"


现在我们要将 avata 字段里面的连接 替换为另一种模式的链接 。

Lua脚本如下: –表示注释; myKeyList用于获取所有的指定模式的键值, 即为上面的

“string.tmd.1”

“string.tmd.2”。

–获取到的数据为一个table类型 即数组

local myKeyList=redis.call("keys","string.tmd.*")
local count=0
local tempStrlocal str  --experience all the keys 遍历数组 有多种方法遍历数组 网上很容易找到
for index,line in pairs(myKeyList)
do  --通过Redis API调用 获取line的值 line即为键
str=redis.call("get",line)
--string.sub() match() gmatch() len() 对字符串操作的函数
local matchStr=string.match(str,"http://tmd2.ghost.com/comment/images/%d+.png") --匹配到http链接
--下面注释掉的函数可以在"服务器"端打印变量的值
print(matchStr)
local reserveStr=matchStr.sub(matchStr,40,string.len(matchStr)); --将链接截断,并替换成指定的模式
--print(reserveStr)
-- .. 用于连接字符串 不像java中用 + 此处替换 str为源 中间为模式 右边为目标
tempStr=string.gsub(str,"http://tmd2.ghost.com/comment/images/%d+.png","http://tmd2-img.ghost.com/1/"..reserveStr)
--print(tempStr) --替换完后将之重新设置到Redis中 redis.call("set",line,tempStr)
--print(redis.call("get",line))
end
--这个返回值在客户端可以打印出来
return "Success"


Lua脚本搞完后 用eval 或者evalsha 执行

redis-cli --eval changeAvataLua


服务器输出如下:

{"bad":106,"comment":{"avata":"http://tmd2-img.ghost.com/1/20.png",
"bc":"不可能!!!我一般睡觉时都会把气放了的","nick":"叶随雨落a8","total":7},
"gif":"2498/1498187979040_39bcc0ad-737b-4fe9-8c59-4dd6318d64f0.gif",
"good":768,"height":174,"id":1,"img":"2498/1498187979040_39bcc0ad-737b-4fe9-8c59-4dd6318d64f0.jpg",
"ra":"1/1.jpg",
"rn":"兰溪",
"src":"http://neihanshequ.com/p62324851262/",
"title":"这就是你睡觉时候你女朋友的反应。",
"type":2,
"weight":0,
"width":310}


以下Lua脚本则反向替换上面的功能。

local myKeyList=redis.call("keys","string.joke.*")
local count=0
local tempStrlocal str
--experience all the keys
for index,line in pairs(myKeyList)
do
str=redis.call("get",line)
local matchStr=string.match(str,"http://tmd2\\--img.ghost.com/1/%d+.png")
print(matchStr)
local reserveStr=matchStr.sub(matchStr,31,string.len(matchStr));
print(reserveStr)
tempStr=string.gsub(str, "http://tmd2\\--
img.ghost.com/1/%d+.png","http://tmd2.ghost.com/comment/images/"..reserveStr)
print(tempStr)
redis.call("set",line,tempStr)
end
return "Success"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lua redis 脚本 遍历