public async Task <object?> GetItem(IBlobCacheKey cacheKey, Type targetType) { var blobContainer = cacheKey.Container; var key = cacheKey.Key; // Attempt to read blob from the storage container try { return(await _blobStorageService.GetDeserializedJson(cacheKey.Container, cacheKey.Key, targetType)); } catch (JsonException e) { // If there's an error deserializing the blob, we should // assume it's not salvageable and delete it so that it's re-built. _logger.LogWarning(e, $"Error deserializing JSON for blobContainer {blobContainer} and cache " + $"key {key} - deleting cached JSON"); await _blobStorageService.DeleteBlob(blobContainer, key); } catch (FileNotFoundException) { // Do nothing as the blob just doesn't exist } catch (Exception e) { _logger.LogError(e, "Caught error fetching cache entry from: {BlobContainer}/{Key}", blobContainer, key); } return(default);
public static Mock <IBlobCacheService> SetupGetItemForCacheMiss <TItem>( this Mock <IBlobCacheService> cacheService, IBlobCacheKey expectedCacheKey) where TItem : class { cacheService.Setup(mock => mock.GetItem( It.Is <IBlobCacheKey>(ck => ck.Key == expectedCacheKey.Key && ck.Container == expectedCacheKey.Container), It.IsAny <Func <Task <TItem> > >())) .Returns(async( IBlobCacheKey _, Func <Task <TItem> > entityProvider) => await entityProvider()); return(cacheService); }
public async Task <Either <ActionResult, TItem> > GetItem <TItem>( IBlobCacheKey cacheKey, Func <Task <Either <ActionResult, TItem> > > itemSupplier) where TItem : class { // Attempt to read blob from the cache container var cachedEntity = await GetItem <TItem>(cacheKey); if (cachedEntity != null) { return(cachedEntity); } // Cache miss - invoke provider instead return(await itemSupplier().OnSuccessDo(async entity => { // Write result to cache as a json blob before returning await SetItem(cacheKey, entity); })); }
public async Task <TItem> GetItem <TItem>( IBlobCacheKey cacheKey, Func <Task <TItem> > itemSupplier) where TItem : class { // Attempt to read blob from the cache container var cachedEntity = await GetItem <TItem>(cacheKey); if (cachedEntity != null) { return(cachedEntity); } // Cache miss - invoke provider instead var entity = await itemSupplier(); // Write result to cache as a json blob before returning await SetItem(cacheKey, entity); return(entity); }
public async Task DeleteCacheFolder(IBlobCacheKey cacheFolderKey) { await _blobStorageService.DeleteBlobs(cacheFolderKey.Container, cacheFolderKey.Key); }
public async Task DeleteItem(IBlobCacheKey cacheKey) { await _blobStorageService.DeleteBlob(cacheKey.Container, cacheKey.Key); }
public async Task <TItem?> GetItem <TItem>(IBlobCacheKey cacheKey) where TItem : class { return((TItem?) await GetItem(cacheKey, typeof(TItem))); }