Version 1 - SDK 0.24.0 to 0.27.0
Version 2 - SDK 0.28.0
Provides functionality for caching assets using least recently used (LRU) caching. 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") }}*/
Example
The AssetCache object is used in the leaderboards sample to make sure that by scrolling through users’ avatar textures the amount of memory used is limited (while keeping the interface smooth).
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
Load the asset from the cache.
Syntax
var callback = function loadedCallbackFn(key, asset, params)
{
// Loading complete, use asset/trigger cache use
};
assetCache.request(key, params, callback);
The callback to be triggered when the requested asset has been loaded. This function will be called with the following arguments:
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.
In SDK 0.28.0
Does not return a value.
If the asset is already loading, the callback argument will be added to list of callbacks to notify when the asset is loaded.
In SDK 0.24.0 to 0.27.0
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.
Summary
Added in SDK 0.28.0
Get the asset from the cache.
Syntax
var assetOrNull = assetCache.get(key);
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.
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.