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") }}*/
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);
Summary
Get/load the asset from the cache.
Syntax
var assetOrNull = assetCache.request(key, params);
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.
Summary
Check if an asset is already in the cache.
Syntax
var assetExists = assetCache.exists(key);
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.
Summary
Check if an asset is currently loading.
Syntax
var assetLoading = assetCache.isLoading(key);
Returns true if the is loading. Returns false if the asset is not in the cache or has completed loading.
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:
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.