/// <summary> /// Put the item in the cache. /// </summary> private async Task CacheSetAsync(TId id, TModel item, string key = null, CancellationToken token = default(CancellationToken)) { InternalContract.RequireNotDefaultValue(item, nameof(item)); key = key ?? GetCacheKeyFromId(id); var serializedCacheEnvelope = ToSerializedCacheEnvelope(item); await Cache.SetAsync(key, serializedCacheEnvelope, CacheOptions, token); }
/// <summary> /// Put the item in the cache. /// </summary> private async Task CacheSetAsync(TModel[] itemsArray, int limit, string key, CancellationToken token) { InternalContract.RequireNotDefaultValue(itemsArray, nameof(itemsArray)); var serializedCacheEnvelope = ToSerializedCacheEnvelope(itemsArray); await Cache.SetAsync(key, serializedCacheEnvelope, CacheOptions, token); _limitOfItemsInReadAllCache = limit; }
/// <summary> /// Put the item in the cache. /// </summary> private async Task CacheSetAsync(PageEnvelope <TModel> pageEnvelope, string keyPrefix, CancellationToken token) { InternalContract.RequireNotDefaultValue(pageEnvelope, nameof(pageEnvelope)); InternalContract.RequireValidated(pageEnvelope, nameof(pageEnvelope)); var serializedCacheEnvelope = ToSerializedCacheEnvelope(pageEnvelope); var key = GetCacheKeyForPage(keyPrefix, pageEnvelope.PageInfo.Offset, pageEnvelope.PageInfo.Limit); await Cache.SetAsync(key, serializedCacheEnvelope, CacheOptions, token); }
/// <summary> /// Check if an item exists in the cache. /// </summary> /// <param name="id">The id for the item</param> /// <param name="token">Propagates notification that operations should be canceled</param> protected async Task <bool> CacheItemExistsAsync(TId id, CancellationToken token) { InternalContract.RequireNotDefaultValue(id, nameof(id)); var key = GetCacheKeyFromId(id); var cachedItem = await Cache.GetAsync(key, token); return(cachedItem != null); }
/// <inheritdoc /> public Task DeleteAsync(string masterId, string slaveId, CancellationToken token = new CancellationToken()) { InternalContract.RequireNotDefaultValue(masterId, nameof(masterId)); InternalContract.RequireNotDefaultValue(slaveId, nameof(slaveId)); var serverMasterId = MapperHelper.MapToType <Guid, string>(masterId); var serverSlaveId = MapperHelper.MapToType <Guid, string>(slaveId); return(_storage.PersonConsent.DeleteAsync(serverMasterId, serverSlaveId, token)); }
private TStorableItem GetMemoryItem(TId id) { ValidateExists(id); InternalContract.RequireNotDefaultValue(id, nameof(id)); var item = _memoryItems[id]; FulcrumAssert.IsNotNull(item, $"{Namespace}: B431A6BB-4A76-4672-9607-65E1C6EBFBC9"); return(CopyItem(item)); }
/// <inheritdoc /> public Task <TStorableItem> ReadAsync(TId id) { InternalContract.RequireNotDefaultValue(id, nameof(id)); lock (_memoryItems) { var itemCopy = GetMemoryItem(id); return(Task.FromResult(itemCopy)); } }
/// <inheritdoc /> public async Task <PersonConsent> ReadAsync(string masterId, string slaveId, CancellationToken token = new CancellationToken()) { InternalContract.RequireNotDefaultValue(masterId, nameof(masterId)); InternalContract.RequireNotDefaultValue(slaveId, nameof(slaveId)); var serverMasterId = MapperHelper.MapToType <Guid, string>(masterId); var serverSlaveId = MapperHelper.MapToType <Guid, string>(slaveId); var serverItem = await _storage.PersonConsent.ReadAsync(serverMasterId, serverSlaveId, token); return(await MapFromServerAsync(serverItem, token)); }
/// <inheritdoc /> /// <remarks> /// Idempotent, i.e. will not throw an exception if the item does not exist. /// </remarks> public Task DeleteAsync(TId id) { InternalContract.RequireNotDefaultValue(id, nameof(id)); lock (_memoryItems) { if (!_memoryItems.ContainsKey(id)) { return(Task.FromResult(0)); } _memoryItems.Remove(id); } return(Task.FromResult(0)); }
/// <inheritdoc /> public Task <TStorableItem> CreateAsync(TId id, TStorableItem item) { InternalContract.RequireNotDefaultValue(id, nameof(id)); InternalContract.RequireNotNull(item, nameof(item)); InternalContract.RequireValidated(item, nameof(item)); var itemCopy = CopyItem(item); itemCopy.ETag = Guid.NewGuid().ToString(); itemCopy.Id = id; lock (_memoryItems) { ValidateNotExists(itemCopy.Id); _memoryItems.Add(itemCopy.Id, itemCopy); return(Task.FromResult(GetMemoryItem(itemCopy.Id))); } }
/// <summary> /// Get an item from the memory. /// </summary> /// <param name="id"></param> /// <param name="okIfNotExists">If false, we will throw an exception if the id is not found.</param> /// <returns></returns> /// <exception cref="FulcrumNotFoundException"></exception> protected TModel GetMemoryItem(TId id, bool okIfNotExists) { InternalContract.RequireNotDefaultValue(id, nameof(id)); if (!Exists(id)) { if (!okIfNotExists) { throw new FulcrumNotFoundException( $"Could not find an item of type {typeof(TModel).Name} with id \"{id}\"."); } return(default(TModel)); } var item = MemoryItems[id]; FulcrumAssert.IsNotNull(item); return(CopyItem(item)); }
/// <inheritdoc /> public Task <TStorableItem> UpdateAsync(TStorableItem item) { InternalContract.RequireNotNull(item, nameof(item)); InternalContract.RequireValidated(item, nameof(item)); InternalContract.RequireNotDefaultValue(item.Id, nameof(item.Id)); var itemCopy = CopyItem(item); lock (_memoryItems) { ValidateExists(itemCopy.Id); var current = GetMemoryItem(itemCopy.Id); ValidateEtag(current, itemCopy); itemCopy.ETag = Guid.NewGuid().ToString(); SetMemoryItem(itemCopy); return(Task.FromResult(GetMemoryItem(itemCopy.Id))); } }
public void FalseParameter() { const string parameterName = "parameterName"; try { const int value = 0; // ReSharper disable once ExpressionIsAlwaysNull InternalContract.RequireNotDefaultValue(value, parameterName); Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail("An exception should have been thrown"); } catch (FulcrumContractException fulcrumException) { Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(fulcrumException.TechnicalMessage.Contains(parameterName)); } catch (Exception e) { Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail($"Expected a specific FulcrumException but got {e.GetType().FullName}."); } }
private async Task <UseCacheStrategyEnum> GetCacheItemStrategyAsync(TId id, CacheEnvelope cacheEnvelope, CancellationToken token) { InternalContract.RequireNotDefaultValue(id, nameof(id)); InternalContract.RequireNotNull(cacheEnvelope, nameof(cacheEnvelope)); var cacheItemStrategy = GetCacheItemStrategy(cacheEnvelope); if (cacheItemStrategy != UseCacheStrategyEnum.Use) { return(cacheItemStrategy); } if (UseCacheStrategyMethodAsync == null) { return(UseCacheStrategyEnum.Use); } var cachedItemInformation = new CachedItemInformation <TId> { Id = id, UpdatedAt = cacheEnvelope.UpdatedAt }; return(await UseCacheStrategyMethodAsync(cachedItemInformation, token)); }
/// <summary> /// Remove from cache /// </summary> protected async Task CacheRemoveByIdAsync(TId id, CancellationToken token) { InternalContract.RequireNotDefaultValue(id, nameof(id)); var key = GetCacheKeyFromId(id); await Cache.RemoveAsync(key, token); }
/// <summary> /// Put the item in the cache. /// </summary> protected Task CacheSetAsync(TModel item, CancellationToken token) { InternalContract.RequireNotDefaultValue(item, nameof(item)); return(CacheSetAsync(GetIdDelegate(item), item, null, token)); }
/// <summary> /// Get a value from the cache if all constraints are fulfilled. /// </summary> protected async Task <TModel> CacheGetByIdAsync(TId id, CancellationToken token = default(CancellationToken)) { InternalContract.RequireNotDefaultValue(id, nameof(id)); return(await CacheGetAsync(id, null, token)); }
/// <summary> /// Put the item in the cache. /// </summary> protected async Task CacheSetByIdAsync(TId id, TModel item, CancellationToken token = default(CancellationToken)) { InternalContract.RequireNotDefaultValue(id, nameof(id)); await CacheSetAsync(id, item, null, token); }