Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Number of works

From ProleWiki, the proletarian encyclopedia
Revision as of 15:02, 14 September 2024 by CriticalResist (talk | contribs)
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