Exemple #1
0
 public static async Task <object> DeserializeFromFileAsync(this IAsyncStreamSerializer serializer, Type type, string path, CancellationToken cancellationToken = default)
 {
     using (var stream = File.OpenRead(path))
     {
         return(await serializer.DeserializeFromStreamAsync(type, stream, cancellationToken).ConfigureAwait(false));
     }
 }
Exemple #2
0
        protected override async Task <IEnumerable <CacheItem <T> > > GetItemsAsync <T>(IEnumerable <string> blobKeys, string partitionDirectory)
        {
            partitionDirectory = partitionDirectory ?? _defaultPartitionDirectory;
            ArgCheck.NotNull(nameof(partitionDirectory), partitionDirectory);

            var utcNow = DateTimeOffset.UtcNow;

            // cache hits
            var items = new List <CacheItem <T> >();

            foreach (var blobKey in blobKeys)
            {
                var blob = GetBlobReference(blobKey, partitionDirectory);

                var exists = await blob.ExistsAsync(_options, new OperationContext()).ConfigureAwait(false);

                if (!exists)
                {
                    continue;
                }

                // load metadata and properties
                await blob.FetchAttributesAsync(new AccessCondition(), _options, new OperationContext()).ConfigureAwait(false);

                // read expiration
                DateTimeOffset?expiration = null;
                if (blob.Metadata.TryGetValue(_cacheExpirationMetadataKey, out var expirationString) &&
                    DateTimeOffset.TryParseExact(expirationString, _dateTimeFormatString, null, DateTimeStyles.None, out var expirationDateTime))
                {
                    if (expirationDateTime <= utcNow)
                    {
                        // remove if expired
                        await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, new AccessCondition()
                        {
                            IfMatchETag = blob.Properties.ETag
                        }, _options, new OperationContext()).ConfigureAwait(false);

                        // this one is expired so move on to the next one
                        continue;
                    }

                    // record the expiration for info later
                    expiration = expirationDateTime;
                }

                // cache hit...

                using (var stream = await blob.OpenReadAsync(new AccessCondition(), _options, new OperationContext()).ConfigureAwait(false))
                {
                    stream.Position = 0;
                    var item = new CacheItem <T>()
                    {
                        Key         = blobKey,
                        Value       = await _serializer.DeserializeFromStreamAsync <T>(stream).ConfigureAwait(false),
                        LastUpdated = blob.Properties.LastModified.GetValueOrDefault(),
                        Expiration  = expiration,
                    };

                    items.Add(item);
                }
            }

            return(items);
        }
Exemple #3
0
 public static async Task <T> DeserializeFromStreamAsync <T>(this IAsyncStreamSerializer serializer, Stream stream, CancellationToken cancellationToken = default)
 {
     return((T)await serializer.DeserializeFromStreamAsync(typeof(T), stream, cancellationToken).ConfigureAwait(false));
 }