Пример #1
0
        public T Get(K key)
        {
            T result = default(T);

            if (default(K) == null && key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (_cacheLock)
            {
                if (_cacheItems.ContainsKey(key))
                {
                    BasicCacheItem <T> cacheItem = _cacheItems[key];
                    if (!cacheItem.IsExpired())
                    {
                        result = cacheItem.Value;
                    }
                    else
                    {
                        _cacheItems.Remove(key);
                    }
                }
            }

            return(result);
        }
Пример #2
0
        public void Add(K key, T value, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            if (default(K) == null && key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (_cacheLock)
            {
                if (Count < MaxItems)
                {
                    BasicCacheItem <T> .ValidateExpiration(absoluteExpiration, slidingExpiration);

                    BasicCacheItem <T> cacheItem = new BasicCacheItem <T>(value);
                    cacheItem.DateAddedToCache   = DateTime.Now;
                    cacheItem.AbsoluteExpiration = absoluteExpiration;
                    cacheItem.SlidingExpiration  = slidingExpiration;

                    _cacheItems.Add(key, cacheItem);
                }
                else
                {
                    GlymaSearchLogger.WriteTrace(LogCategoryId.Connector, TraceSeverity.Medium, "Cache item limit of " + MaxItems + " has been reached.  Item with key " + key.ToString() + " will not be added to the cache.");
                }
            }
        }
Пример #3
0
        public void Insert(K key, T value, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            if (default(K) == null && key == null)
            {
                throw new ArgumentNullException("key");
            }

            lock (_cacheLock)
            {
                if (_cacheItems.ContainsKey(key))
                {
                    BasicCacheItem <T> cacheItem = _cacheItems[key];
                    cacheItem.Value            = value;
                    cacheItem.DateAddedToCache = DateTime.Now;
                }
                else
                {
                    Add(key, value, absoluteExpiration, slidingExpiration);
                }
            }
        }