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
(caching method v1)
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 store the result in a page as a cache
-- Function to cache the result for the current page view
local function setCache(category, pages)
local function setCache(category, pages)
     local cacheTitle = mw.title.new("Module:Number of works/cache")
     local cache = mw.ext.LuaCache
     if cacheTitle then
     if cache then
         local content = mw.text.jsonEncode({
         local key = "NumberOfWorks_" .. category
            [category] = {
         cache:set(key, pages, CACHE_EXPIRATION)
                pages = pages,
                timestamp = os.time()
            }
         })
        cacheTitle:edit(content, "Updating cached page count for category " .. category)
     end
     end
end
end


-- Function to get the cached result
-- Function to get the cached result for the current page view
local function getCache(category)
local function getCache(category)
     local cacheTitle = mw.title.new("Module:Number of works/cache")
     local cache = mw.ext.LuaCache
     if cacheTitle and cacheTitle.exists then
     if cache then
         local content = cacheTitle:getContent()
         local key = "NumberOfWorks_" .. category
        local cacheData = mw.text.jsonDecode(content)
         return cache:get(key)
          
        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
     end
     return nil
     return nil

Revision as of 14:58, 14 September 2024

local p = {}
local CACHE_EXPIRATION = 86400 -- Cache for 24 hours (in seconds)

-- Function to cache the result for the current page view
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)
    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