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 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 cache the result for the current page view
-- Function to get the number of works, with caching
local function setCache(category, pages)
    local cache = mw.ext.LuaCache
    if cache then
        local key = "NumberOfWorks_" .. category
        cache:set(key, pages, CACHE_EXPIRATION)
    end
end
 
-- Function to get the cached result for the current page view
local function getCache(category)
    local cache = mw.ext.LuaCache
    if cache then
        local key = "NumberOfWorks_" .. category
        return cache:get(key)
    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
     if category == '' then return "0 works" end


     -- Check the cache first
     -- Create a unique cache key based on the category
     local cachedPages = getCache(category)
     local cacheKey = 'NumberOfWorks_' .. category
     if cachedPages then
      
        if tonumber(cachedPages) == 1 then
    -- Use mw.cache to store the result
            return "1 work"
    local cache = mw.cache.makeLuaSandboxCache(1) -- 1 means per-page caching, avoids recalculating during the same page render
        else
    local cachedResult = cache:get(cacheKey)
            return cachedPages .. " works"
 
         end
    -- If we have a cached result, return it
    if cachedResult then
         return cachedResult
     end
     end


     -- No cached result, perform expensive call
     -- If no cached result, perform the expensive call
     local pages = mw.site.stats.pagesInCategory(category)
     local pages = mw.site.stats.pagesInCategory(category)


     -- Cache the result
     -- Format the result and store it in the cache
     setCache(category, pages)
     local result = ''
 
    -- Return the result
     if tonumber(pages) == 1 then
     if tonumber(pages) == 1 then
         return "1 work"
         result = "1 work"
     else
     else
         return pages .. " works"
         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