14.04.2020, 13:00
Some issues with your code:
1. Don't use var1 and similiar variable names. It makes the code hard to read and understand.
2. You should not use storage.all() as it's quite an expensive operation when there is a lot of data in the storage.
3. Your MySQL query does not properly escape input variables. For example, query will fail if object name contains double quotes.
For your task you can use list data type of the storage engine. It allows to create a queue system with multiple scripts adding data to queue and one script reading and removing data from it.
Add item to list:
Get last element of the list (or nil when list is empty):
Read all entries from multiple lists and remove this data. This code read all storage lists which name starts with "log_".
1. Don't use var1 and similiar variable names. It makes the code hard to read and understand.
2. You should not use storage.all() as it's quite an expensive operation when there is a lot of data in the storage.
3. Your MySQL query does not properly escape input variables. For example, query will fail if object name contains double quotes.
For your task you can use list data type of the storage engine. It allows to create a queue system with multiple scripts adding data to queue and one script reading and removing data from it.
Add item to list:
Code:
list_name = 'log_1'
data = '1234'
storage.exec('rpush', list_name, data)
Get last element of the list (or nil when list is empty):
Code:
function get_last_list_item(key)
local items = storage.exec('lrange', key, -1, -1)
if type(items) == 'table' then
return items[ 1 ]
end
end
list_name = 'log_1'
last = get_last_list_item(list_name)
Read all entries from multiple lists and remove this data. This code read all storage lists which name starts with "log_".
Code:
-- open single connection for better performance when multiple storage commands are used
storage.openconn()
-- find all matching keys
keys = storage.keys('log_*')
for _, key in ipairs(keys) do
-- get all elements from the list
data = storage.exec('lrange', key, 0, -1)
if type(data) == 'table' then
for _, item in ipairs(data) do
-- do something with data
end
-- remove read entries from the list
storage.exec('ltrim', key, #data, -1)
end
end
storage.closeconn()