From ac16e66325570ce926a3485cce793037c09f7e20 Mon Sep 17 00:00:00 2001 From: Andreia Gaita Date: Thu, 1 Feb 2018 16:19:52 +0100 Subject: [PATCH] Make sure we always create a valid index if the database has old/invalid json data Fixes #1319 --- src/GitHub.App/Caches/CacheIndex.cs | 1 + src/GitHub.App/Extensions/AkavacheExtensions.cs | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/GitHub.App/Caches/CacheIndex.cs b/src/GitHub.App/Caches/CacheIndex.cs index a3eca8a0e8..d3ad77de7f 100644 --- a/src/GitHub.App/Caches/CacheIndex.cs +++ b/src/GitHub.App/Caches/CacheIndex.cs @@ -62,6 +62,7 @@ public static IObservable AddAndSaveToIndex(IBlobCache cache, string Guard.ArgumentNotNull(item, nameof(item)); return cache.GetOrCreateObject(indexKey, () => Create(indexKey)) + .Select(x => x.IndexKey == null ? Create(indexKey) : x) .Do(index => { var k = string.Format(CultureInfo.InvariantCulture, "{0}|{1}", index.IndexKey, item.Key); diff --git a/src/GitHub.App/Extensions/AkavacheExtensions.cs b/src/GitHub.App/Extensions/AkavacheExtensions.cs index a95e4c5c8d..bbf59bed19 100644 --- a/src/GitHub.App/Extensions/AkavacheExtensions.cs +++ b/src/GitHub.App/Extensions/AkavacheExtensions.cs @@ -200,7 +200,11 @@ static IObservable GetAndFetchLatestFromIndex(this IBlobCache This, bool shouldInvalidateOnError = false) where T : CacheItem { - var idx = Observable.Defer(() => This.GetOrCreateObject(key, () => CacheIndex.Create(key))).Replay().RefCount(); + var idx = Observable.Defer(() => This + .GetOrCreateObject(key, () => CacheIndex.Create(key))) + .Select(x => x.IndexKey == null ? CacheIndex.Create(key) : x) + .Replay() + .RefCount(); var fetch = idx @@ -264,6 +268,7 @@ public static IObservable PutAndUpdateIndex(this IBlobCache blobCache, { var absoluteExpiration = blobCache.Scheduler.Now + maxCacheDuration; return blobCache.GetOrCreateObject(key, () => CacheIndex.Create(key)) + .Select(x => x.IndexKey == null ? CacheIndex.Create(key) : x) .SelectMany(index => fetchFunc() .Catch(Observable.Throw) .SelectMany(x => x.Save(blobCache, key, absoluteExpiration))