protected override async Task SetItemsAsync <T>(IEnumerable <CacheItem <T> > items, string partitionDirectory) { partitionDirectory = partitionDirectory ?? _defaultPartitionDirectory; ArgCheck.NotNull(nameof(partitionDirectory), partitionDirectory); var utcNow = DateTimeOffset.UtcNow; foreach (var item in items) { var blob = GetBlobReference(item.Key, partitionDirectory); using (var stream = await blob.OpenWriteAsync(new AccessCondition(), _options, new OperationContext()).ConfigureAwait(false)) { stream.Position = 0; await _serializer.SerializeToStreamAsync(stream, item.Value).ConfigureAwait(false); } if (item.Expiration.HasValue) { // load metadata and properties await blob.FetchAttributesAsync(new AccessCondition(), _options, new OperationContext()).ConfigureAwait(false); // set expiration blob.Metadata[_cacheExpirationMetadataKey] = item.Expiration.Value.ToString(_dateTimeFormatString); await blob.SetMetadataAsync(new AccessCondition(), _options, new OperationContext()).ConfigureAwait(false); } } }
public static async Task SerializeToFileAsync(this IAsyncStreamSerializer serializer, string path, object graph, bool overwrite = false, CancellationToken cancellationToken = default) { using (var stream = File.Open(path, overwrite ? FileMode.Create : FileMode.CreateNew)) { await serializer.SerializeToStreamAsync(stream, graph, cancellationToken).ConfigureAwait(false); } }