-
Notifications
You must be signed in to change notification settings - Fork 315
/
MemoryCacheTenantResolver.cs
114 lines (91 loc) · 3.96 KB
/
MemoryCacheTenantResolver.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SaasKit.Multitenancy
{
using System.Threading;
using Microsoft.Extensions.Primitives;
public abstract class MemoryCacheTenantResolver<TTenant> : ITenantResolver<TTenant>
{
protected readonly IMemoryCache cache;
protected readonly ILogger log;
public MemoryCacheTenantResolver(IMemoryCache cache, ILoggerFactory loggerFactory)
{
Ensure.Argument.NotNull(cache, nameof(cache));
Ensure.Argument.NotNull(loggerFactory, nameof(loggerFactory));
this.cache = cache;
this.log = loggerFactory.CreateLogger<MemoryCacheTenantResolver<TTenant>>();
}
protected virtual MemoryCacheEntryOptions CreateCacheEntryOptions()
{
return new MemoryCacheEntryOptions()
.SetSlidingExpiration(new TimeSpan(1, 0, 0));
}
private MemoryCacheEntryOptions GetCacheEntryOptions()
{
var cacheEntryOptions = CreateCacheEntryOptions();
if (DisposeTenantOnExpiration)
{
var changeToken = new CancellationTokenSource();
cacheEntryOptions
.RegisterPostEvictionCallback(
(key, value, reason, state) =>
{
DisposeTenantContext(key, value as TenantContext<TTenant>);
changeToken.Cancel();
})
.AddExpirationToken(new CancellationChangeToken(changeToken.Token));
}
return cacheEntryOptions;
}
protected virtual bool DisposeTenantOnExpiration => true;
protected virtual void DisposeTenantContext(object cacheKey, TenantContext<TTenant> tenantContext)
{
if (tenantContext != null)
{
log.LogDebug("Disposing TenantContext:{id} instance with key \"{cacheKey}\".", tenantContext.Id, cacheKey);
tenantContext.Dispose();
}
}
protected abstract string GetContextIdentifier(HttpContext context);
protected abstract IEnumerable<string> GetTenantIdentifiers(TenantContext<TTenant> context);
protected abstract Task<TenantContext<TTenant>> ResolveAsync(HttpContext context);
async Task<TenantContext<TTenant>> ITenantResolver<TTenant>.ResolveAsync(HttpContext context)
{
Ensure.Argument.NotNull(context, nameof(context));
// Obtain the key used to identify cached tenants from the current request
var cacheKey = GetContextIdentifier(context);
if (cacheKey == null)
{
return null;
}
var tenantContext = cache.Get(cacheKey) as TenantContext<TTenant>;
if (tenantContext == null)
{
log.LogDebug("TenantContext not present in cache with key \"{cacheKey}\". Attempting to resolve.", cacheKey);
tenantContext = await ResolveAsync(context);
if (tenantContext != null)
{
var tenantIdentifiers = GetTenantIdentifiers(tenantContext);
if (tenantIdentifiers != null)
{
var cacheEntryOptions = GetCacheEntryOptions();
log.LogDebug("TenantContext:{id} resolved. Caching with keys \"{tenantIdentifiers}\".", tenantContext.Id, tenantIdentifiers);
foreach (var identifier in tenantIdentifiers)
{
cache.Set(identifier, tenantContext, cacheEntryOptions);
}
}
}
}
else
{
log.LogDebug("TenantContext:{id} retrieved from cache with key \"{cacheKey}\".", tenantContext.Id, cacheKey);
}
return tenantContext;
}
}
}