Module:WikitextProcessor: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 10: | Line 10: | ||
-- Module-level cache for processed content | -- Module-level cache for processed content | ||
local contentCache = {} | local contentCache = {} | ||
-------------------------------------------------------------------------------- | |||
-- Caching Mechanism (copied from TemplateHelpers to avoid circular dependency) | |||
-------------------------------------------------------------------------------- | |||
-- Helper for generating cache keys from multiple arguments | |||
-- @param prefix String prefix for the cache key (usually the function name) | |||
-- @param ... Any number of arguments to include in the cache key | |||
-- @return A string cache key | |||
local function generateCacheKey(prefix, ...) | |||
local args = {...} | |||
local parts = {prefix} | |||
for i, arg in ipairs(args) do | |||
if type(arg) == "table" then | |||
-- For tables, we can't reliably generate a cache key | |||
-- So we just use a placeholder with the table's memory address | |||
parts[i+1] = "table:" .. tostring(arg) | |||
elseif type(arg) == "nil" then | |||
parts[i+1] = "nil" | |||
else | |||
parts[i+1] = tostring(arg) | |||
end | |||
end | |||
return table.concat(parts, ":") | |||
end | |||
-- Generic caching wrapper | |||
-- @param cacheKey The cache key to use | |||
-- @param operation A function that returns the value to cache | |||
-- @return The cached result or the result of executing the operation | |||
local function withCache(cacheKey, operation) | |||
-- Check if result is already cached | |||
if contentCache[cacheKey] ~= nil then | |||
return contentCache[cacheKey] | |||
end | |||
-- Execute operation and cache result | |||
local result = operation() | |||
contentCache[cacheKey] = result | |||
return result | |||
end | |||
-- Constants as upvalues for performance | -- Constants as upvalues for performance | ||
| Line 137: | Line 180: | ||
} | } | ||
} | } | ||
-- Normalizes content string by cleaning up whitespace | -- Normalizes content string by cleaning up whitespace | ||
| Line 147: | Line 187: | ||
end | end | ||
local cacheKey = | local cacheKey = generateCacheKey("normalizeContentString", content) | ||
return | return withCache(cacheKey, function() | ||
-- Apply string normalization - consolidate whitespace operations | -- Apply string normalization - consolidate whitespace operations | ||
-- Handle multiple return values from gsub properly | -- Handle multiple return values from gsub properly | ||
| Line 165: | Line 205: | ||
end | end | ||
local cacheKey = | local cacheKey = generateCacheKey("replacePlaceholders", content, tostring(placeholderMap)) | ||
return | return withCache(cacheKey, function() | ||
local result = content | local result = content | ||
| Line 194: | Line 234: | ||
end | end | ||
local cacheKey = | local cacheKey = generateCacheKey("processWikiLinksToHTML", content) | ||
return | return withCache(cacheKey, function() | ||
local result = content | local result = content | ||
| Line 223: | Line 263: | ||
-- Create cache key including all parameters | -- Create cache key including all parameters | ||
local placeholdersKey = placeholders and tostring(placeholders) or "nil" | local placeholdersKey = placeholders and tostring(placeholders) or "nil" | ||
local cacheKey = | local cacheKey = generateCacheKey("processContentForFrontend", content, placeholdersKey) | ||
return | return withCache(cacheKey, function() | ||
-- Step 1: Normalize content string | -- Step 1: Normalize content string | ||
local processedContent = p.normalizeContentString(content) | local processedContent = p.normalizeContentString(content) | ||