@@ -157,28 +157,30 @@ function _M:getRulesForType(rule_type)
157
157
-- 0. check if lastModifiedDate for the dict matches the local date
158
158
local lastModified = dict :get (" _lastModified" )
159
159
if (lastModified == nil ) then
160
+ ngx .log (ngx .DEBUG , " Returning the rules from the cache as lastModified is nil in the dict named=" , tostring (dict_name ))
160
161
return cached_rules [rule_type ]
161
162
end
162
163
-- 1. return the keys from the local variable, if lastModified date allows and if the local cache is not empty
163
164
if (lastModified == last_modified_date [dict_name ]) then
165
+ ngx .log (ngx .DEBUG , " Returning the rules from the cache as the dict hasn't been updated in the dict named=" , tostring (dict_name ))
164
166
return cached_rules [rule_type ]
165
167
end
166
168
-- 2. else, read it from the shared dict
167
169
-- docs: http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT.get_keys
168
- -- wil return a max os 1024 keys
170
+ -- will return a max of 1024 keys
169
171
cached_rules [rule_type ] = {}
170
172
local keys = dict :get_keys ()
171
- local val_string , val , data , domain , expire_at_utc , id , meta
173
+ local val_string , val , data , domain , expire_at_utc , id , meta , decode_ok
172
174
local i , format_split_idx
173
175
for i , key in pairs (keys ) do
174
176
val_string , data = dict :get (key )
175
177
if (val_string ~= nil and key ~= " _lastModified" ) then
176
178
format_split_idx = key :find (" " )
177
- val = cjson .decode ( val_string )
178
- if ( val ~= nil and format_split_idx ~= nil ) then
179
+ decode_ok , val = pcall ( cjson .decode , val_string )
180
+ if ( decode_ok and val ~= nil and format_split_idx ~= nil ) then
179
181
id = key :sub (1 , format_split_idx - 1 )
180
182
cached_rules [rule_type ][i ] = {
181
- id = tonumber (id ) or id , -- try to convert it to a number, but if it's not the keep string
183
+ id = tonumber (id ) or id , -- try to convert it to a number, but if it's not keep the string
182
184
format = key :sub (format_split_idx + 1 ),
183
185
domain = val [" domain" ],
184
186
expire_at_utc = val [" expire_at_utc" ],
@@ -187,7 +189,7 @@ function _M:getRulesForType(rule_type)
187
189
data = data
188
190
}
189
191
else
190
- ngx .log (ngx .WARN , " Could not read rule from shared_dict:" , tostring (key ), " with val: " , tostring (val ))
192
+ ngx .log (ngx .WARN , " Could not read rule from shared_dict:" , tostring (dict_name ), " , key= " , tostring ( key ), " , val= " , tostring (val ))
191
193
end
192
194
end
193
195
end
0 commit comments