private void OnOptionsChange(CosmosCacheOptions options)
        {
            // Did we create our own internal client? If so, we need to dispose it.
            if (this.initializedClient && this.cosmosClient != null)
            {
                // In case this becomes an issue with concurrent access to the client, we can see if ReaderWriterLockSlim can be leveraged.
                this.cosmosClient.Dispose();
            }

            this.options = options;

            // Force re-initialization on the next Connect
            this.cosmosContainer = null;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CosmosCache"/> class.
        /// </summary>
        /// <param name="optionsAccessor">Options accessor.</param>
        public CosmosCache(IOptions <CosmosCacheOptions> optionsAccessor)
        {
            if (optionsAccessor == null)
            {
                throw new ArgumentNullException(nameof(optionsAccessor));
            }

            if (string.IsNullOrEmpty(optionsAccessor.Value.DatabaseName))
            {
                throw new ArgumentNullException(nameof(optionsAccessor.Value.DatabaseName));
            }

            if (string.IsNullOrEmpty(optionsAccessor.Value.ContainerName))
            {
                throw new ArgumentNullException(nameof(optionsAccessor.Value.ContainerName));
            }

            if (optionsAccessor.Value.ClientBuilder == null && optionsAccessor.Value.CosmosClient == null)
            {
                throw new ArgumentNullException("You need to specify either a CosmosConfiguration or an existing CosmosClient in the CosmosCacheOptions.");
            }

            this.options = optionsAccessor.Value;
        }
        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,
            });
        }