Skip to main content

CacheManager

Purpose

The CacheManager class is used for storing and retrieving specific values from the client-side browser cache, using the localstorage-slim library.

Methods

getCacheEntry
// Retrieves an entry from the user's browser cache.
// If an entry is not a manual entry, then if the browser cache
// does not have the value, it is fetched via AJAX (hence the async).
static async getCacheEntry(entry, formData = null);

// Example usage
CacheManager.getCacheEntry(CacheManager.cVars.searchTagsList);

This method behaves differently depending on whether the value being retrieved is from the cVars or cManualVars objects, explained below.

getPaginatedCacheEntry
// Retrieves a a paginated entry from the user's browser cache.
// If the page doesn't exist, it is retrieved via the corresponding AJAX form
// specified in CacheManager.cPaginatedVars, and the ttl is NOT refreshed if a page already exists.
// TTL options: 0 (default, use the ttl from the CacheManager.cPaginatedVars entry),
// -1 (indefinite), or a custom value.
static async getCacheEntryPaginated(entry, keyReplacements = {}, formData = null, customTtl = 0);

// Example usage
let data = await CacheManager.getCacheEntryPaginated(CacheManager.cPaginatedVars.userCachedNotifications, {
  pageOffset: NavbarManager.notificationsOffset.toString(),
}, {
  offset: NavbarManager.notificationsOffset,
  markAsRead: "Y",
});

Variables: cVars and cManualVars

cVars

Each cVar is an object with an lsKey, ajaxUrl, and ttl field. An example is below:

searchTagsList: {
  lsKey: "deserted-chateau-search-tags-list",
  ajaxUrl: "/forms/search/GetTopTags",
  ttl: 60 * 60 * 24,
},

This tells the CacheManager which key the value is stored in within the browser cache, how long the cached value should remain valid for, and the AJAX form to use to retrieve a new copy of the value if needed.

cManualVars

These are similar to cVars, but have no ajaxUrl value; they must be populated manually, and most of these keys do not have an expiry ttl or have it set to null (do not expire).

cPaginatedVars

These are similar to cVars, but store pages of results rather than a single value object. The lsKey has at least one replaceable part, specified in [squareBrackets], which is replaced with the value given to keyReplacements in the CacheManager.getCacheEntryPaginated method.

The baseKey is used for deleting all cached pages of this key from the user's local storage when the TTL expires. The ttlKey is used to keep track of when the first page was cached, so that all pages for the key are cleared after the first page is older than the ttl.

userCachedNotifications: {
  lsKey: `deserted-chateau-user-cached-notifications-[pageOffset]`,
  baseKey: `deserted-chateau-user-cached-notifications`,
  ttlKey: `deserted-chateau-user-cached-notifications-ttl`,
  ajaxUrl: "/forms/user/GetUserNotifications",
  ttlTime: 60 * 15,
},