|
|
(2 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| local p = {} | | 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) | | function p.getWorkCount(frame) |
| local category = frame.args.category or '' | | local category = frame.args.category or '' |
| if category == '' then return "0 works" end
| | local pages = mw.site.stats.pagesInCategory(category) |
| | |
| -- 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 | | if tonumber(pages) == 1 then |
| result = "1 work" | | return "1 work" |
| else | | else |
| result = pages .. " works" | | return pages .. " works" |
| end | | end |
|
| |
| cache:set(cacheKey, result, CACHE_EXPIRATION)
| |
|
| |
| return result
| |
| end | | end |
|
| |
|
local p = {}
function p.getWorkCount(frame)
local category = frame.args.category or ''
local pages = mw.site.stats.pagesInCategory(category)
if tonumber(pages) == 1 then
return "1 work"
else
return pages .. " works"
end
end
function p.main(frame)
return p.getWorkCount(frame)
end
return p