예제 #1
0
        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);
예제 #2
0
        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);
        }
예제 #3
0
        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);
            }));
        }
예제 #4
0
        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);
        }
예제 #5
0
 public async Task DeleteCacheFolder(IBlobCacheKey cacheFolderKey)
 {
     await _blobStorageService.DeleteBlobs(cacheFolderKey.Container, cacheFolderKey.Key);
 }
예제 #6
0
 public async Task DeleteItem(IBlobCacheKey cacheKey)
 {
     await _blobStorageService.DeleteBlob(cacheKey.Container, cacheKey.Key);
 }
예제 #7
0
 public async Task <TItem?> GetItem <TItem>(IBlobCacheKey cacheKey)
     where TItem : class
 {
     return((TItem?) await GetItem(cacheKey, typeof(TItem)));
 }