(info) This AI is still in early stages of development.

Cache()

Define a new Cache.key and it's associated settings.

 

Syntax

Cache(key, fn, ttl);

Parameters

ParameterTypeMandatoryNotesAPI Version
keyString(tick)The "key" (name) of the cache1.0
fn

Function

Variant

(tick)

A function that will generate new data when the cache expires.

Or a static primitive, object or array value. Note: This also causes ttl to default to -1.

1.0
ttlNumber(error)

The "time to live" which defines how long data will be cached for before it expires:

  • -1 – cache does not expire, requires manual refresh
  • 0 – cache expires every "game tick" (whenever gameTime changes)
  • N – the cache will expire after N milliseconds of game time

It's important to note that game time doesn't necessarily run at the same speed as real world time – users can speed up or slow down the game (in single player games) or lag can stall game time (in multiplayer games), and slow running scripts can also cause game lag (in both single player and multiplayer games).

Default: -1

1.0

Return value

The function does not return anything.

Examples

Cache that updates only when you tell it to
Cache(
  "foo",
  function() {
    return "gameTime: "+gameTime+" (requires manual refresh)";
  },
  -1 // negative ttl = manual refresh mode
);
 
console(cache.foo); // outputs same time until you refresh the cache
 
// to refresh the cache (this works on any cache):
Cache.foo = void Cache;
Cache that updates when gameTime changes
Cache(
  "foo",
  function() {
    return "gameTime: "+gameTime+" (updates when gameTime changes)";
  },
  0 // zero ttl = update whenever gameTime changes
);
 
console(cache.foo); // outputs current gameTime
Cache that updates after set amount of game time
Cache(
  "foo",
  function() {
    return "gameTime: "+gameTime+" (updates every game second)";
  },
  1000 // cache will last for 1 game second
);
 
console(cache.foo); // time changes every second *of game time*
Availability STABLE

Requires:

Contents

Jump to:

Cache API

Topics: