Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

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

 

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
Cache(key, fn, ttl);

Parameters

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

Function

Variant

(tick)

A function that will generate new data when the cache expires

0.1

.

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

1.0
ttlNumber(tick)(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.1

Return value

The function does not return anything.

Examples

Code Block
themeRDark
languagejavascript
titleCache that updates only when you tell it to
linenumberstrue
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;
Code Block
themeRDark
languagejavascript
titleCache that updates when gameTime changes
linenumberstrue
Cache(
  "foo",
  function() {
    return "gameTime: "+gameTime+" (updates when gameTime changes)";
  },
  0 // zero ttl = update whenever gameTime changes
);
 
console(cache.foo); // outputs current gameTime
Code Block
themeRDark
languagejavascript
titleCache that updates after set amount of game time
linenumberstrue
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*
Div
classbox
Availability
Status
colourGreen
titleStable

Requires:

Div
classbox
Contents

Jump to:

Table of Contents
maxLevel5

Div
classbox

Cache API

Topics:

Child pages (Children Display)
alltrue
depthall
pageCache API
excerpttrue