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

Commit 88821ed

Browse files
authored
Merge pull request #1023 from github/feature/cache-versioning
Added a version to host cache.
2 parents d3b561d + 31b12e9 commit 88821ed

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)