Example #1
0
		public void TestBasic()
		{
			var memoryCache = new MemoryCache("Foo");
			var policy = new CacheItemPolicy();
			memoryCache.AddOrGetExisting("Pop", 123, DateTimeOffset.MaxValue);
			memoryCache.AddOrGetExisting("Top", "Gun", DateTimeOffset.MaxValue);

			memoryCache.Add("Pop", 12, DateTime.MaxValue);

			Assert.AreEqual("Gun", memoryCache.Get("Top"));
			Assert.AreEqual(123, memoryCache.Get("Pop"));
		}
Example #2
0
        public void AddOrGetExisting()
        {
            var cache2 = new CacheTest();
            var cache3 = new CacheTest();
            var cache4 = new CacheTest();

            System.Runtime.Caching.MemoryCache cache = System.Runtime.Caching.MemoryCache.Default;

            int cache1_1 = cache.AddOrGetExisting("cache1", 1);
            int cache1_2 = cache.AddOrGetExisting("cache1", 2);

            int cache2_1 = cache.AddOrGetExisting("cache2", i => cache2.Increment());
            int cache2_2 = cache.AddOrGetExisting("cache2", i => cache2.Increment());

            int cache3_1 = cache.AddOrGetExisting("cache3", i => cache3.Increment(), new CacheItemPolicy());
            int cache3_2 = cache.AddOrGetExisting("cache3", i => cache3.Increment(), new CacheItemPolicy());

            int cache4_1 = cache.AddOrGetExisting("cache4", i => cache4.Increment(), new DateTimeOffset(new DateTime(2100, 01, 01)));
            int cache4_2 = cache.AddOrGetExisting("cache4", i => cache4.Increment(), new DateTimeOffset(new DateTime(2100, 01, 01)));

            Assert.AreEqual(1, cache1_1);
            Assert.AreEqual(1, cache1_2);

            Assert.AreEqual(1, cache2_1);
            Assert.AreEqual(1, cache2_2);

            Assert.AreEqual(1, cache3_1);
            Assert.AreEqual(1, cache3_2);

            Assert.AreEqual(1, cache4_1);
            Assert.AreEqual(1, cache4_2);
        }
Example #3
0
        /// <summary>
        ///     Gets or adds the item into the memory cache.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source.</param>
        /// <param name="key">The key.</param>
        /// <param name="addItem">The function delegate used to add the item into the cache.</param>
        /// <param name="policy">The policy.</param>
        /// <returns>Returns a <see cref="T" /> representing the item in the cache.</returns>
        public static T GetOrAdd <T>(this MemoryCache source, string key, Func <string, T> addItem, CacheItemPolicy policy)
        {
            ValidateKey(key);

            var lazyCacheItem = new Lazy <T>(() => addItem(key));

            EnsureRemovedCallback <T>(policy);

            var cacheItem = source.AddOrGetExisting(key, lazyCacheItem, policy);

            if (cacheItem != null)
            {
                return(Unwrap <T>(cacheItem));
            }

            try
            {
                return(lazyCacheItem.Value);
            }
            catch
            {
                source.Remove(key);
                throw;
            }
        }
Example #4
0
        public bool SetNX <T>(string key, T value, TimeSpan?slidingExpireTime = default(TimeSpan?))
        {
            if (value == null)
            {
                throw new Exception("Can not insert null values to the cache!");
            }

            return(_memoryCache.AddOrGetExisting(
                       key,
                       value,
                       new CacheItemPolicy
            {
                SlidingExpiration = slidingExpireTime ?? _defaultExpireTimeSpan
            }) != null);
        }