|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using Microsoft.Extensions.Caching.Distributed; |
| 3 | +using Microsoft.Extensions.Caching.StackExchangeRedis; |
| 4 | +using System.Collections.Concurrent; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Threading; |
| 7 | +using System; |
| 8 | +using GeneXus.Services; |
| 9 | +using System.Linq; |
| 10 | + |
| 11 | +namespace GeneXus.Application |
| 12 | +{ |
| 13 | + public class TenantRedisCache : IDistributedCache |
| 14 | + { |
| 15 | + private readonly IHttpContextAccessor _httpContextAccessor; |
| 16 | + private readonly IServiceProvider _serviceProvider; |
| 17 | + private readonly ConcurrentDictionary<string, RedisCache> _redisCaches = new(); |
| 18 | + |
| 19 | + public TenantRedisCache(IHttpContextAccessor httpContextAccessor, IServiceProvider serviceProvider) |
| 20 | + { |
| 21 | + _httpContextAccessor = httpContextAccessor; |
| 22 | + _serviceProvider = serviceProvider; |
| 23 | + } |
| 24 | + |
| 25 | + private IDistributedCache GetTenantCache() |
| 26 | + { |
| 27 | + string tenantId = _httpContextAccessor.HttpContext?.Items[TenantMiddleware.TENANT_ID]?.ToString() ?? "default"; |
| 28 | + |
| 29 | + return _redisCaches.GetOrAdd(tenantId, id => |
| 30 | + { |
| 31 | + ISessionService sessionService = GXSessionServiceFactory.GetProvider(); |
| 32 | + var options = new RedisCacheOptions |
| 33 | + { |
| 34 | + Configuration = sessionService.ConnectionString, |
| 35 | + InstanceName = $"{id}:" |
| 36 | + }; |
| 37 | + return new RedisCache(options); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + public byte[] Get(string key) => GetTenantCache().Get(key); |
| 42 | + public Task<byte[]> GetAsync(string key, CancellationToken token = default) => GetTenantCache().GetAsync(key, token); |
| 43 | + public void Refresh(string key) => GetTenantCache().Refresh(key); |
| 44 | + public Task RefreshAsync(string key, CancellationToken token = default) => GetTenantCache().RefreshAsync(key, token); |
| 45 | + public void Remove(string key) => GetTenantCache().Remove(key); |
| 46 | + public Task RemoveAsync(string key, CancellationToken token = default) => GetTenantCache().RemoveAsync(key, token); |
| 47 | + public void Set(string key, byte[] value, DistributedCacheEntryOptions options) => GetTenantCache().Set(key, value, options); |
| 48 | + public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default) => GetTenantCache().SetAsync(key, value, options, token); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public class TenantMiddleware |
| 53 | + { |
| 54 | + internal const string TENANT_ID = "TenantId"; |
| 55 | + private readonly RequestDelegate _next; |
| 56 | + |
| 57 | + public TenantMiddleware(RequestDelegate next) |
| 58 | + { |
| 59 | + _next = next; |
| 60 | + } |
| 61 | + |
| 62 | + public async Task Invoke(HttpContext context) |
| 63 | + { |
| 64 | + string host = context.Request.Host.Host; |
| 65 | + string subdomain = host.Split('.').FirstOrDefault(); |
| 66 | + context.Items[TENANT_ID] = subdomain; |
| 67 | + |
| 68 | + await _next(context); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments