This module provides a way of storing data between module invocations. Possible data types are strings, numbers, nil and tables. This is useful for templates that tend to appear multiple times on the same page. Do not use this for modules that only appear a single time per page. It will lead to performance decreases.
Warning[]
Metatables are not preserved. To work around this, this module automatically checks for a __tostring
method and converts the table to a string if possible.
Functions[]
Basic[]
cache.auto[]
cache.auto(func, args, prefix)
The cache.auto function is the simplest way of implementing caching. It wraps the function supplying the value and automatically decides whether to execute the function or use a cached version.
It's parameters are:
- The function for which caching should be applied.
- A table holding the functions arguments.
- An optional prefix for the key under which the output will be stored (useful if multiple functions have the same arguments).
In pseudo-code it would look something like this:
function cache.auto(func, args, prefix)
if prefix_args[1]_args[2].isCached() then
return get_cache(prefix_args[1]_args[2])
else
return set_cache(prefix_args[1]_args[2], func(args))
end
end
Advanced[]
Use these functions if you want to manually implement caching in your module.
cache.set[]
cache.set(args, value, prefix)
Stores content. Logs the stored content and the variable name. Returns the content.
cache.get[]
cache.get(args, prefix)
Retrieves content. Returns the content.
Usage[]
local p = {}
local cache = require('Module:Cache')
function p.example(args)
-- First usage of p._example(); Executes the function and stores the value.
cache.auto(p._example, args, 'example_prefix')
-- Second usage; Uses the cached version.
cache.auto(p._example, args, 'example_prefix')
end
function p._example(args)
return args[1]
end
return p
Tests[]
All tests passed.
Name | Expected | Actual | |
---|---|---|---|
test_auto_boolean | |||
test_auto_number | |||
test_auto_string | |||
test_auto_table | |||
test_get_boolean | |||
test_get_no_set | |||
test_get_number | |||
test_get_string | |||
test_get_table | |||
test_set_boolean | |||
test_set_number | |||
test_set_string | |||
test_set_table |
--------------------------------------------------------------------------------
-- Imports
--------------------------------------------------------------------------------
local libraryUtil = require( 'libraryUtil' )
local var = require( 'Module:Variables' ).var
local vardefine = require( 'Module:Variables' ).vardefine
--------------------------------------------------------------------------------
-- Strings
--------------------------------------------------------------------------------
local i18n = {
error = {
unsupported_type = '不支持的数据类型'
},
log = {
caching = '没有找到缓存版本。存储中。'
}
}
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
local h = {}
--- Generate a variable unique key.
-- @param args (table): The template/module arguments.
-- @param prefix (string): A unique prefix for the cached template/module.
-- @return (string): A unique key.
function h.make_key( args, prefix )
prefix = ( prefix or 'cache' )
local key = { prefix }
for _,arg in pairs( args ) do
key[#key+1] = tostring( arg )
end
local key = table.concat( key, '_' )
return mw.hash.hashValue( 'md5', key )
end
--- Convert a value into a string for caching.
-- @param value (string/number/bool/table/nil): The value to be converted
-- @return (string): The converted value.
-- @return (string): The original type.
function h.preprocess( value )
local value_type = type( value )
if value_type == 'number' or value_type == 'boolean' then
value = tostring( value )
elseif value_type == 'table' then
-- Check the table for a metatable to detect mw.html and similar objects.
local mt = getmetatable( value )
if mt and mt.__tostring then
value = tostring( value )
value_type = 'string'
else
value = mw.text.jsonEncode( value )
end
elseif value_type ~= 'string' then
error( i18n.error.unsupported_type )
end
return value, value_type
end
--- Convert a string value back to its original type.
-- @param value (string): The string value.
-- @param value_type (string): The original type.
-- @return (string/number/bool/table/nil): The value in its original type.
function h.process( value, value_type )
if value_type == 'string' then
return value
elseif value_type == 'number' then
return tonumber( value )
elseif value_type == 'boolean' then
return ( value == 'true' )
elseif value_type == 'table' then
return mw.text.jsonDecode( value )
elseif not value_type then
return
else
error( i18n.error.unsupported_type )
end
end
--- Write a value to the cache.
-- @param key (string): The key under which the value will be stored.
-- @param value (string/number/bool/table/nil): The value to store.
-- @return (string/number/bool/table/nil): The value.
function h.write( key, value )
local processed_value, value_type = h.preprocess( value )
vardefine( key, processed_value )
vardefine( key .. '__type', value_type )
return value
end
--- Read a value from the cache.
-- @param key (string): The key from which to retrieve the value.
-- @return (string/number/bool/table/nil): The original value.
function h.read( key )
local value = var( key )
local value_type = var( key .. '__type' )
return h.process( value, value_type )
end
--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------
local p = {}
function p.auto( func, args, prefix )
libraryUtil.checkType( 'auto', 1, func, 'function' )
libraryUtil.checkType( 'auto', 2, args, 'table' )
libraryUtil.checkType( 'auto', 3, prefix, 'string', true )
local key = h.make_key( args, prefix )
local cached = h.read( key )
if cached then
return cached
else
mw.log( i18n.log.caching )
return h.write( key, func( args ) )
end
end
function p.set( args, value, prefix )
libraryUtil.checkType( 'set', 1, args, 'table' )
libraryUtil.checkTypeMulti( 'set', 2, value, { 'boolean', 'string', 'number', 'table' } )
libraryUtil.checkType( 'set', 3, prefix, 'string', true )
local key = h.make_key( args, prefix )
return h.write( key, value )
end
function p.get( args, prefix )
libraryUtil.checkType( 'get', 2, args, 'table' )
libraryUtil.checkType( 'get', 3, prefix, 'string', true )
local key = h.make_key( args, prefix )
return h.read( key )
end
function p.key( args, prefix )
return h.make_key( args, prefix )
end
--------------------------------------------------------------------------------
-- Return
--------------------------------------------------------------------------------
return p