Beispiel #1
0
        /// <summary>
        /// Get from cache
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValue(TKey key, out TValue value)
        {
            bool result;

            lock (cache)
            {
                TimedCacheValue <TValue> cacheValue = null;
                if (cache.TryGetValue(key, out cacheValue))
                {
                    // check if the cached value has expired
                    DateTime now = DateTime.UtcNow;
                    if (cacheValue.TimeOut < now)
                    {
                        // cached info expired
                        this.cache.Remove(key);
                        result = false;
                        value  = default(TValue);
                    }
                    else
                    {
                        // cached value valid
                        result = true;
                        value  = cacheValue.Value;
                    }
                }
                else
                {
                    // value not cached
                    result = false;
                    value  = default(TValue);
                }
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Add to chache
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(TKey key, TValue value)
        {
            DateTime validUntilDateTime = this.GetValidUntilDateTime();

            TimedCacheValue <TValue> cacheValue = new TimedCacheValue <TValue>(validUntilDateTime, value);

            lock (cache) {
                this.cache.Add(key, cacheValue);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Try adds the key-value pair to the cache.
        /// If the key/value does not exist, it is added, and returned
        /// If the key/value does exist, the already existing value is returned
        /// </summary>
        /// <param name="key">The key</param>
        /// <param name="value">The value to cache</param>
        public TValue TryAddValue(TKey key, TValue value)
        {
            TValue result;

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            else
            {
                lock (cache)
                {
                    TimedCacheValue <TValue> cacheValue = null;
                    if (cache.TryGetValue(key, out cacheValue))
                    {
                        // check if the cached value has expired
                        DateTime now = DateTime.UtcNow;
                        if (cacheValue.TimeOut < now)
                        {
                            // cached info expired
                            // so adding the new value instead
                            this.cache.Remove(key);
                            DateTime validUntilDateTime = this.GetValidUntilDateTime();
                            cacheValue = new TimedCacheValue <TValue>(validUntilDateTime, value);
                            this.cache.Add(key, cacheValue);
                            result = value;
                        }
                        else
                        {
                            // cached value valid
                            result = cacheValue.Value;
                        }
                    }
                    else
                    {
                        // value not cached
                        // adding the information
                        DateTime validUntilDateTime = this.GetValidUntilDateTime();
                        cacheValue = new TimedCacheValue <TValue>(validUntilDateTime, value);
                        this.cache.Add(key, cacheValue);
                        result = value;
                    }
                }
            }

            return(result);
        }