redis的lua使用(EVALSHA)

redis 127.0.0.1:6379> SCRIPT LOAD "local list=redis.call('KEYS', KEYS[1] .. '*') return (table.getn(list))"
"fdece9fbf98e91fe138eb2fcc5a6f0fbd24c90f9"
redis 127.0.0.1:6379> EVALSHA fdece9fbf98e91fe138eb2fcc5a6f0fbd24c90f9 1 BILL_ACCOUNT_06
(integer) 1640
redis 127.0.0.1:6379>

--------------------------------------------------------------------

功能:多索引记录,set类型存放索引,记录存放在string类型里

SCRIPT LOAD "local list,z,x,y=redis.call('SMEMBERS', KEYS[1]),{} for x,y in ipairs(list) do table.insert(z, redis.call('GET', y)) end return (z)"

EVALSHA 98cf4fd57708c3378215378a16d0f32e2f6c5d40 1 key_Set 

功能:返回key*的数据

SCRIPT LOAD "local list,z,x,y=redis.call('KEYS', KEYS[1] .. '*'),{} for x,y in ipairs(list) do table.insert(z, redis.call('HGETALL', y)) end return (z)"

EVALSHA f2ef5669bef11c60caa5ec8a3139ed3f108540ff 1 U

功能:索引为数值类型的单索引记录集可以放在 Sorted-Sets 类型里,这样也可以用eval进行枚举类的批量数据查询

SCRIPT LOAD "local result,dot,str,i,j,k={},',',KEYS[2],0,0 while true do j=string.find(str, dot,i+1) if nil==j then k=string.sub(str,i+1) table.insert(result,redis.call('zrangebyscore', KEYS[1],k,k)) break end k=string.sub(str,i+1,j-1) table.insert(result,redis.call('zrangebyscore', KEYS[1],k,k)) i=j end return(result)"

EVALSHA f4a12bbcdb685923b04eeab466fa1de751c52c96 2 key_Sorted-Sets  " ,分割的枚举集合"

功能:索引为数值类型的单索引记录集可以放在 Sorted-Sets 类型里,这样也可以用eval进行枚举类的批量数据更新

SCRIPT LOAD "local result,dot,str,i,j,k={},',',KEYS[3],0,0 while true do j=string.find(str, dot,i+1) if nil==j then k=string.sub(str,i+1) table.insert(result,redis.call('zadd', KEYS[1],KEYS[2],k)) break end k=string.sub(str,i+1,j-1) table.insert(result,redis.call('zadd', KEYS[1],KEYS[2],k)) i=j end return(result)"

EVALSHA e9992e80eb06b78433b80ba207d45486c49e690a 3 key_Sorted-Sets  score  " ,分割的枚举集合"

--------------------

SCRIPT LOAD "local result,dot,sKey,sField,iF,jF,sValue,iV,jV={},',',KEYS[1],KEYS[2],0,0,KEYS[3],0,0 while true do jF=string.find(sField, dot,iF+1) jV=string.find(sValue, dot,iV+1) if nil==jF then  table.insert(result,redis.call('HSET', sKey,string.sub(sField,iF+1),string.sub(sValue,iV+1))) break end  table.insert(result,redis.call('HSET', sKey,string.sub(sField,iF+1,jF-1),string.sub(sValue,iV+1,jV-1))) iF=jF iV=jV end return(result)"
EVALSHA f65b5d677cdfdf9fd9a4ec5cf3e9a4fd4a187dc9 3  UUUU 221,222,223 331,332,333

SCRIPT LOAD "local result,dot,sPrefix,sKey,iK,jK,sField,iF,jF,sValue,iV,jV={},',',KEYS[1],KEYS[2],0,0,KEYS[3],0,0,KEYS[4],0,0 while true do jK=string.find(sKey, dot,iK+1) jF=string.find(sField, dot,iF+1) jV=string.find(sValue, dot,iV+1) if nil==jF then  table.insert(result,redis.call('HSET', sPrefix .. string.sub(sKey,iK+1),string.sub(sField,iF+1),string.sub(sValue,iV+1))) break end  table.insert(result,redis.call('HSET', sPrefix .. string.sub(sKey,iK+1,jK-1),string.sub(sField,iF+1,jF-1),string.sub(sValue,iV+1,jV-1))) iK=jK iF=jF iV=jV end return(result)"
EVALSHA d965a30e6524a96b070b3a7ed92ea2b7ff3c2dcd 4 AAAA 111,112,113 221,222,223 331,332,333

对照:

SCRIPT LOAD "local result,dot,dotV,sKey,sField,iF,jF,sValue,iV,jV={},',',',,,,',KEYS[1],KEYS[2],0,0,KEYS[3],-3,0 while true do jF=string.find(sField, dot,iF+1) jV=string.find(sValue, dotV,iV+4) if nil==jF then  table.insert(result,redis.call('HSET', sKey,string.sub(sField,iF+1),string.sub(sValue,iV+4))) break end  table.insert(result,redis.call('HSET', sKey,string.sub(sField,iF+1,jF-1),string.sub(sValue,iV+4,jV-1))) iF=jF iV=jV end return(result)"
EVALSHA eb4c19bfec0498da4901f374c30b7f1b18aacf7a 3  UUUU 221,222,223 331,,,,332,,,,333

SCRIPT LOAD "local result,dot,dotV,sPrefix,sKey,iK,jK,sField,iF,jF,sValue,iV,jV={},',',',,,,',KEYS[1],KEYS[2],0,0,KEYS[3],0,0,KEYS[4],-3,0 while true do jK=string.find(sKey, dot,iK+1) jF=string.find(sField, dot,iF+1) jV=string.find(sValue, dotV,iV+4) if nil==jF then  table.insert(result,redis.call('HSET', sPrefix .. string.sub(sKey,iK+1),string.sub(sField,iF+1),string.sub(sValue,iV+4))) break end  table.insert(result,redis.call('HSET', sPrefix .. string.sub(sKey,iK+1,jK-1),string.sub(sField,iF+1,jF-1),string.sub(sValue,iV+4,jV-1))) iK=jK iF=jF iV=jV end return(result)"
EVALSHA 65e1e8efb1ec004b027cbfb0ba56bb7b27c83479 4 AAAA 111,112,113 221,222,223 331,,,,332,,,,333

--------------------

功能:索引为数值类型的单索引记录集可以放在 Sorted-Sets 类型里,这样也可以用eval进行枚举类的批量数据先查询是否存在,存在任一个则返回,均不存在则进行更新

SCRIPT LOAD "local result,dot,str,i,j,k,n={},',',KEYS[3],0,0,nil,0 while true do j=string.find(str, dot,i+1) if nil==j then if redis.call('zscore', KEYS[1],string.sub(str,i+1))  then n=n+1 end break end if redis.call('zscore', KEYS[1],string.sub(str,i+1,j-1)) then n=n+1 end i=j end if 0~=n then return(n) end i,j=0,0 while true do j=string.find(str, dot,i+1) if nil==j then k=string.sub(str,i+1) table.insert(result,redis.call('zadd', KEYS[1],KEYS[2],k)) break end k=string.sub(str,i+1,j-1) table.insert(result,redis.call('zadd', KEYS[1],KEYS[2],k)) i=j end return(result)"

EVALSHA  5aaecb5a2a17068f78400767a50863f85318c79d 3 key_Sorted-Sets  score  " ,分割的枚举集合"

上一篇:将文件导入到SQL server数据库表中的字段中


下一篇:本塔科技借力VR Engine,提供更优质的3D VR内容