This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSharedCache.cs
More file actions
96 lines (84 loc) · 2.84 KB
/
Copy pathSharedCache.cs
File metadata and controls
96 lines (84 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.ComponentModel.Composition;
using System.Globalization;
using System.Reactive;
using System.Reactive.Linq;
using Akavache;
using GitHub.Logging;
using Serilog;
namespace GitHub.Caches
{
/// <summary>
/// A cache for data that's not host specific
/// </summary>
[Export(typeof(ISharedCache))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class SharedCache : ISharedCache
{
const string enterpriseHostApiBaseUriCacheKey = "enterprise-host-api-base-uri";
static readonly ILogger log = LogManager.ForContext<SharedCache>();
static SharedCache()
{
try
{
BlobCache.ApplicationName = Info.ApplicationInfo.ApplicationName;
}
catch (Exception e)
{
log.Error(e, "Error while running the static inializer for SharedCache");
}
}
public SharedCache() : this(null, null)
{
}
protected SharedCache(IBlobCache userAccountCache, IBlobCache localMachineCache)
{
UserAccount = userAccountCache ?? GetBlobCacheWithFallback(() => BlobCache.UserAccount, "UserAccount");
LocalMachine = localMachineCache ?? GetBlobCacheWithFallback(() => BlobCache.LocalMachine, "LocalMachine");
}
public IBlobCache UserAccount { get; private set; }
public IBlobCache LocalMachine { get; private set; }
public IObservable<Uri> GetEnterpriseHostUri()
{
return UserAccount.GetObject<Uri>(enterpriseHostApiBaseUriCacheKey);
}
public IObservable<Unit> InsertEnterpriseHostUri(Uri enterpriseHostUri)
{
return UserAccount.InsertObject(enterpriseHostApiBaseUriCacheKey, enterpriseHostUri);
}
public IObservable<Unit> InvalidateEnterpriseHostUri()
{
return UserAccount.InvalidateObject<Uri>(enterpriseHostApiBaseUriCacheKey);
}
bool disposed;
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (disposed) return;
UserAccount.Dispose();
UserAccount.Shutdown.Wait();
LocalMachine.Dispose();
LocalMachine.Shutdown.Wait();
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
static IBlobCache GetBlobCacheWithFallback(Func<IBlobCache> blobCacheFunc, string cacheName)
{
try
{
return blobCacheFunc();
}
catch (Exception e)
{
log.Error(e, "Failed to set the {CacheName} cache", cacheName);
return new InMemoryBlobCache();
}
}
}
}