-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachingResolver.cs
37 lines (33 loc) · 1.13 KB
/
CachingResolver.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
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using SaasKit.Multitenancy;
namespace nats_cache_test
{
public class CachingResolver : MemoryCacheTenantResolver<Tenant>
{
public CachingResolver(
IMemoryCache cache,
ILoggerFactory loggerFactory
) : base(cache, loggerFactory)
{
}
protected override string GetContextIdentifier(HttpContext context)
{
return context.Request.Headers.FirstOrDefault(s => s.Key.Equals("X-Tenant-Id")).Value;
}
protected override IEnumerable<string> GetTenantIdentifiers(TenantContext<Tenant> context)
{
return new List<string> { context.Tenant.Id };
}
protected override Task<TenantContext<Tenant>> ResolveAsync(HttpContext context)
{
var tenantId = GetContextIdentifier(context);
return Task.Run(() => {
return new TenantContext<Tenant>(new Tenant { Id = tenantId }); });
}
}
}