Browse Source

Consistent cache keys

pull/613/head
Sebastian 5 years ago
parent
commit
088af92fd0
  1. 8
      backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/EventEnricher.cs
  2. 29
      backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Internal/Parser.cs
  3. 4
      backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs
  4. 21
      backend/src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsIndex.cs
  5. 4
      backend/src/Squidex.Domain.Apps.Entities/Rules/RuleEnqueuer.cs
  6. 21
      backend/src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasIndex.cs
  7. 4
      backend/src/Squidex.Infrastructure/UsageTracking/CachingUsageTracker.cs

8
backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/EventEnricher.cs

@ -18,7 +18,7 @@ namespace Squidex.Domain.Apps.Core.HandleRules
{ {
public sealed class EventEnricher : IEventEnricher public sealed class EventEnricher : IEventEnricher
{ {
private static readonly TimeSpan UserCacheDuration = TimeSpan.FromMinutes(10); private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(10);
private readonly IMemoryCache userCache; private readonly IMemoryCache userCache;
private readonly IUserResolver userResolver; private readonly IUserResolver userResolver;
@ -56,11 +56,11 @@ namespace Squidex.Domain.Apps.Core.HandleRules
private Task<IUser?> FindUserAsync(RefToken actor) private Task<IUser?> FindUserAsync(RefToken actor)
{ {
var key = $"EventEnrichers_Users_{actor.Identifier}"; var cacheKey = $"{typeof(EventEnricher)}_Users_{actor.Identifier}";
return userCache.GetOrCreateAsync(key, async x => return userCache.GetOrCreateAsync(cacheKey, async x =>
{ {
x.AbsoluteExpirationRelativeToNow = UserCacheDuration; x.AbsoluteExpirationRelativeToNow = CacheDuration;
IUser? user; IUser? user;
try try

29
backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/Internal/Parser.cs

@ -15,40 +15,33 @@ namespace Squidex.Domain.Apps.Core.Scripting.Internal
{ {
internal sealed class Parser internal sealed class Parser
{ {
private static readonly TimeSpan Expiration = TimeSpan.FromMinutes(10); private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(10);
private static readonly ParserOptions DefaultParserOptions = new ParserOptions private static readonly ParserOptions DefaultParserOptions = new ParserOptions
{ {
AdaptRegexp = true, Tolerant = true, Loc = true AdaptRegexp = true, Tolerant = true, Loc = true
}; };
private readonly IMemoryCache memoryCache; private readonly IMemoryCache cache;
public Parser(IMemoryCache memoryCache) public Parser(IMemoryCache cache)
{ {
Guard.NotNull(memoryCache, nameof(memoryCache)); Guard.NotNull(cache, nameof(cache));
this.memoryCache = memoryCache; this.cache = cache;
} }
public Script Parse(string script) public Script Parse(string script)
{ {
var key = Key(script); var cacheKey = $"{typeof(Parser)}_Script_{script}";
if (!memoryCache.TryGetValue<Script>(key, out var compiledScript)) return cache.GetOrCreate(cacheKey, entry =>
{ {
var parser = new JavaScriptParser(script, DefaultParserOptions); entry.AbsoluteExpirationRelativeToNow = CacheDuration;
compiledScript = parser.ParseScript();
memoryCache.Set(key, compiledScript, Expiration);
}
return compiledScript; var parser = new JavaScriptParser(script, DefaultParserOptions);
}
private static string Key(string script) return parser.ParseScript();
{ });
return $"SCRIPT_{script}";
} }
} }
} }

4
backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/JintScriptEngine.cs

@ -34,9 +34,9 @@ namespace Squidex.Domain.Apps.Core.Scripting
public TimeSpan TimeoutExecution { get; set; } = TimeSpan.FromMilliseconds(4000); public TimeSpan TimeoutExecution { get; set; } = TimeSpan.FromMilliseconds(4000);
public JintScriptEngine(IMemoryCache memoryCache, IEnumerable<IJintExtension>? extensions = null) public JintScriptEngine(IMemoryCache cache, IEnumerable<IJintExtension>? extensions = null)
{ {
parser = new Parser(memoryCache); parser = new Parser(cache);
this.extensions = extensions?.ToArray() ?? Array.Empty<IJintExtension>(); this.extensions = extensions?.ToArray() ?? Array.Empty<IJintExtension>();
} }

21
backend/src/Squidex.Domain.Apps.Entities/Apps/Indexes/AppsIndex.cs

@ -29,16 +29,15 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes
{ {
private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(5); private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(5);
private readonly IGrainFactory grainFactory; private readonly IGrainFactory grainFactory;
private readonly IReplicatedCache replicatedCache; private readonly IReplicatedCache grainCache;
public AppsIndex(IGrainFactory grainFactory, IReplicatedCache replicatedCache) public AppsIndex(IGrainFactory grainFactory, IReplicatedCache grainCache)
{ {
Guard.NotNull(grainFactory, nameof(grainFactory)); Guard.NotNull(grainFactory, nameof(grainFactory));
Guard.NotNull(replicatedCache, nameof(replicatedCache)); Guard.NotNull(grainCache, nameof(grainCache));
this.grainFactory = grainFactory; this.grainFactory = grainFactory;
this.grainCache = grainCache;
this.replicatedCache = replicatedCache;
} }
public async Task RebuildByContributorsAsync(DomainId appId, HashSet<string> contributors) public async Task RebuildByContributorsAsync(DomainId appId, HashSet<string> contributors)
@ -117,7 +116,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes
{ {
if (canCache) if (canCache)
{ {
if (replicatedCache.TryGetValue(GetCacheKey(name), out var v) && v is IAppEntity cacheApp) if (grainCache.TryGetValue(GetCacheKey(name), out var v) && v is IAppEntity cacheApp)
{ {
return cacheApp; return cacheApp;
} }
@ -140,7 +139,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes
{ {
if (canCache) if (canCache)
{ {
if (replicatedCache.TryGetValue(GetCacheKey(appId), out var v) && v is IAppEntity cachedApp) if (grainCache.TryGetValue(GetCacheKey(appId), out var v) && v is IAppEntity cachedApp)
{ {
return cachedApp; return cachedApp;
} }
@ -312,19 +311,19 @@ namespace Squidex.Domain.Apps.Entities.Apps.Indexes
private static string GetCacheKey(DomainId id) private static string GetCacheKey(DomainId id)
{ {
return $"APPS_ID_{id}"; return $"{typeof(AppsIndex)}_Apps_Id_{id}";
} }
private static string GetCacheKey(string name) private static string GetCacheKey(string name)
{ {
return $"APPS_NAME_{name}"; return $"{typeof(AppsIndex)}_Apps_Name_{name}";
} }
private Task CacheItAsync(IAppEntity app, bool publish) private Task CacheItAsync(IAppEntity app, bool publish)
{ {
return Task.WhenAll( return Task.WhenAll(
replicatedCache.AddAsync(GetCacheKey(app.Id), app, CacheDuration, publish), grainCache.AddAsync(GetCacheKey(app.Id), app, CacheDuration, publish),
replicatedCache.AddAsync(GetCacheKey(app.Name), app, CacheDuration, publish)); grainCache.AddAsync(GetCacheKey(app.Name), app, CacheDuration, publish));
} }
} }
} }

4
backend/src/Squidex.Domain.Apps.Entities/Rules/RuleEnqueuer.cs

@ -82,7 +82,9 @@ namespace Squidex.Domain.Apps.Entities.Rules
private Task<List<IRuleEntity>> GetRulesAsync(DomainId appId) private Task<List<IRuleEntity>> GetRulesAsync(DomainId appId)
{ {
return cache.GetOrCreateAsync(appId, entry => var cacheKey = $"{typeof(RuleEnqueuer)}_Rules_{appId}";
return cache.GetOrCreateAsync(cacheKey, entry =>
{ {
entry.AbsoluteExpirationRelativeToNow = CacheDuration; entry.AbsoluteExpirationRelativeToNow = CacheDuration;

21
backend/src/Squidex.Domain.Apps.Entities/Schemas/Indexes/SchemasIndex.cs

@ -26,16 +26,15 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
{ {
private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(5); private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(5);
private readonly IGrainFactory grainFactory; private readonly IGrainFactory grainFactory;
private readonly IReplicatedCache replicatedCache; private readonly IReplicatedCache grainCache;
public SchemasIndex(IGrainFactory grainFactory, IReplicatedCache replicatedCache) public SchemasIndex(IGrainFactory grainFactory, IReplicatedCache grainCache)
{ {
Guard.NotNull(grainFactory, nameof(grainFactory)); Guard.NotNull(grainFactory, nameof(grainFactory));
Guard.NotNull(replicatedCache, nameof(replicatedCache)); Guard.NotNull(grainCache, nameof(grainCache));
this.grainFactory = grainFactory; this.grainFactory = grainFactory;
this.grainCache = grainCache;
this.replicatedCache = replicatedCache;
} }
public Task RebuildAsync(DomainId appId, Dictionary<string, DomainId> schemas) public Task RebuildAsync(DomainId appId, Dictionary<string, DomainId> schemas)
@ -65,7 +64,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
if (canCache) if (canCache)
{ {
if (replicatedCache.TryGetValue(cacheKey, out var v) && v is ISchemaEntity cachedSchema) if (grainCache.TryGetValue(cacheKey, out var v) && v is ISchemaEntity cachedSchema)
{ {
return cachedSchema; return cachedSchema;
} }
@ -90,7 +89,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
if (canCache) if (canCache)
{ {
if (replicatedCache.TryGetValue(cacheKey, out var v) && v is ISchemaEntity cachedSchema) if (grainCache.TryGetValue(cacheKey, out var v) && v is ISchemaEntity cachedSchema)
{ {
return cachedSchema; return cachedSchema;
} }
@ -214,19 +213,19 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Indexes
private static string GetCacheKey(DomainId appId, string name) private static string GetCacheKey(DomainId appId, string name)
{ {
return $"SCHEMAS_NAME_{appId}_{name}"; return $"{typeof(SchemasIndex)}_Schemas_Name_{appId}_{name}";
} }
private static string GetCacheKey(DomainId appId, DomainId id) private static string GetCacheKey(DomainId appId, DomainId id)
{ {
return $"SCHEMAS_ID_{appId}_{id}"; return $"{typeof(SchemasIndex)}_Schemas_Id_{appId}_{id}";
} }
private Task CacheItAsync(ISchemaEntity schema, bool publish) private Task CacheItAsync(ISchemaEntity schema, bool publish)
{ {
return Task.WhenAll( return Task.WhenAll(
replicatedCache.AddAsync(GetCacheKey(schema.AppId.Id, schema.Id), schema, CacheDuration, publish), grainCache.AddAsync(GetCacheKey(schema.AppId.Id, schema.Id), schema, CacheDuration, publish),
replicatedCache.AddAsync(GetCacheKey(schema.AppId.Id, schema.SchemaDef.Name), schema, CacheDuration, publish)); grainCache.AddAsync(GetCacheKey(schema.AppId.Id, schema.SchemaDef.Name), schema, CacheDuration, publish));
} }
} }
} }

4
backend/src/Squidex.Infrastructure/UsageTracking/CachingUsageTracker.cs

@ -45,7 +45,7 @@ namespace Squidex.Infrastructure.UsageTracking
{ {
Guard.NotNull(key, nameof(key)); Guard.NotNull(key, nameof(key));
var cacheKey = string.Join("$", "Usage", nameof(GetForMonthAsync), key, date, category); var cacheKey = $"{typeof(CachingUsageTracker)}_UsageForMonth_{key}_{date}_{category}";
return cache.GetOrCreateAsync(cacheKey, entry => return cache.GetOrCreateAsync(cacheKey, entry =>
{ {
@ -59,7 +59,7 @@ namespace Squidex.Infrastructure.UsageTracking
{ {
Guard.NotNull(key, nameof(key)); Guard.NotNull(key, nameof(key));
var cacheKey = string.Join("$", "Usage", nameof(GetAsync), key, fromDate, toDate, category); var cacheKey = $"{typeof(CachingUsageTracker)}_Usage_{key}_{fromDate}_{toDate}_{category}";
return cache.GetOrCreateAsync(cacheKey, entry => return cache.GetOrCreateAsync(cacheKey, entry =>
{ {

Loading…
Cancel
Save