local p = {}
local CACHE_EXPIRATION = 86400 -- Cache for 24 hours (in seconds)
-- Function to get the number of works, with caching
function p.getWorkCount(frame)
local category = frame.args.category or ''
if category == '' then return "0 works" end
-- Create a unique cache key based on the category
local cacheKey = 'NumberOfWorks_' .. category
-- Use mw.cache to store the result
local cache = mw.cache.makeLuaSandboxCache(1) -- 1 means per-page caching, avoids recalculating during the same page render
local cachedResult = cache:get(cacheKey)
-- If we have a cached result, return it
if cachedResult then
return cachedResult
end
-- If no cached result, perform the expensive call
local pages = mw.site.stats.pagesInCategory(category)
-- Format the result and store it in the cache
local result = ''
if tonumber(pages) == 1 then
result = "1 work"
else
result = pages .. " works"
end
cache:set(cacheKey, result, CACHE_EXPIRATION)
return result
end
function p.main(frame)
return p.getWorkCount(frame)
end
return p