More languages
More actions
(1 work instead of 1 works) |
(caching method v1) |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local CACHE_EXPIRATION = 86400 -- Cache for 24 hours (in seconds) | |||
-- Function to store the result in a page as a cache | |||
local function setCache(category, pages) | |||
local cacheTitle = mw.title.new("Module:Number of works/cache") | |||
if cacheTitle then | |||
local content = mw.text.jsonEncode({ | |||
[category] = { | |||
pages = pages, | |||
timestamp = os.time() | |||
} | |||
}) | |||
cacheTitle:edit(content, "Updating cached page count for category " .. category) | |||
end | |||
end | |||
-- Function to get the cached result | |||
local function getCache(category) | |||
local cacheTitle = mw.title.new("Module:Number of works/cache") | |||
if cacheTitle and cacheTitle.exists then | |||
local content = cacheTitle:getContent() | |||
local cacheData = mw.text.jsonDecode(content) | |||
if cacheData and cacheData[category] then | |||
local data = cacheData[category] | |||
local cacheAge = os.time() - data.timestamp | |||
if cacheAge < CACHE_EXPIRATION then | |||
return data.pages | |||
end | |||
end | |||
end | |||
return nil | |||
end | |||
-- Main function to get the number of works | |||
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 | |||
-- Check the cache first | |||
local cachedPages = getCache(category) | |||
if cachedPages then | |||
if tonumber(cachedPages) == 1 then | |||
return "1 work" | |||
else | |||
return cachedPages .. " works" | |||
end | |||
end | |||
-- No cached result, perform expensive call | |||
local pages = mw.site.stats.pagesInCategory(category) | local pages = mw.site.stats.pagesInCategory(category) | ||
-- Cache the result | |||
setCache(category, pages) | |||
-- Return the result | |||
if tonumber(pages) == 1 then | if tonumber(pages) == 1 then | ||
return "1 work" | return "1 work" |
Revision as of 14:56, 14 September 2024
local p = {}
local CACHE_EXPIRATION = 86400 -- Cache for 24 hours (in seconds)
-- Function to store the result in a page as a cache
local function setCache(category, pages)
local cacheTitle = mw.title.new("Module:Number of works/cache")
if cacheTitle then
local content = mw.text.jsonEncode({
[category] = {
pages = pages,
timestamp = os.time()
}
})
cacheTitle:edit(content, "Updating cached page count for category " .. category)
end
end
-- Function to get the cached result
local function getCache(category)
local cacheTitle = mw.title.new("Module:Number of works/cache")
if cacheTitle and cacheTitle.exists then
local content = cacheTitle:getContent()
local cacheData = mw.text.jsonDecode(content)
if cacheData and cacheData[category] then
local data = cacheData[category]
local cacheAge = os.time() - data.timestamp
if cacheAge < CACHE_EXPIRATION then
return data.pages
end
end
end
return nil
end
-- Main function to get the number of works
function p.getWorkCount(frame)
local category = frame.args.category or ''
if category == '' then return "0 works" end
-- Check the cache first
local cachedPages = getCache(category)
if cachedPages then
if tonumber(cachedPages) == 1 then
return "1 work"
else
return cachedPages .. " works"
end
end
-- No cached result, perform expensive call
local pages = mw.site.stats.pagesInCategory(category)
-- Cache the result
setCache(category, pages)
-- Return the result
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