2.9.16
Coherent GT
A modern user interface library for games
Disk cache

Coherent GT allows users to enable disk caching of resources loaded from the network. The functionality is analogous to the cache found in web browsers. Every resource loaded through an HTTP request is checked if available in the cache. If it is available and the server signals that there isn't a newer version of that resource, it'll be loaded from the cache.

Disk cache implementation

The application that uses Coherent GT is incharge of implementing the persistance of the cache. The simplest way is to save the resources passed by the library in files on disk. More advanced strategies are also possible. The application can save the resources in a database, package them in a proprietary program etc.

To enable the disk cache, developers need to implement the Coherent::UIGT::IDiskCache interface. The library will write 3 types of resources: an index 'file' that contains a list of cached urls, a headers 'file' for every resource with HTTP headers, a content 'file' with the actual data blob of a single resource. Although we use the term 'file' to describe those resources, you are free to persist them in any way suitable.

To enable the cache implementation, add it to the Coherent::UIGT::SystemSettings structure at system initialization.

// Disk Cache
m_DiskCache.reset(new FileDiskCache("disk_cache"));
cacheSettings.DiskCache = m_DiskCache.get();
cacheSettings.MaxCacheSize = 16 * 1024 * 1024;
settings.DiskCache = &cacheSettings;
auto system = InitializeUIGTSystem(COHERENT_UI_GT_LICENSE,
settings,
Coherent::LoggingGT::Debug,
&g_SimpleLogHandler);

The MaxCacheSize property puts an upper bound to the size of resources cached. If the limit is reached, the system will start requesting deletions of least-recently used resources until there is enough space for new ones.

All samples distributed support the file cache. The implementation can be seen in Samples/Common/Application.cpp. The FileDiskCache class in the file shows a simple caching scheme that saves the resources as files on disk.

Pre-populating the cache

To increase the starting speed of applications, developers can pre-populate the cache and distribute it. Any resource that has changed will automatically be updated by the server. Pre-populating the cache is very simple. Just run the application on the content you wish to cache. Then distribute the persisted data along the application.

Cache integrity verification

The disk cache might get corrupted by unexpected process termination. It is a client's responsibility to provide Coherent GT a valid cache. Coherent GT only validates the cached content by matching the expected length stored in the headers Content-Length field and the reported length of the cached content that is returned from the Coherent::UIGT::IDiskCache::GetContentSize call. That validation check covers most of the cases where the disk cache gets corrupted. In cases where the server doesn't send Content-Length field in the headers a similar check can be implemented by using the Coherent::UIGT::IDiskCache::OnFinishAppendContent call. Where additional integrity checks are needed, the client can generate checksums for the headers in the Coherent::UIGT::IDiskCache::SaveHeaders call and for the content in the Coherent::UIGT::IDiskCache::OnFinishAppendContent.