/// <summary>Fetches a Mod Profile from the cache if available.</summary> public static bool TryGetMod(int gameId, int modId, out ModProfile profile) { profile = null; bool success = false; string endpointURL = APIClient.BuildGetModEndpointURL(gameId, modId); Entry entry; int entryIndex; if (LocalUser.OAuthToken == RequestCache.lastOAuthToken && RequestCache.TryGetEntry(endpointURL, out entryIndex, out entry)) { // check if stale if ((ServerTimeStamp.Now - entry.timeStamp) >= RequestCache.ENTRY_LIFETIME) { // clear it, and any entries older than it RequestCache.RemoveOldestEntries(entryIndex + 1); } else { string modJSON = entry.responseBody; profile = JsonConvert.DeserializeObject <ModProfile>(modJSON); success = true; } } return(success); }
/// <summary>Stores a collection of mods in the response cache.</summary> public static void StoreMods(int gameId, IEnumerable <ModProfile> mods) { if (mods == null) { return; } List <string> endpointList = new List <string>(); List <Entry> entryList = new List <Entry>(); int now = ServerTimeStamp.Now; uint culmativeSize = 0; foreach (var mod in mods) { if (mod != null) { string endpointURL = APIClient.BuildGetModEndpointURL(gameId, mod.id); // skip if already cached if (RequestCache.urlResponseIndexMap.ContainsKey(endpointURL)) { continue; } string modJSON = JsonConvert.SerializeObject(mod); uint size = (uint)modJSON.Length * sizeof(char); // break out if size is greater than max cache size if (culmativeSize + size >= RequestCache.MAX_CACHE_SIZE) { break; } else { endpointList.Add(endpointURL); entryList.Add(new Entry() { timeStamp = now, size = size, responseBody = modJSON, }); culmativeSize += size; } } } // early out if no mods to add if (culmativeSize == 0) { return; } // make space in cache if (culmativeSize + RequestCache.currentCacheSize > RequestCache.MAX_CACHE_SIZE) { RequestCache.TrimCacheToMaxSize(RequestCache.MAX_CACHE_SIZE - culmativeSize); } // add entries int indexBase = RequestCache.responses.Count; for (int i = 0; i < endpointList.Count; ++i) { RequestCache.urlResponseIndexMap.Add(endpointList[i], i + indexBase); } RequestCache.responses.AddRange(entryList); RequestCache.currentCacheSize += culmativeSize; }