2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
namespace ASC.Core.Caching;
|
|
|
|
|
|
|
|
[Singletone]
|
|
|
|
class TenantServiceCache
|
2020-10-19 15:53:15 +00:00
|
|
|
{
|
2022-03-25 14:54:34 +00:00
|
|
|
private const string Key = "tenants";
|
2022-03-17 15:07:17 +00:00
|
|
|
private readonly TimeSpan _cacheExpiration;
|
2022-03-25 14:54:34 +00:00
|
|
|
internal readonly ICache Cache;
|
|
|
|
internal readonly ICacheNotify<TenantCacheItem> CacheNotifyItem;
|
|
|
|
internal readonly ICacheNotify<TenantSetting> CacheNotifySettings;
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
public TenantServiceCache(
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
ICacheNotify<TenantCacheItem> cacheNotifyItem,
|
|
|
|
ICacheNotify<TenantSetting> cacheNotifySettings,
|
|
|
|
ICache cache)
|
|
|
|
{
|
2022-03-25 14:54:34 +00:00
|
|
|
CacheNotifyItem = cacheNotifyItem;
|
|
|
|
CacheNotifySettings = cacheNotifySettings;
|
|
|
|
Cache = cache;
|
2022-02-15 11:52:43 +00:00
|
|
|
_cacheExpiration = TimeSpan.FromMinutes(2);
|
|
|
|
|
|
|
|
cacheNotifyItem.Subscribe((t) =>
|
|
|
|
{
|
|
|
|
var tenants = GetTenantStore();
|
|
|
|
tenants.Remove(t.TenantId);
|
|
|
|
tenants.Clear(coreBaseSettings);
|
|
|
|
}, CacheNotifyAction.InsertOrUpdate);
|
|
|
|
|
|
|
|
cacheNotifySettings.Subscribe((s) =>
|
|
|
|
{
|
2022-03-25 14:54:34 +00:00
|
|
|
Cache.Remove(s.Key);
|
2022-02-15 11:52:43 +00:00
|
|
|
}, CacheNotifyAction.Remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal TenantStore GetTenantStore()
|
|
|
|
{
|
2022-03-25 14:54:34 +00:00
|
|
|
var store = Cache.Get<TenantStore>(Key);
|
2022-02-15 11:52:43 +00:00
|
|
|
if (store == null)
|
|
|
|
{
|
|
|
|
store = new TenantStore();
|
2022-03-25 14:54:34 +00:00
|
|
|
Cache.Insert(Key, store, DateTime.UtcNow.Add(_cacheExpiration));
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2022-01-21 11:02:17 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
_byId.TryGetValue(id, out t);
|
2022-02-14 21:02:57 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
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.Id);
|
|
|
|
lock (_locker)
|
|
|
|
{
|
|
|
|
_byId[t.Id] = t;
|
|
|
|
_byDomain[t.Alias] = t;
|
|
|
|
if (!string.IsNullOrEmpty(t.MappedDomain))
|
2022-02-14 21:02:57 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
_byDomain[t.MappedDomain] = t;
|
2022-02-14 21:02:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (!string.IsNullOrEmpty(ip))
|
|
|
|
{
|
|
|
|
_byDomain[ip] = t;
|
2022-02-14 21:02:57 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-14 21:02:57 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void Remove(int id)
|
|
|
|
{
|
|
|
|
var t = Get(id);
|
|
|
|
if (t != null)
|
|
|
|
{
|
|
|
|
lock (_locker)
|
|
|
|
{
|
|
|
|
_byId.Remove(id);
|
|
|
|
_byDomain.Remove(t.Alias);
|
|
|
|
if (!string.IsNullOrEmpty(t.MappedDomain))
|
2022-02-14 21:02:57 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
_byDomain.Remove(t.MappedDomain);
|
2022-02-14 21:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-14 21:02:57 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
internal void Clear(CoreBaseSettings coreBaseSettings)
|
2020-02-20 08:05:10 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
if (!coreBaseSettings.Standalone)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_locker)
|
|
|
|
{
|
|
|
|
_byId.Clear();
|
|
|
|
_byDomain.Clear();
|
|
|
|
}
|
2020-02-20 08:05:10 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
class CachedTenantService : ITenantService
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
private readonly ITenantService _service;
|
|
|
|
private readonly ICacheNotify<TenantSetting> _cacheNotifySettings;
|
|
|
|
private readonly ICacheNotify<TenantCacheItem> _cacheNotifyItem;
|
|
|
|
private readonly TenantServiceCache _tenantServiceCache;
|
2022-03-17 15:07:17 +00:00
|
|
|
private readonly TimeSpan _settingsExpiration;
|
2022-03-25 14:54:34 +00:00
|
|
|
private readonly ICache _cache;
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
public CachedTenantService()
|
|
|
|
{
|
|
|
|
_settingsExpiration = TimeSpan.FromMinutes(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CachedTenantService(DbTenantService service, TenantServiceCache tenantServiceCache, ICache cache) : this()
|
|
|
|
{
|
2022-03-25 14:54:34 +00:00
|
|
|
_cache = cache;
|
2022-07-28 12:29:03 +00:00
|
|
|
_service = service ?? throw new ArgumentNullException(nameof(service));
|
|
|
|
_tenantServiceCache = tenantServiceCache;
|
|
|
|
_cacheNotifyItem = tenantServiceCache.CacheNotifyItem;
|
|
|
|
_cacheNotifySettings = tenantServiceCache.CacheNotifySettings;
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ValidateDomain(string domain)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
_service.ValidateDomain(domain);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<Tenant> GetTenants(string login, string passwordHash)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
return _service.GetTenants(login, passwordHash);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<Tenant> GetTenants(DateTime from, bool active = true)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
return _service.GetTenants(from, active);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
public IEnumerable<Tenant> GetTenants(List<int> ids)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
return _service.GetTenants(ids);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant GetTenant(int id)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
var tenants = _tenantServiceCache.GetTenantStore();
|
2022-02-15 11:52:43 +00:00
|
|
|
var t = tenants.Get(id);
|
|
|
|
if (t == null)
|
2020-02-20 08:05:10 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
t = _service.GetTenant(id);
|
2022-02-15 11:52:43 +00:00
|
|
|
if (t != null)
|
|
|
|
{
|
|
|
|
tenants.Insert(t);
|
|
|
|
}
|
2020-02-20 08:05:10 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant GetTenant(string domain)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
var tenants = _tenantServiceCache.GetTenantStore();
|
2022-02-15 11:52:43 +00:00
|
|
|
var t = tenants.Get(domain);
|
|
|
|
if (t == null)
|
2020-02-20 08:05:10 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
t = _service.GetTenant(domain);
|
2022-02-15 11:52:43 +00:00
|
|
|
if (t != null)
|
|
|
|
{
|
|
|
|
tenants.Insert(t);
|
|
|
|
}
|
2020-02-20 08:05:10 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return t;
|
2019-10-21 12:33:02 +00:00
|
|
|
}
|
2020-10-21 13:39:06 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public Tenant GetTenantForStandaloneWithoutAlias(string ip)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
var tenants = _tenantServiceCache.GetTenantStore();
|
2022-02-15 11:52:43 +00:00
|
|
|
var t = tenants.Get(ip);
|
|
|
|
if (t == null)
|
2020-02-20 14:57:48 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
t = _service.GetTenantForStandaloneWithoutAlias(ip);
|
2022-02-15 11:52:43 +00:00
|
|
|
if (t != null)
|
|
|
|
{
|
|
|
|
tenants.Insert(t, ip);
|
|
|
|
}
|
2021-11-16 17:40:15 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tenant SaveTenant(CoreSettings coreSettings, Tenant tenant)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
tenant = _service.SaveTenant(coreSettings, tenant);
|
|
|
|
_cacheNotifyItem.Publish(new TenantCacheItem() { TenantId = tenant.Id }, CacheNotifyAction.InsertOrUpdate);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return tenant;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveTenant(int id, bool auto = false)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
_service.RemoveTenant(id, auto);
|
|
|
|
_cacheNotifyItem.Publish(new TenantCacheItem() { TenantId = id }, CacheNotifyAction.InsertOrUpdate);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<TenantVersion> GetTenantVersions()
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
return _service.GetTenantVersions();
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-11-16 17:40:15 +00:00
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
data = _service.GetTenantSettings(tenant, key);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
_cache.Insert(cacheKey, data ?? Array.Empty<byte>(), DateTime.UtcNow + _settingsExpiration);
|
2021-11-16 17:40:15 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return data == null ? null : data.Length == 0 ? null : data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetTenantSettings(int tenant, string key, byte[] data)
|
|
|
|
{
|
2022-07-28 12:29:03 +00:00
|
|
|
_service.SetTenantSettings(tenant, key, data);
|
2022-02-15 11:52:43 +00:00
|
|
|
var cacheKey = string.Format("settings/{0}/{1}", tenant, key);
|
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
_cacheNotifySettings.Publish(new TenantSetting { Key = cacheKey }, CacheNotifyAction.Remove);
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|