DocSpace-client/common/ASC.Core.Common/Caching/CachedTenantService.cs
pavelbannov dba3efb235 Merge branch 'feature/backend-refactor' into feature/asc-common-refactor
# Conflicts:
#	common/ASC.Api.Core/GlobalUsings.cs
#	common/ASC.Common/Caching/AscCache.cs
#	common/ASC.Common/Collections/CachedDictionaryBase.cs
#	common/ASC.Common/Collections/HttpRequestDictionary.cs
#	common/ASC.Common/DIHelper.cs
#	common/ASC.Common/Data/StreamExtension.cs
#	common/ASC.Common/Data/TempStream.cs
#	common/ASC.Common/DependencyInjection/AutofacExtension.cs
#	common/ASC.Common/Logging/Log.cs
#	common/ASC.Common/Logging/SelfCleaningTarget.cs
#	common/ASC.Common/Logging/SpecialFolderPathConverter.cs
#	common/ASC.Common/Mapping/MappingProfile.cs
#	common/ASC.Common/Security/AscRandom.cs
#	common/ASC.Common/Security/Authorizing/AuthorizingException.cs
#	common/ASC.Common/Security/Authorizing/Constants.cs
#	common/ASC.Common/Security/Authorizing/Domain/Role.cs
#	common/ASC.Common/Security/Cryptography/Hasher.cs
#	common/ASC.Common/Threading/DistributedTask.cs
#	common/ASC.Common/Threading/DistributedTaskQueue.cs
#	common/ASC.Common/Utils/DnsLookup.cs
#	common/ASC.Common/Utils/HtmlUtil.cs
#	common/ASC.Common/Utils/HttpRequestExtensions.cs
#	common/ASC.Common/Utils/JsonWebToken.cs
#	common/ASC.Common/Utils/MailAddressUtils.cs
#	common/ASC.Common/Utils/RandomString.cs
#	common/ASC.Common/Utils/TimeZoneConverter/TimeZoneConverter.cs
#	common/ASC.Common/Utils/VelocityFormatter.cs
#	common/ASC.Common/Utils/Wildcard.cs
#	common/ASC.Common/Web/MimeMapping.cs
#	common/ASC.Common/Web/VirtualPathUtility.cs
#	common/services/ASC.ClearEvents/Program.cs
#	products/ASC.Files/Core/ThirdPartyApp/BoxApp.cs
#	products/ASC.Files/Core/ThirdPartyApp/GoogleDriveApp.cs
2022-02-11 16:17:55 +03:00

306 lines
10 KiB
C#

