从stackexchange.redis运行时出现Lua脚本错误

时间:2016-02-12 07:42:16

标签: lua redis stackexchange.redis

我们有一个冗长的lua脚本。请在问题描述后找到它。

如果所有3个参数匹配(UserName,UserCategory,UserCategoryItem),脚本的逻辑是返回true / false

脚本的第一部分拆分RedisKeys以获取UserName,UserCategory,UserCategoryItem。还有一个随机函数来测试“函数”是否在Redis lua脚本中有效。它有效。

脚本的第二部分将此值与数据库中的值进行比较。这些值在脚本中是硬编码的。如果所有值都匹配,则返回true,否则返回false。

单独运行时,这两个脚本运行完美。但是他们在一起跑时会抛出一个错误。

ISSUE /问题

在IDE中运行时,所有三个脚本都可以完美运行。我使用SciTE IDE(适用于Windows环境的Lua IDE)

从Stackexchange.Redis客户端运行时,脚本会产生问题。

脚本的前两部分(第一部分和第二部分)完全从Stackexchange.Redis运行。只有当它们在第三个脚本(第三部分)中合并时才会引发错误。 由于错误本质上是通用的,因此我无法进一步调查此问题。

我将脚本分为三部分。最后的脚本结合了前两个脚本。

var redis = ConnectionMultiplexer.Connect("localhost");
var db = redis.GetDatabase();

//PART ONE OF THE SCRIPT

            var doesExist1 = (string)db.ScriptEvaluate(@" 
 local function isDateInRange(DateStart, DateEnd, GivenDate)
    if (GivenDate > DateStart and GivenDate < DateEnd)
    then
        return(true)
    else
        return(false)
    end
end

--Fetch User's Category's StartDate and EndDate
local function IsCategoryDateValid()
local givenDate, DateStart, DateEnd

GivenDate = '2017-01-09'
StartDate = '2015-01-01'
EndDate = '2018-01-01'

local isDateValid
isDateValid = isDateInRange(StartDate, EndDate, GivenDate)
return(isDateValid)
end

-- Pass User, UserCategory and UserCategoryItem as parameters
local userNameKey = 'UserDetails:DummyName'
local userCategoryKey = 'UserCategories:Book'
local userCategoryItemKey = 'UserCategoryItems:Harry-Potter'

local userNameKeySplit = {}
local a = 1
for i in string.gmatch(userNameKey, '([^'..':'..']+)') do
  userNameKeySplit[a] = i
  a = a + 1
end
local userName = userNameKeySplit[2]


local userCategoryKeySplit = {}
local b = 1
for j in string.gmatch(userCategoryKey, '([^'..':'..']+)') do
  userCategoryKeySplit[b] = j
  b = b + 1
end
local userCategory = userCategoryKeySplit[2]



local userCategoryItemKeySplit = {}
local c = 1
for k in string.gmatch(userCategoryItemKey, '([^'..':'..']+)') do
  userCategoryItemKeySplit[c] = k
  c = c + 1
end
local userCategoryItem = userCategoryItemKeySplit[2]

return(userName..' '..userCategory..' '..userCategoryItem)

",

                new RedisKey[] { "UserCategoryItemNames:" + userModel.UserId });


//PART TWO OF THE SCRIPT

            var doesExist2 = (bool)db.ScriptEvaluate(@" 
local userName = 'DummyName'
local userCategory = 'Book'
local userCategoryItem = 'Harry-Potter'

-- Fetch Users, UserCategories and UserCategoryItems from the Redis DB
local userData = {'DummyName', 'User1', 'User2', 'User3'}
local userCategoryData = {'Book', 'Movie', 'Journals'}
local userCategoryItemData = {'Hardy-Boys', 'Harry-Potter', 'Shawshank-Redemption', 'The-Terminal'}

local msg
-- Loop through the fetched values and compare them with parameters; if all the parameters are found return true else return false
for i=1,#userData,1
        do
            if(userData[i] == userName)
            then
                for i=1,#userCategoryData,1
                do
                    if(userCategoryData[i] == userCategory)
                    then
                        for i=1,#userCategoryItemData,1
                        do
                            if(userCategoryItemData[i] == userCategoryItem)
                            then
                                msg = true
                            break
                            else
                                msg = false
                            end
                        end
                    break
                    else
                        msg = false
                    end
                end
            break
            else
                msg = false
                break
            end
        end



return(msg)

//PART ONE & TWO COMBINED OF THE SCRIPT

                                        ",

                new RedisKey[] { "UserDetails:" + "DummyName", "UserCategories:" + "Book", "UserCategoryItems:" + "Harry-Potter" });

            var doesExist3 = (bool)db.ScriptEvaluate(@" 

local function isDateInRange(DateStart, DateEnd, GivenDate)
    if (GivenDate > DateStart and GivenDate < DateEnd)
    then
        return(true)
    else
        return(false)
    end
end

--Fetch User's Category's StartDate and EndDate
local function IsCategoryDateValid()
local givenDate, DateStart, DateEnd

GivenDate = '2017-01-09'
StartDate = '2015-01-01'
EndDate = '2018-01-01'

local isDateValid
isDateValid = isDateInRange(StartDate, EndDate, GivenDate)
return(isDateValid)
end

-- Pass User, UserCategory and UserCategoryItem as parameters
local userNameKey = 'UserDetails:DummyName'
local userCategoryKey = 'UserCategories:Book'
local userCategoryItemKey = 'UserCategoryItems:Harry-Potter'

local userNameKeySplit = {}
local a = 1
for i in string.gmatch(userNameKey, '([^'..':'..']+)') do
  userNameKeySplit[a] = i
  a = a + 1
end
local userName = userNameKeySplit[2]


local userCategoryKeySplit = {}
local b = 1
for j in string.gmatch(userCategoryKey, '([^'..':'..']+)') do
  userCategoryKeySplit[b] = j
  b = b + 1
end
local userCategory = userCategoryKeySplit[2]



local userCategoryItemKeySplit = {}
local c = 1
for k in string.gmatch(userCategoryItemKey, '([^'..':'..']+)') do
  userCategoryItemKeySplit[c] = k
  c = c + 1
end
local userCategoryItem = userCategoryItemKeySplit[2]

-- Fetch Users, UserCategories and UserCategoryItems from the Redis DB
local userData = {'DummyName', 'User1', 'User2', 'User3'}
local userCategoryData = {'Book', 'Movie', 'Journals'}
local userCategoryItemData = {'Hardy-Boys', 'Harry-Potter', 'Shawshank-Redemption', 'The-Terminal'}

local msg
-- Loop through the fetched values and compare them with parameters; if all the parameters are found return true else return false
for i=1,#userData,1
        do
            if(userData[i] == userName)
            then
                for i=1,#userCategoryData,1
                do
                    if(userCategoryData[i] == userCategory)
                    then
                        for i=1,#userCategoryItemData,1
                        do
                            if(userCategoryItemData[i] == userCategoryItem)
                            then
                                msg = true
                            break
                            else
                                msg = false
                            end
                        end
                    break
                    else
                        msg = false
                    end
                end
            break
            else
                msg = false
                break
            end
        end



return(msg)

                                        ",

                new RedisKey[] { "UserDetails:" + "DummyName", "UserCategories:" + "Book", "UserCategoryItems:" + "Harry-Potter" });

感谢您的耐心和阅读。我希望我已经详细澄清了这个问题。请在解决问题时提供一些帮助。谢谢。

0 个答案:

没有答案