Cache Manager
For Web platforms, resources cache are managed by the browser after download, not the engine.
However, on some non-Web platforms, such as WeChat Mini Game, these platforms have a file system, which can be used to cache some remote resources, but do not implement a caching mechanism for resources. In this case, the engine needs to implement a set of caching mechanisms for managing resources downloaded from the network, including caching resources, cleaning cached resources, querying cached resources and other features.
Since v2.4, Creator provides a Cache Manager on all platforms where file systems exist, and you can access it via .
The logic of the engine downloading resources is as follows:
If not, check if the resource is in the cache, and if it is, use it directly;
If not, download the resources from the remote server and use them directly after downloading them to the temporary directory (the native platform downloads the resources to the cache directory);
Slowly save the resources from the temporary directory to the local cache directory in the background (the native platform skips this step);
When the cache space is full, the older resources will be deleted using the LRU algorithm at this point (there is no size limit on the native platform’s cache space, so skip this step, you can call manually for cleanup if needed).
The Cache Manager provides a getCache
interface to query all cache resources, and you can query the cache path of a resource by passing in its original path, for example:
After a resource is downloaded locally, it may be stored as a temporary file in a temporary directory. The Cache Manager provides a tempFiles
interface to query all resources downloaded to the temporary directory, which you can do by passing in the original path of the resource, for example:
- — Controls the period of caching a single resource, the default is once every 500ms.
cacheManager.cacheEnabled
— Controls whether or not to cache resources, which defaults to caching. Alternatively, you can override the global settings by specifying the optional parametercacheEnabled
, for example:
The cache manager provides three interfaces removeCache
, and clearLRU
to clean up cache resources.
removeCache
— Clean up a single cached resource, and you need to provide the original path of the resource when using it.clearCache
— Clean up all cache resources, please use it carefully.— Clean up older resources. The mini game platform will automatically call
clearLRU
when the cache space is full.