11using System ;
22using System . ComponentModel . Composition ;
33using System . IO ;
4+ using System . Reactive . Linq ;
45using Akavache ;
56using GitHub . Primitives ;
67using Rothko ;
78using GitHub . Extensions ;
9+ using System . Threading . Tasks ;
810
911namespace 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 ; } }
0 commit comments