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

Cache API

The Cache API simplifies the creation and management of cached data.

 

Overview

The Cache API provides a simple and effective way to cache data for a specified amount of time.

When cached data expires, it's automatically regenerated the next time something tries to access it (a sort of lazy invalidation).

There's 3 main ways in which a cache can expire:

  • Manual – the cache will only expire when forced to do so by your code
  • Game tick – the cache will automatically expire whenever the value of gameTime changes
  • Time offset – the cache will automatically expire after a certain amount of game time has passed

The Cache API is really simple to use:

  • Use Cache() to define cache keys
  • Use Cache.key to retrieve (get) or invalidate (refresh) cached data

Here's some neat examples of using the Cache API:

In the above examples, game tick caching is used. So in the same processing slot, while Warzone is paused waiting for scripts to process events, if your script requests the same list of structures or droids more than once (which is surprisingly common) the first request will get fresh data and cache it, whilst subsequent requests will be given the cached data. Because gameTime is paused during a processing slot, the data won't be changing anyway, and caching it has virtually no overhead (other than using a little RAM), so the performance improvements can be quite noticeable, especially on complex scripts.

Working out what to cache does require some insight in to your code, the data it works with and the time it takes to generate, etc., but most script developers will surely see many obvious scenarios in which the Cache API can reduce processing overheads and boost performance of their scripts. In some cases, code refactoring may be required to get the maximum advantage from using Cache API. For example, if you regularly refer to big lists of enemy droids, maybe you can cache that list for a few seconds and alter your code to deal with any errors caused by droids in the list being destroyed or new droids appearing on the map.

If you like the Cache API, I recommend you also take a look at the Process API – it allows you to perform slow tasks over multiple game ticks to avoid game lag, an

Availability STABLE

Requires:

  • Warzone 3.1 or above
  • Util API

Cache API

Topics: