@@ -16,6 +16,11 @@ local concat = table.concat
1616local setmetatable = setmetatable
1717local pcall = pcall
1818
19+ local tab_new = require (' table.new' )
20+ local isempty = require (' table.isempty' )
21+
22+ local manifests_cache = tab_new (32 , 0 )
23+
1924local _M = {}
2025
2126local resty_env = require (' resty.env' )
@@ -70,8 +75,22 @@ local function lua_load_path(load_path)
7075 return format (' %s/?.lua' , load_path )
7176end
7277
78+ local function get_manifest (name , version )
79+ local manifests = manifests_cache [name ]
80+ if manifests then
81+ for _ , manifest in ipairs (manifests ) do
82+ if version == manifest .version then
83+ return manifest
84+ end
85+ end
86+ end
87+ end
88+
7389local function load_manifest (name , version , path )
74- local manifest = read_manifest (path )
90+ local manifest = get_manifest (name , version )
91+ if not manifest then
92+ manifest = read_manifest (path )
93+ end
7594
7695 if manifest then
7796 if manifest .version ~= version then
110129function _M :load_path (name , version , paths )
111130 local failures = {}
112131
113- for _ , path in ipairs ( paths or self . policy_load_paths ()) do
114- local manifest , load_path = load_manifest (name , version , format (' %s/%s/%s ' , path , name , version ) )
132+ if version == ' builtin ' then
133+ local manifest , load_path = load_manifest (name , version , format (' %s/%s' , self . builtin_policy_load_path () , name ) )
115134
116135 if manifest then
117136 return load_path , manifest .configuration
@@ -120,8 +139,8 @@ function _M:load_path(name, version, paths)
120139 end
121140 end
122141
123- if version == ' builtin ' then
124- local manifest , load_path = load_manifest (name , version , format (' %s/%s' , self . builtin_policy_load_path () , name ) )
142+ for _ , path in ipairs ( paths or self . policy_load_paths ()) do
143+ local manifest , load_path = load_manifest (name , version , format (' %s/%s/%s ' , path , name , version ) )
125144
126145 if manifest then
127146 return load_path , manifest .configuration
@@ -130,6 +149,7 @@ function _M:load_path(name, version, paths)
130149 end
131150 end
132151
152+
133153 return nil , nil , failures
134154end
135155
173193-- Returns all the policy modules
174194function _M :get_all ()
175195 local policy_modules = {}
196+ local manifests
176197
177- local policy_manifests_loader = require (' apicast.policy_manifests_loader' )
178- local manifests = policy_manifests_loader .get_all ()
198+ if isempty (manifests_cache ) then
199+ local policy_manifests_loader = require (' apicast.policy_manifests_loader' )
200+ manifests = policy_manifests_loader .get_all ()
201+ manifests_cache = manifests
202+ else
203+ manifests = manifests_cache
204+ end
179205
180206 for policy_name , policy_manifests in pairs (manifests ) do
181207 for _ , manifest in ipairs (policy_manifests ) do
0 commit comments