renew my shared dictionary

46 views
Skip to first unread message

Hadi Abbasi

unread,
Oct 13, 2019, 10:19:56 AM10/13/19
to openresty-en
Hello friends...
I have a data structure in my openresty api.
the information on the dictionary is so important for my cdn moment by moment, I always refresh its data by current available admins settings every 5 minutes.
I can check all of removable and stale data using my algorithms!
(like collecting old keys in tmp array and iteration on new data array and removing current available keys from that tmp array and at the end of iteration, I have removable properties in my tmp array so I can remove all of removable information from my data structure)

 but just I wanna know if I can renew my shared dictionary using a way like:

----------nginx.conf
lua_shared_dict  data_struct
50m;



------ update data structure location
local new_shared_dictionary = {}
new_shared_dictionary
["property1"] = x

new_shared_dictionary
["property2"]= y
ngx
.shared.data_struct =  new_shared_dictionary



I think I have no any simple choice  like my example! am I right?
good luck...
Best,
Hadi


Piotr Przybylski

unread,
Oct 16, 2019, 2:27:19 PM10/16/19
to openresty-en
I use a different approach: configuration is stored in shared dictionary as JSON and in workers in deserialized form. Each time I want to get configuration I compare version stored in worker with version in shared dictionary. When my version is too old I update worker-level cache. It works as long as entire configuration can be deserialized in a sane amount of time, or if it can be divided into small parts that can be managed that way. It has an advantage of being simple to work with.

A simplified example:
-- config.lua
local M = {}

-- worker-level cache
local local_data_version = 0
local local_data

-- ngx.shared.my_config.data_json and ngx.shared.my_config.version are periodically set in ngx.timer worker
function M.get_full_config()
  local shared_dict_config = ngx.shared.my_config
  local shared_data_version = shared_dict_config.version
  if shared_data_version > data_version then
    data_version = shared_data_version
    data = cjson.decode(shared_dict_config.data_json)
  end

  return data
end

return M

Hadi Abbasi

unread,
Oct 17, 2019, 11:04:34 AM10/17/19
to openresty-en
thanks a lot..I'll check your approach...
and I think I have no any way to replace my dictionary by new raw one...
because it's atomic and renew possibility is risky and dangerous for that!
good luck...
Reply all
Reply to author
Forward
0 new messages