Example #1
0
        public void AddObject(string key, object obj, TimeSpan lifetime)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            lock (_locker)
            {
                if (this.Cache.ContainsKey(key))
                {
                    MemoryCacheItem existsCacheItem = this.Cache[key];
                    existsCacheItem.Reinit(DateTime.Now, lifetime, obj);
                }
                else
                {
                    MemoryCacheItem cacheItem = new MemoryCacheItem(DateTime.Now, lifetime, obj);
                    this.Cache.Add(key, cacheItem);
                }
            }
        }
Example #2
0
        public object GetObject(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key");
            }

            object obj = null;

            if (this.Cache.ContainsKey(key))
            {
                MemoryCacheItem cacheItem = this.Cache[key];
                obj = cacheItem.GetObject();
            }

            return(obj);
        }