Module Cache

Useful cache constructors.

For when you don't want to spam global savedata with lots of outdated junk.

Warning: Incorrect usage of Cache functions will cause desyncs. Read the instructions very carefully.

Module Status: Experimental 2020-10-31.

Usage:

    local Cache = require('__eradicators-library__/erlib/factorio/Cache')()
    

AutoCache

AutoCache(constructor) Creates a new AutoCache.

TickedCache

TickedCache() A tick-volatile cache.
TickedCache.__len() The Length Operator # works as it always does.
TickedCache:insert([pos], value) Standard table methods do not work for ticked caches, use this instead.
TickedCache:remove([pos]) Standard table methods do not work for ticked caches, use this instead.
TickedCache:map(func) Standard table methods do not work for ticked caches, use this instead.
TickedCache:size() Standard table methods do not work for ticked caches, use this instead.
TickedCache:is_empty() Standard table methods do not work for ticked caches, use this instead.

Todo

todo1 Use Table.map instead of custom local implementation for TickedCache.


AutoCache

AutoCache(constructor)
Creates a new AutoCache.

A read-only table that fills itself on first access. This is intended to store pre-processed prototype data at runtime without cluttering global savedata with tons of garbage. Less garbage means faster save/load. It also removes the need to migrate old data.

The engine limits access to prototype data to inside events for no good reason. AutoCache provides a comfortable way to transparently fetch the data in the first event you need it.

Warning: Because the constructor is called in a non-deterministic way it will cause desyncs or other weird bugs if you try to do anything other than reading startup settings or game.*_prototypes. You must not store any LuaObject wrappers, only pure lua values are desync-safe.

The constructor will be triggered before the following metamethods:
__index, __newindex, __pairs, __ipairs and __len.

Parameters:

  • constructor function This is called exactly once with an empty table as the only argument. It must then fill that table with the desired values. It must absolutely not change any other part of the lua state except this table. Any values returned by this function are discarted.

Returns:

    table the AutoCache'ed table.

Usage:

    -- First you need a data collector.
    local function get_entity_types (data)
      for k,v in pairs(game.prototypes) do
        data[v.name] = v.type -- You must store your data in the given table.
        end
      return nil -- The return value is not used.
      end
    
    -- Then you tell AutoCache to handle the collection for you.
    local MyData = AutoCache(get_entity_types)
    
    -- The collected data can only be used in events.
    script.on_event(defines.events.on_stuff_happend,function(event)
      if MyData[event.entity.name] == 'assembling-machine' then
        DoStuff()
        end
      end)
    

TickedCache

TickedCache()

A tick-volatile cache. Only contains values that were written during the same tick. The main use-case for this is to correlate data between events that happen sequentially in the same tick. This is desync safe because it has no influence across tick boundaries.

A TickedCache table emulates normal Lua table behavior as best as it can but there are a few important differences.

  1. Lua table, ErLib Table or table_size methods are not supported. You must use the equivalent methods mentioned on this page.

  2. Lua next is not supported, use standard for-loop pairs or ipairs instead.

Returns:

    TickedCache an empty ticked cache.
TickedCache.__len()
The Length Operator # works as it always does.

Returns:

    int The length of the array part.
TickedCache:insert([pos], value)
Standard table methods do not work for ticked caches, use this instead. Behaves the same as table.insert.

Parameters:

TickedCache:remove([pos])
Standard table methods do not work for ticked caches, use this instead. Behaves the same as table.remove.

Parameters:

TickedCache:map(func)
Standard table methods do not work for ticked caches, use this instead. The cache is altered in-place. Behaves the same as Table.map.

Parameters:

TickedCache:size()
Standard table methods do not work for ticked caches, use this instead.

Returns:

    uint64 the total number of NotNil keys in the cache.
TickedCache:is_empty()
Standard table methods do not work for ticked caches, use this instead.
Equivalent to (TC:size() == 0).

Returns:

    boolean

Todo

todo1
Use Table.map instead of custom local implementation for TickedCache.
  • todo1
generated by LDoc 1.4.6 Last updated 2021-09-10 19:51:19