More languages
More actions
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
local CACHE_EXPIRATION = 86400 -- Cache for 24 hours (in seconds) | local CACHE_EXPIRATION = 86400 -- Cache for 24 hours (in seconds) | ||
-- Function to get the number of works, with caching | |||
-- Function | |||
function p.getWorkCount(frame) | function p.getWorkCount(frame) | ||
local category = frame.args.category or '' | local category = frame.args.category or '' | ||
if category == '' then return "0 works" end | if category == '' then return "0 works" end | ||
-- | -- Create a unique cache key based on the category | ||
local | 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 | end | ||
-- | -- If no cached result, perform the expensive call | ||
local pages = mw.site.stats.pagesInCategory(category) | local pages = mw.site.stats.pagesInCategory(category) | ||
-- | -- Format the result and store it in the cache | ||
local result = '' | |||
if tonumber(pages) == 1 then | if tonumber(pages) == 1 then | ||
result = "1 work" | |||
else | else | ||
result = pages .. " works" | |||
end | end | ||
cache:set(cacheKey, result, CACHE_EXPIRATION) | |||
return result | |||
end | end | ||
Revision as of 15:02, 14 September 2024
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