/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Core.Caching
{
[Singletone]
class TenantServiceCache
{
private const string KEY = "tenants";
private TimeSpan CacheExpiration { get; set; }
internal ICache Cache { get; }
internal IEventBus<TenantCacheItem> CacheNotifyItem { get; }
internal IEventBus<TenantSetting> CacheNotifySettings { get; }
public TenantServiceCache(
CoreBaseSettings coreBaseSettings,
IEventBus<TenantCacheItem> cacheNotifyItem,
IEventBus<TenantSetting> cacheNotifySettings,
ICache cache)
{
CacheNotifyItem = cacheNotifyItem;
CacheNotifySettings = cacheNotifySettings;
Cache = cache;
CacheExpiration = TimeSpan.FromMinutes(2);
cacheNotifyItem.Subscribe((t) =>
{
var tenants = GetTenantStore();
tenants.Remove(t.TenantId);
tenants.Clear(coreBaseSettings);
}, ASC.Common.Caching.EventType.InsertOrUpdate);
cacheNotifySettings.Subscribe((s) =>
{
Cache.Remove(s.Key);
}, ASC.Common.Caching.EventType.Remove);
}
internal TenantStore GetTenantStore()
{
var store = Cache.Get<TenantStore>(KEY);
if (store == null)
{
store = new TenantStore();
Cache.Insert(KEY, store, DateTime.UtcNow.Add(CacheExpiration));
}
return store;
}
internal class TenantStore
{
private readonly Dictionary<int, Tenant> byId = new Dictionary<int, Tenant>();
private readonly Dictionary<string, Tenant> byDomain = new Dictionary<string, Tenant>();
private readonly object locker = new object();
public Tenant Get(int id)
{
Tenant t;
lock (locker)
{
byId.TryGetValue(id, out t);
}
return t;
}
public Tenant Get(string domain)
{
if (string.IsNullOrEmpty(domain)) return null;
Tenant t;
lock (locker)
{
byDomain.TryGetValue(domain, out t);
}
return t;
}
public void Insert(Tenant t, string ip = null)
{
if (t == null)
{
return;
}
Remove(t.TenantId);
lock (locker)
{
byId[t.TenantId] = t;
byDomain[t.TenantAlias] = t;
if (!string.IsNullOrEmpty(t.MappedDomain)) byDomain[t.MappedDomain] = t;
if (!string.IsNullOrEmpty(ip)) byDomain[ip] = t;
}
}
public void Remove(int id)
{
var t = Get(id);
if (t != null)
{
lock (locker)
{
byId.Remove(id);
byDomain.Remove(t.TenantAlias);
if (!string.IsNullOrEmpty(t.MappedDomain))
{
byDomain.Remove(t.MappedDomain);
}
}
}
}
internal void Clear(CoreBaseSettings coreBaseSettings)
{
if (!coreBaseSettings.Standalone) return;
lock (locker)
{
byId.Clear();
byDomain.Clear();
}
}
}
}
[Scope]
class ConfigureCachedTenantService : IConfigureNamedOptions<CachedTenantService>
{
private IOptionsSnapshot<DbTenantService> Service { get; }
private TenantServiceCache TenantServiceCache { get; }
public ConfigureCachedTenantService(
IOptionsSnapshot<DbTenantService> service,
TenantServiceCache tenantServiceCache)
{
Service = service;
TenantServiceCache = tenantServiceCache;
}
public void Configure(string name, CachedTenantService options)
{
Configure(options);
options.Service = Service.Get(name);
}
public void Configure(CachedTenantService options)
{
options.Service = Service.Value;
options.TenantServiceCache = TenantServiceCache;
options.CacheNotifyItem = TenantServiceCache.CacheNotifyItem;
options.CacheNotifySettings = TenantServiceCache.CacheNotifySettings;
}
}
[Scope]
class CachedTenantService : ITenantService
{
internal ITenantService Service { get; set; }
private readonly ICache cache;
internal IEventBus<TenantSetting> CacheNotifySettings { get; set; }
internal IEventBus<TenantCacheItem> CacheNotifyItem { get; set; }
private TimeSpan SettingsExpiration { get; set; }
internal TenantServiceCache TenantServiceCache { get; set; }
public CachedTenantService()
{
SettingsExpiration = TimeSpan.FromMinutes(2);
}
public CachedTenantService(DbTenantService service, TenantServiceCache tenantServiceCache, ICache cache) : this()
{
this.cache = cache;
Service = service ?? throw new ArgumentNullException(nameof(service));
TenantServiceCache = tenantServiceCache;
CacheNotifyItem = tenantServiceCache.CacheNotifyItem;
CacheNotifySettings = tenantServiceCache.CacheNotifySettings;
}
public void ValidateDomain(string domain)
{
Service.ValidateDomain(domain);
}
public IEnumerable<Tenant> GetTenants(string login, string passwordHash)
{
return Service.GetTenants(login, passwordHash);
}
public IEnumerable<Tenant> GetTenants(DateTime from, bool active = true)
{
return Service.GetTenants(from, active);
}
public IEnumerable<Tenant> GetTenants(List<int> ids)
{
return Service.GetTenants(ids);
}
public Tenant GetTenant(int id)
{
var tenants = TenantServiceCache.GetTenantStore();
var t = tenants.Get(id);
if (t == null)
{
t = Service.GetTenant(id);
if (t != null)
{
tenants.Insert(t);
}
}
return t;
}
public Tenant GetTenant(string domain)
{
var tenants = TenantServiceCache.GetTenantStore();
var t = tenants.Get(domain);
if (t == null)
{
t = Service.GetTenant(domain);
if (t != null)
{
tenants.Insert(t);
}
}
return t;
}
public Tenant GetTenantForStandaloneWithoutAlias(string ip)
{
var tenants = TenantServiceCache.GetTenantStore();
var t = tenants.Get(ip);
if (t == null)
{
t = Service.GetTenantForStandaloneWithoutAlias(ip);
if (t != null)
{
tenants.Insert(t, ip);
}
}
return t;
}
public Tenant SaveTenant(CoreSettings coreSettings, Tenant tenant)
{
tenant = Service.SaveTenant(coreSettings, tenant);
CacheNotifyItem.Publish(new TenantCacheItem() { TenantId = tenant.TenantId }, ASC.Common.Caching.EventType.InsertOrUpdate);
return tenant;
}
public void RemoveTenant(int id, bool auto = false)
{
Service.RemoveTenant(id, auto);
CacheNotifyItem.Publish(new TenantCacheItem() { TenantId = id }, ASC.Common.Caching.EventType.InsertOrUpdate);
}
public IEnumerable<TenantVersion> GetTenantVersions()
{
return Service.GetTenantVersions();
}
public byte[] GetTenantSettings(int tenant, string key)
{
var cacheKey = string.Format("settings/{0}/{1}", tenant, key);
var data = cache.Get<byte[]>(cacheKey);
if (data == null)
{
data = Service.GetTenantSettings(tenant, key);
cache.Insert(cacheKey, data ?? Array.Empty<byte>(), DateTime.UtcNow + SettingsExpiration);
}
return data == null ? null : data.Length == 0 ? null : data;
}
public void SetTenantSettings(int tenant, string key, byte[] data)
{
Service.SetTenantSettings(tenant, key, data);
var cacheKey = string.Format("settings/{0}/{1}", tenant, key);
CacheNotifySettings.Publish(new TenantSetting { Key = cacheKey }, ASC.Common.Caching.EventType.Remove);
}
}
}