// ---------[ Access Interface ]--------- /// <summary>Fetches a response from the cache.</summary> public static bool TryGetResponse(string url, out string response) { response = null; string endpointURL = null; // try to remove the apiURL if (!RequestCache.TryTrimAPIURLAndKey(url, out endpointURL)) { return(false); } bool success = false; 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 { response = entry.responseBody; success = true; } } return(success); }
/// <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>Removes entries from the cache until the total cache size is below the given value.</summary> private static void TrimCacheToMaxSize(uint maxSize) { uint trimmedSize = RequestCache.currentCacheSize; int lastIndex; for (lastIndex = 0; lastIndex < RequestCache.responses.Count && trimmedSize > maxSize; ++lastIndex) { trimmedSize -= RequestCache.responses[lastIndex].size; } if (trimmedSize > 0) { RequestCache.RemoveOldestEntries(lastIndex + 1); } else { RequestCache.Clear(); } }
/// <summary>Stores a response in the cache.</summary> public static void StoreResponse(string url, string responseBody) { if (LocalUser.OAuthToken != RequestCache.lastOAuthToken) { RequestCache.Clear(); RequestCache.lastOAuthToken = LocalUser.OAuthToken; } string endpointURL = null; // try to remove the apiURL if (!RequestCache.TryTrimAPIURLAndKey(url, out endpointURL)) { Debug.LogWarning("[mod.io] Attempted to cache response for url that does not contain the api URL." + "\nRequest URL: " + (url == null ? "NULL" : url)); return; } // remove stale entry int oldIndex; Entry oldValue; if (RequestCache.TryGetEntry(endpointURL, out oldIndex, out oldValue)) { Debug.LogWarning("[mod.io] Stale cached request found. Removing all older entries."); RequestCache.RemoveOldestEntries(oldIndex + 1); } // calculate new entry size uint size = 0; if (responseBody != null) { size = (uint)responseBody.Length * sizeof(char); } // trim cache if necessary if (size > RequestCache.MAX_CACHE_SIZE) { Debug.Log("[mod.io] Could not cache entry as the response body is larger than MAX_CACHE_SIZE." + "\nMAX_CACHE_SIZE=" + ValueFormatting.ByteCount(RequestCache.MAX_CACHE_SIZE, "0.0") + "\nendpointURL=" + endpointURL + "\nResponseBody Size=" + ValueFormatting.ByteCount(size, "0.0")); return; } if (RequestCache.currentCacheSize + size > RequestCache.MAX_CACHE_SIZE) { RequestCache.TrimCacheToMaxSize(RequestCache.MAX_CACHE_SIZE - size); } // add new entry Entry newValue = new Entry() { timeStamp = ServerTimeStamp.Now, responseBody = responseBody, size = size, }; RequestCache.urlResponseIndexMap.Add(endpointURL, RequestCache.responses.Count); RequestCache.responses.Add(newValue); RequestCache.currentCacheSize += size; }