/// <inheritdoc/>
        public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            await this.ConnectAsync().ConfigureAwait(false);

            await this.cosmosContainer.UpsertItemAsync(
                partitionKey : new PartitionKey(key),
                item : CosmosCache.BuildCosmosCacheSession(
                    key,
                    value,
                    options,
                    this.options),
                requestOptions : null,
                cancellationToken : token).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            await this.ConnectAsync();

            var operation = await this.cosmosContainer.UpsertItemAsync(
                partitionKey : new PartitionKey(key),
                item : CosmosCache.BuildCosmosCacheSession(
                    key,
                    value,
                    options),
                requestOptions : null,
                cancellationToken : token).ConfigureAwait(false);

            SetWriteInformation(operation.RequestCharge, this.cosmosClient.Endpoint.ToString(), $"Upserted key: {key}");
        }
        private static CosmosCacheSession BuildCosmosCacheSession(string key, byte[] content, DistributedCacheEntryOptions options, CosmosCacheOptions cosmosCacheOptions)
        {
            DateTimeOffset creationTime = DateTimeOffset.UtcNow;

            DateTimeOffset?absoluteExpiration = CosmosCache.GetAbsoluteExpiration(creationTime, options);

            long?timeToLive = CosmosCache.GetExpirationInSeconds(creationTime, absoluteExpiration, options);

            bool hasSlidingExpiration = timeToLive.HasValue && options.SlidingExpiration.HasValue;

            long?absoluteSlidingExpiration = null;

            if (hasSlidingExpiration && absoluteExpiration.HasValue)
            {
                absoluteSlidingExpiration = absoluteExpiration.Value.ToUnixTimeSeconds();
            }

            return(new CosmosCacheSession()
            {
                SessionKey = key,
                Content = content,
                TimeToLive = timeToLive,
                IsSlidingExpiration = hasSlidingExpiration,
                AbsoluteSlidingExpiration = absoluteSlidingExpiration,
                PartitionKeyAttribute = cosmosCacheOptions.ContainerPartitionKeyAttribute,
            });
        }
        private static CosmosCacheSession BuildCosmosCacheSession(string key, byte[] content, DistributedCacheEntryOptions options, DateTimeOffset?creationTime = null)
        {
            if (!creationTime.HasValue)
            {
                creationTime = DateTimeOffset.UtcNow;
            }

            DateTimeOffset?absoluteExpiration = CosmosCache.GetAbsoluteExpiration(creationTime.Value, options);

            long?timeToLive = CosmosCache.GetExpirationInSeconds(creationTime.Value, absoluteExpiration, options);

            return(new CosmosCacheSession()
            {
                SessionKey = key,
                Content = content,
                TimeToLive = timeToLive,
            });
        }
        /// <inheritdoc/>
        public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            await this.ConnectAsync(token).ConfigureAwait(false);

            ItemResponse <CosmosCacheSession> setCacheSessionResponse = await this.cosmosContainer.UpsertItemAsync(
                partitionKey : new PartitionKey(key),
                item : CosmosCache.BuildCosmosCacheSession(
                    key,
                    value,
                    options,
                    this.options),
                requestOptions : new ItemRequestOptions()
            {
                EnableContentResponseOnWrite = false,
            },
                cancellationToken : token).ConfigureAwait(false);

            this.options.DiagnosticsHandler?.Invoke(setCacheSessionResponse.Diagnostics);
        }