Материал из Dota 2 Вики
Перейти к: навигация, поиск
Lua logo.svg
Это страница документации для Модуль:Cache.
Она содержит информацию о использовании, категории и другое содержание, которое не является частью оригинальной страницы модуля.

This module provides a way of storing data between module invocations. Legal data types are strings, numbers and tables. The data gets stored as json in a variable named cargo_data. To see the stored raw json data, simply add {{#var:cargo_data}} at the end of the page.

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.




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:

  1. The function for which caching should be applied.
  2. A table holding the functions arguments.
  3. 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])
    return set_cache(prefix_args[1]_args[2], func(args))


Use these functions if you want to manually implement caching in your module.


cache.set(args, value, prefix)

Stores content. Logs the stored content and the variable name. Returns the content.


cache.get(args, prefix)

Retrieves content. Returns the content.


local p = {}
local cache = require('Модуль: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')

function p._example(args)
  return args[1]

return p


ДаД 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