Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit acca60c

Browse files
committed
Added a version to host cache.
In #1021 we require the repository parent (in the case of forked repositories) - this data is returned from the API but wasn't getting stored in our cache, meaning that we have invalid data when data is returned from the cache as opposed to from the API. This adds a `cacheVersion` to host caches. This version is checked when the host cache is first loaded, and if it doesn't match the expected version, the cache is invalidated. In this way each time we make a change as required by #1021 we can bump `HostCacheFactory.CacheVersion` to ensure the cache won't be returning invalid data.
1 parent 1cccfe2 commit acca60c

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/GitHub.App/Factories/HostCacheFactory.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
using System;
22
using System.ComponentModel.Composition;
33
using System.IO;
4+
using System.Reactive.Linq;
45
using Akavache;
56
using GitHub.Primitives;
67
using Rothko;
78
using GitHub.Extensions;
9+
using System.Threading.Tasks;
810

911
namespace GitHub.Factories
1012
{
1113
[Export(typeof(IHostCacheFactory))]
1214
[PartCreationPolicy(CreationPolicy.Shared)]
1315
public class HostCacheFactory : IHostCacheFactory
1416
{
17+
const int CacheVersion = 1;
18+
const string CacheVersionKey = "cacheVersion";
19+
1520
readonly Lazy<IBlobCacheFactory> blobCacheFactory;
1621
readonly Lazy<IOperatingSystem> operatingSystem;
1722

@@ -22,7 +27,7 @@ public HostCacheFactory(Lazy<IBlobCacheFactory> blobCacheFactory, Lazy<IOperatin
2227
this.operatingSystem = operatingSystem;
2328
}
2429

25-
public IBlobCache Create(HostAddress hostAddress)
30+
public async Task<IBlobCache> Create(HostAddress hostAddress)
2631
{
2732
var environment = OperatingSystem.Environment;
2833
// For GitHub.com, the cache file name should be "api.github.com.cache.db"
@@ -34,7 +39,17 @@ public IBlobCache Create(HostAddress hostAddress)
3439

3540
// CreateDirectory is a noop if the directory already exists.
3641
OperatingSystem.Directory.CreateDirectory(Path.GetDirectoryName(userAccountPath));
37-
return BlobCacheFactory.CreateBlobCache(userAccountPath);
42+
43+
var cache = BlobCacheFactory.CreateBlobCache(userAccountPath);
44+
var version = await cache.GetOrCreateObject<int>(CacheVersionKey, () => 0);
45+
46+
if (version != CacheVersion)
47+
{
48+
await cache.InvalidateAll();
49+
await cache.InsertObject(CacheVersionKey, CacheVersion);
50+
}
51+
52+
return cache;
3853
}
3954

4055
IOperatingSystem OperatingSystem { get { return operatingSystem.Value; } }
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using Akavache;
22
using GitHub.Primitives;
33
using System;
4+
using System.Threading.Tasks;
45

56
namespace GitHub.Factories
67
{
78
public interface IHostCacheFactory : IDisposable
89
{
9-
IBlobCache Create(HostAddress hostAddress);
10+
Task<IBlobCache> Create(HostAddress hostAddress);
1011
}
1112
}

src/GitHub.App/Factories/RepositoryHostFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public RepositoryHostFactory(
4444
public async Task<IRepositoryHost> Create(HostAddress hostAddress)
4545
{
4646
var apiClient = await apiClientFactory.Create(hostAddress);
47-
var hostCache = hostCacheFactory.Create(hostAddress);
47+
var hostCache = await hostCacheFactory.Create(hostAddress);
4848
var modelService = new ModelService(apiClient, hostCache, avatarProvider);
4949
var host = new RepositoryHost(apiClient, modelService, loginManager, loginCache, usage);
5050
hosts.Add(host);

0 commit comments

Comments
 (0)