public static bool Save(string id, ApiCacheObject obj, bool andClone = false) { if (!obj.ShouldCache()) { return(false); } Type type = obj.GetType(); if (!cache.ContainsKey(type)) { cache.Add(type, new Dictionary <string, CacheEntry>()); } Dictionary <string, CacheEntry> dictionary = cache[type]; if (!dictionary.ContainsKey(id)) { dictionary.Add(id, new CacheEntry { obj = obj, time = Time.get_realtimeSinceStartup() }); } else { dictionary[id].time = Time.get_realtimeSinceStartup(); dictionary[id].obj = obj; } if (andClone) { Save(id + "_copy", obj.Clone()); } return(true); }
private static bool TestFetch(Type t, string id, ApiCacheObject target, float maxCacheAge = 3600f) { if (t == null || target == null || string.IsNullOrEmpty(id)) { return(false); } if (!t.IsAssignableFrom(target.GetType())) { return(false); } if (!cache.ContainsKey(t)) { return(false); } Dictionary <string, CacheEntry> dictionary = cache[t]; if (!dictionary.ContainsKey(id)) { return(false); } if (dictionary[id].obj == null || !dictionary[id].obj.GetType().Equals(t) || (maxCacheAge > 0f && Time.get_realtimeSinceStartup() - dictionary[id].time > maxCacheAge)) { Invalidate(t, id); return(false); } return(true); }