Table Of Contents

Previous topic

20.40. The ResourceLoader Object

Next topic

20.42. The Scene Object

This Page

20.41. The AssetCache Object

Provides functionality for caching assets. When the cache is full each cache miss causes the removal of the asset that has the oldest requested time.

Required scripts

The AssetCache object requires:

/*{{ javascript("jslib/assetcache.js") }}*/

20.41.1. Constructor

20.41.1.1. create

Summary

Creates and returns an AssetCache object.

Syntax

var cacheParams = {
        size: 32,
        load: function loadAssetFn(key, params, loadedCallback) {},
        destroy: function destroyAssetFn(key, asset) {}
    };

var assetCache = AssetCache.create(cacheParams);

// example usage:
cacheParams = {
        size: 64,
        onLoad: function loadTextureFn(key, params, loadedCallback)
        {
            graphicsDevice.createTexture({
                    src: key,
                    mipmaps: true,
                    onload: loadedCallback
                });
        },
        onDestroy: function destroyTextureFn(key, texture)
        {
            if (texture)
            {
                texture.destroy();
            }
        }
    };
var textureCache = AssetCache.create(cacheParams);
onLoad
A JavaScript function. The function called when a cache miss occurs to load the asset into the cache. See assetCache.onLoad.
onDestroy
A JavaScript function. This is called for the asset with the oldest requested time when a cache miss occurs and the cache is full. See assetCache.onDestroy.
size (Optional)
A JavaScript integer. The size (maximum possible number of assets) of the asset cache. Defaults to 64.

20.41.2. Methods

20.41.2.1. request

Summary

Get/load the asset from the cache.

Syntax

var assetOrNull = assetCache.request(key, params);
key
The cache identifier. This is normally the URL of the asset to get.
params
Custom params object passed onto the assetCache.onLoad function in the case of a cache miss.

Returns the loaded asset for a cache hit. If the key is missing from the cache (cache miss) or if the asset is loading returns null.

In the case of a cache miss the asset will be loaded asynchronously using the assetCache.onLoad function. If the cache is full this will also trigger an assetCache.onDestroy function for the asset that has the oldest requested time.

20.41.2.2. exists

Summary

Check if an asset is already in the cache.

Syntax

var assetExists = assetCache.exists(key);
key
The cache identifier. This is normally the URL of the asset.

Returns true if the asset is in the cache (assets which are loading will also return true here). Returns false if the asset is not in the cache.

20.41.2.3. isLoading

Summary

Check if an asset is currently loading.

Syntax

var assetLoading = assetCache.isLoading(key);
key
The cache identifier. This is normally the URL of the asset.

Returns true if the is loading. Returns false if the asset is not in the cache or has completed loading.

20.41.3. Properties

20.41.3.1. onLoad

Summary

The function called when a cache miss occurs to load the asset into the cache.

Syntax

assetCache.onLoad = function assetCacheOnLoadFn(key, params, loadedCallback) {};

It takes the arguments:

key
The cache identifier. This is normally the URL of the asset to load.
params
Custom params object passed into the request function.
loadedCallback
Callback to call with the asset once it is loaded.

20.41.3.2. onDestroy

Summary

This is called for the asset with the oldest requested time when a cache miss occurs and the cache is full.

Syntax

assetCache.onDestroy = function assetCacheOnLoadFn(key, asset) {};

It takes the arguments:

key
The cache identifier. This is normally the URL of the asset to load.
asset
The asset being removed from the cache.