Beispiel #1
0
        /// <summary>Generates the string for debugging the data in the request cache.</summary>
        public static string GenerateDebugInfo(int responseBodyCharacterLimit)
        {
            var s = new System.Text.StringBuilder();

            s.AppendLine("currentCacheSize="
                         + ValueFormatting.ByteCount(RequestCache.currentCacheSize, "0.0")
                         + "/"
                         + ValueFormatting.ByteCount(RequestCache.MAX_CACHE_SIZE, "0.0"));

            s.AppendLine("constructedCache=");

            foreach (var kvp in RequestCache.urlResponseIndexMap)
            {
                s.AppendLine("[" + kvp.Value.ToString("00") + "]:" + kvp.Key);

                if (kvp.Value < RequestCache.responses.Count)
                {
                    Entry e = RequestCache.responses[kvp.Value];

                    s.AppendLine("> "
                                 + ServerTimeStamp.ToLocalDateTime(e.timeStamp).ToString()
                                 + " [+" + (ServerTimeStamp.Now - e.timeStamp).ToString() + "s] -- "
                                 + ValueFormatting.ByteCount(e.size, "0.00"));

                    string r = e.responseBody;
                    if (string.IsNullOrEmpty(r))
                    {
                        r = "[NULL-OR-EMPTY]";
                    }
                    else
                    {
                        r = r.Substring(0, UnityEngine.Mathf.Min(responseBodyCharacterLimit, r.Length));
                    }

                    s.AppendLine("> " + r);
                }
                else
                {
                    s.AppendLine("[BAD INDEX!!!]:" + kvp.Key.ToString());
                }
            }

            return(s.ToString());
        }
Beispiel #2
0
        /// <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;
        }