Esempio n. 1
0
        /// <summary>
        ///     SetItem T By Key
        /// </summary>
        /// <typeparam name="T">Type Of Cached Value</typeparam>
        /// <param name="key">Key Lookup</param>
        /// <param name="original">Item T To Be Cached</param>
        /// <returns>Success True|False</returns>
        public async Task <bool> SetItem <T>(string key, LitterBoxItem <T> original)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                this.RaiseException(new ArgumentException($"{nameof(this.SetItem)}=>{nameof(key)} Cannot Be NullOrWhiteSpace"));
                return(false);
            }

            if (original == null)
            {
                this.RaiseException(new ArgumentException($"{nameof(this.SetItem)}=>{nameof(original)} Cannot Be Null"));
                return(false);
            }

            var success = false;

            // when using multi-caching; modifying the TTR and TTL on the object collides with other caches
            var litter = original.Clone();

            litter.TimeToRefresh = litter.TimeToRefresh ?? (int)this._configuration.DefaultTimeToRefresh.TotalSeconds;
            litter.TimeToLive    = litter.TimeToLive ?? (int)this._configuration.DefaultTimeToLive.TotalSeconds;

            try {
                var uri            = UriFactory.CreateDocumentCollectionUri(this._configuration.Database, this._configuration.Collection);
                var document       = new CacheItem <T>(key, litter, this._configuration.PartitionKey);
                var requestOptions = new RequestOptions {
                    ConsistencyLevel        = ConsistencyLevel.Eventual,
                    DisableRUPerMinuteUsage = true,
                    PartitionKey            = new PartitionKey(this._configuration.PartitionKey)
                };
                var result = await this.GetPooledConnection <DocumentDBConnection>().Cache.UpsertDocumentAsync(uri, document, requestOptions).ConfigureAwait(false);

                if (result != null)
                {
                    success = true;
                }
            }
            catch (Exception ex) {
                this.RaiseException(ex);
            }

            this._inProcess.TryRemove(key, out var itemSet);

            return(success);
        }
Esempio n. 2
0
        /// <summary>
        ///     SetItem T By Key
        /// </summary>
        /// <typeparam name="T">Type Of Cached Value</typeparam>
        /// <param name="key">Key Lookup</param>
        /// <param name="original">Item T To Be Cached</param>
        /// <returns>Success True|False</returns>
        public async Task <bool> SetItem <T>(string key, LitterBoxItem <T> original)
        {
            var success = false;

            if (string.IsNullOrWhiteSpace(key))
            {
                this.RaiseException(new ArgumentException($"{nameof(this.SetItem)}=>{nameof(key)} Cannot Be NullOrWhiteSpace"));
                return(success);
            }

            if (original == null)
            {
                this.RaiseException(new ArgumentException($"{nameof(this.SetItem)}=>{nameof(original)} Cannot Be Null"));
                return(success);
            }

            // when using multi-caching; modifying the TTR and TTL on the object collides with other caches
            var litter = original.Clone();

            litter.TimeToRefresh = litter.TimeToRefresh ?? (int)this._configuration.DefaultTimeToRefresh.TotalSeconds;
            litter.TimeToLive    = litter.TimeToLive ?? (int)this._configuration.DefaultTimeToLive.TotalSeconds;

            try {
                var json = Utilities.Serialize(litter);
                if (!string.IsNullOrWhiteSpace(json))
                {
                    var connection = this.GetPooledConnection <RedisConnection>();

                    var hashSetSuccess = await connection.Cache.HashSetAsync(key, "litter", Compression.Zip(json)).ConfigureAwait(false);

                    var keyExpireSuccess = await connection.Cache.KeyExpireAsync(key, TimeSpan.FromSeconds((double)litter.TimeToLive)).ConfigureAwait(false);

                    success = hashSetSuccess & keyExpireSuccess;
                }
            }
            catch (Exception ex) {
                this.RaiseException(ex);
            }

            this._inProcess.TryRemove(key, out var removed);

            return(success);
        }