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

Module:Number of works: Difference between revisions

From ProleWiki, the proletarian encyclopedia
No edit summary
No edit summary
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
-- Table to store local cached results during this execution
local cache = {}
 
-- 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
     if category == '' then return "0 works" end


     -- Create a unique cache key based on the category
     -- Check if we already have the result for this category in local cache
    local cacheKey = 'NumberOfWorks_' .. category
     if cache[category] then
   
         return cache[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
     -- Perform the expensive call to get the number of pages in the category
     local pages = mw.site.stats.pagesInCategory(category)
     local pages = mw.site.stats.pagesInCategory(category)


     -- Format the result and store it in the cache
     -- Format the result
     local result = ''
     local result = ''
     if tonumber(pages) == 1 then
     if tonumber(pages) == 1 then
Line 29: Line 24:
         result = pages .. " works"
         result = pages .. " works"
     end
     end
      
 
     cache:set(cacheKey, result, CACHE_EXPIRATION)
     -- Store the result in the local cache
     cache[category] = result


     return result
     return result

Revision as of 15:03, 14 September 2024

local p = {}

-- Table to store local cached results during this execution
local cache = {}

-- 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 if we already have the result for this category in local cache
    if cache[category] then
        return cache[category]
    end

    -- Perform the expensive call to get the number of pages in the category
    local pages = mw.site.stats.pagesInCategory(category)

    -- Format the result
    local result = ''
    if tonumber(pages) == 1 then
        result = "1 work"
    else
        result = pages .. " works"
    end

    -- Store the result in the local cache
    cache[category] = result

    return result
end

function p.main(frame)
    return p.getWorkCount(frame)
end

return p