public void DumpedDataCanBeReloaded() { var cache = new CacheWithFail <string, Data>(); cache.Add("Foo", new Data { String = "Foo", Int = 1 }); cache.Add("Bar", new Data { String = "Bar", Int = 2 }); var json = cache.DumpCacheAsJson(); var newCache = new CacheWithFail <string, Data>(); newCache.LoadCacheFromJson(json); Data data; newCache.TryGetValue("Foo", out data).Should().Be(CacheState.Found); data.String.Should().Be("Foo"); data.Int.Should().Be(1); newCache.TryGetValue("Bar", out data).Should().Be(CacheState.Found); data.String.Should().Be("Bar"); data.Int.Should().Be(2); }
public void DumpedDataCanBeReloaded() { var cache = new CacheWithFail<string, Data>(); cache.Add("Foo", new Data { String = "Foo", Int = 1 }); cache.Add("Bar", new Data { String = "Bar", Int = 2 }); var json = cache.DumpCacheAsJson(); var newCache = new CacheWithFail<string, Data>(); newCache.LoadCacheFromJson(json); Data data; newCache.TryGetValue("Foo", out data).Should().Be(CacheState.Found); data.String.Should().Be("Foo"); data.Int.Should().Be(1); newCache.TryGetValue("Bar", out data).Should().Be(CacheState.Found); data.String.Should().Be("Bar"); data.Int.Should().Be(2); }
public void LoadCacheFromJson(string json) { if (json.IsNullOrEmpty()) { return; } try { var caches = JsonConvert.DeserializeObject <Dictionary <string, string> >(json); string output; if (caches.TryGetValue("_seriesCache", out output)) { _seriesCache.LoadCacheFromJson(output); } } catch (Exception ex) { Debug.WriteLine("Failed to deserialize caches: " + ex.Message); _seriesCache.Clear(); } }