public async Task <IAppEntity?> GetAppAsync(DomainId appId, bool canCache = false) { var app = await localCache.GetOrCreateAsync(AppCacheKey(appId), () => { return(indexForApps.GetAppAsync(appId, canCache)); }); if (app != null) { localCache.Add(AppCacheKey(app.Id), app); } return(app?.IsArchived == true ? null : app); }
public async Task Should_add_item_to_cache_when_context_exists() { using (sut.StartContext()) { sut.Add("Key", 1); await Task.Delay(5); AssertCache(sut, "Key", 1, true); await Task.Delay(5); sut.Remove("Key"); AssertCache(sut, "Key", null, false); } }
public async Task <IAppEntity?> GetAppAsync(DomainId appId, bool canCache = false) { var cacheKey = AppCacheKey(appId); if (localCache.TryGetValue(cacheKey, out var cached) && cached is IAppEntity found) { return(found); } var app = await indexForApps.GetAppAsync(appId, canCache); if (app != null) { localCache.Add(cacheKey, app); localCache.Add(AppCacheKey(app.Name), app); } return(app); }
public static T GetOrCreate <T>(this ILocalCache cache, object key, Func <T> task) { if (cache.TryGetValue(key, out var value) && value is T typedValue) { return(typedValue); } typedValue = task(); cache.Add(key, typedValue); return(typedValue); }
public async Task Should_add_item_to_cache_when_context_exists() { using (sut.StartContext()) { sut.Add("Key", 1); await Task.Delay(5); var found = sut.TryGetValue("Key", out var value); Assert.True(found); Assert.Equal(1, value); await Task.Delay(5); sut.Remove("Key"); var foundAfterRemove = sut.TryGetValue("Key", out value); Assert.False(foundAfterRemove); Assert.Null(value); } }
public async Task <IAppEntity?> GetAppAsync(DomainId appId, bool canCache = false, CancellationToken ct = default) { var cacheKey = AppCacheKey(appId); var app = await GetOrCreate(cacheKey, () => { return(indexForApps.GetAppAsync(appId, canCache, ct)); }); if (app != null) { localCache.Add(AppCacheKey(app.Name), app); } return(app); }
public async Task <DomainId> ResolveOrCreateAsync(Context context, ICommandBus commandBus, string path) { Guard.NotNull(commandBus, nameof(commandBus)); Guard.NotNull(path, nameof(path)); path = path.Trim(TrimChars); var elements = path.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries); if (elements.Length == 0) { return(DomainId.Empty); } var currentId = DomainId.Empty; var i = elements.Length; for (; i > 0; i--) { var subPath = string.Join('/', elements.Take(i)); if (localCache.TryGetValue(GetCacheKey(subPath), out var cached) && cached is DomainId id) { currentId = id; break; } } var creating = false; for (; i < elements.Length; i++) { var name = elements[i]; var isResolved = false; if (!creating) { var children = await assetQuery.QueryAssetFoldersAsync(context, currentId); foreach (var child in children) { var childPath = string.Join('/', elements.Take(i).Union(Enumerable.Repeat(child.FolderName, 1))); localCache.Add(GetCacheKey(childPath), child.Id); } foreach (var child in children) { if (child.FolderName == name) { currentId = child.Id; isResolved = true; break; } } } if (!isResolved) { var command = new CreateAssetFolder { ParentId = currentId, FolderName = name }; await commandBus.PublishAsync(command); currentId = command.AssetFolderId; creating = true; } var newPath = string.Join('/', elements.Take(i).Union(Enumerable.Repeat(name, 1))); localCache.Add(GetCacheKey(newPath), currentId); } return(currentId); }