Hello,

the way the pseudo-variables were designed makes different definitions for $redis(r=>value[i] and $redis(r=>value[j], practically, if you have the index going on a large range, then you define a lot of variables. The solution is to leverage another pseduo-variable that you set with the index, like:

KSR.pvx.var_seti("i", right);

resultRight = tostring(KSR.pv.get("$redis(r=>value[$var(i)])"));

KSR.pvx.var_seti("i", left);

resultLeft = tostring(KSR.pv.get("$redis(r=>value[$var(i)])"));

Cheers,
Daniel

On 16.11.23 10:46, Ali Taher via sr-users wrote:

Hello all,

 

If the data returned from redis is of type array, and if I want to iterate through all the elements in the array, then I have to get $redis(r=>value[x]) for each element, and a pseudo variable for each element is allocated in the memory, is that correct?

 

Regards,

 

 

From: Ali Taher via sr-users <sr-users@lists.kamailio.org>
Sent: Wednesday, November 15, 2023 4:51 PM
To: Kamailio (SER) - Users Mailing List <sr-users@lists.kamailio.org>
Cc: Ali Taher <ataher@vanrise.com>
Subject: [SR-Users] pv cache limit is going to be exceeded

 

Hello all,

 

I’m using the following Lua script to fetch data from Redis, which is in form of list:

 

function split(str, delimiter)

    for token in string.gmatch(str, "[^" .. delimiter .. "]+") do

        return token

    end

end

 

function contains(mainString, subString)

    return string.find(mainString, subString, 1, true) ~= nil

end

 

function longest_match_acode()

 

                local currentSeconds = os.time()

                local milliseconds = os.clock() * 1000

                local ddate = os.date("%Y-%m-%d %H:%M:%S", currentSeconds) .. string.format(".%03d", milliseconds)

                KSR.xlog.xinfo("starting date:" ..ddate)

 

                local size = 0

                local result

 

                local acode = "29192"

                local inputbcode = "4474173"

                local delimiter = ":"

 

                while size == 0 do

                                local currentSeconds = os.time()

                                acode = string.sub(acode, 1, string.len(acode) - 1)

                                KSR.ndb_redis.redis_cmd("srvN", "LRANGE price:" .. acode .." 0 -1", "r")

                                size = tonumber(KSR.pv.get("$redis(r=>size)"))

                end

 

                local left = 0

                local right = size - 1

 

                while left <= right do

                    local currentSeconds = os.time()

        local milliseconds = os.clock() * 1000

        local ddate = os.date("%Y-%m-%d %H:%M:%S", currentSeconds) .. string.format(".%03d", milliseconds)

 

                                resultRight = tostring(KSR.pv.get("$redis(r=>value[" .. right .. "])"))

                                resultLeft = tostring(KSR.pv.get("$redis(r=>value[" .. left .. "])"))

 

                                local bcodeLeft = split(resultLeft, delimiter)

                                local bcodeRight = split(resultRight, delimiter)

 

                                if contains(inputbcode, bcodeRight) then

                                                KSR.xlog.xinfo("Input bcode contains bcode value from Redis: " .. resultRight .. " at index: " .. right .. " with date: " .. ddate)

                                end

 

                                if contains(inputbcode, bcodeLeft) then

                                   KSR.xlog.xinfo("Input bcode contains bcode value from Redis: " .. resultLeft .. " at index: " .. left .. " with date: " .. ddate)

                                end

                                left = left + 1

                                right = right - 1

                end

                local currentSeconds = os.time()

                local milliseconds = os.clock() * 1000

                local ddate = os.date("%Y-%m-%d %H:%M:%S", currentSeconds) .. string.format(".%03d", milliseconds)

                KSR.xlog.xinfo("ending date:" ..ddate)

 

end

 

function ksr_request_route()

 

                if KSR.is_INVITE() then

        longest_match_acode()

        KSR.sl.send_reply("404", "Not found");

                end       

end

 

I’m getting the following warning in the log : WARNING: LUA {INVITE}: <core> [core/pvapi.c:340]: pv_cache_add(): pv cache limit is going to be exceeded - pkg memory may get filled with pv declarations

I think the issue is in the following lines:

resultRight = tostring(KSR.pv.get("$redis(r=>value[" .. right .. "])"))

resultLeft = tostring(KSR.pv.get("$redis(r=>value[" .. left .. "])"))

 

the list coming from Redis has around 30k items, which I’m iterating through using value[x] which is exhausting the pv declaration as per my understanding.

How to overcome this issue? Is it possible to save the list as an array variable and then iterate through that array?

 

Regards,


__________________________________________________________
Kamailio - Users Mailing List - Non Commercial Discussions
To unsubscribe send an email to sr-users-leave@lists.kamailio.org
Important: keep the mailing list in the recipients, do not reply only to the sender!
Edit mailing list options or unsubscribe:
-- 
Daniel-Constantin Mierla (@ asipto.com)
twitter.com/miconda -- linkedin.com/in/miconda
Kamailio Consultancy and Development Services
Kamailio Advanced Training -- asipto.com