Define a new Cache.key and it's associated settings. |
Cache(key, fn, ttl); |
Parameter | Type | Mandatory | Notes | API Version |
---|---|---|---|---|
key | String | The "key" (name) of the cache | 1.0 | |
fn | Function Variant | 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 | |
ttl | Number | The "time to live" which defines how long data will be cached for before it expires:
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 |
The function does not return anything.
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( "foo", function() { return "gameTime: "+gameTime+" (updates when gameTime changes)"; }, 0 // zero ttl = update whenever gameTime changes ); console(cache.foo); // outputs current gameTime |
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* |