@@ -16,6 +16,11 @@ local concat = table.concat
1616local setmetatable = setmetatable
1717local pcall = pcall
1818
19+ local isempty = require (' table.isempty' )
20+
21+ -- Module-level cache storage (one per worker process)
22+ local manifests_cache = {}
23+
1924local _M = {}
2025
2126local resty_env = require (' resty.env' )
@@ -70,8 +75,26 @@ local function lua_load_path(load_path)
7075 return format (' %s/?.lua' , load_path )
7176end
7277
78+ -- Get a cached manifest by policy name and version
79+ -- @tparam string name The policy name
80+ -- @tparam string version The policy version
81+ -- @treturn table|nil The cached manifest table, or nil if not cached
82+ local function get_cached_manifest (name , version )
83+ local manifests = manifests_cache [name ]
84+ if manifests then
85+ for _ , manifest in ipairs (manifests ) do
86+ if version == manifest .version then
87+ return manifest
88+ end
89+ end
90+ end
91+ end
92+
7393local function load_manifest (name , version , path )
74- local manifest = read_manifest (path )
94+ local manifest = get_cached_manifest (name , version )
95+ if not manifest then
96+ manifest = read_manifest (path )
97+ end
7598
7699 if manifest then
77100 if manifest .version ~= version then
110133function _M :load_path (name , version , paths )
111134 local failures = {}
112135
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 ) )
136+ if version == ' builtin ' then
137+ local manifest , load_path = load_manifest (name , version , format (' %s/%s' , self . builtin_policy_load_path () , name ) )
115138
116139 if manifest then
117140 return load_path , manifest .configuration
@@ -120,8 +143,8 @@ function _M:load_path(name, version, paths)
120143 end
121144 end
122145
123- if version == ' builtin ' then
124- local manifest , load_path = load_manifest (name , version , format (' %s/%s' , self . builtin_policy_load_path () , name ) )
146+ for _ , path in ipairs ( paths or self . policy_load_paths ()) do
147+ local manifest , load_path = load_manifest (name , version , format (' %s/%s/%s ' , path , name , version ) )
125148
126149 if manifest then
127150 return load_path , manifest .configuration
@@ -130,6 +153,7 @@ function _M:load_path(name, version, paths)
130153 end
131154 end
132155
156+
133157 return nil , nil , failures
134158end
135159
173197-- Returns all the policy modules
174198function _M :get_all ()
175199 local policy_modules = {}
200+ local manifests
176201
177- local policy_manifests_loader = require (' apicast.policy_manifests_loader' )
178- local manifests = policy_manifests_loader .get_all ()
202+ if isempty (manifests_cache ) then
203+ local policy_manifests_loader = require (' apicast.policy_manifests_loader' )
204+ manifests = policy_manifests_loader .get_all ()
205+ manifests_cache = manifests
206+ else
207+ manifests = manifests_cache
208+ end
179209
180210 for policy_name , policy_manifests in pairs (manifests ) do
181211 for _ , manifest in ipairs (policy_manifests ) do
0 commit comments