当前位置: 首页>>代码示例>>C#>>正文


C# MemoryCache.Get方法代码示例

本文整理汇总了C#中System.Runtime.Caching.MemoryCache.Get方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryCache.Get方法的具体用法?C# MemoryCache.Get怎么用?C# MemoryCache.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Runtime.Caching.MemoryCache的用法示例。


在下文中一共展示了MemoryCache.Get方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestBasic

		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"));
		}
开发者ID:ntung,项目名称:ToolsPack.Net,代码行数:12,代码来源:MemoryCacheTests.cs

示例2: ExistingObject_IncrementByOneAndSetExpirationDate

            public void ExistingObject_IncrementByOneAndSetExpirationDate()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                    .Limit(1)
                    .Over(100);
                var cache = new MemoryCache("testing_cache");
                var repository = new MemoryThrottleRepository(cache);
                string id = repository.CreateThrottleKey(key, limiter);

                var cacheItem = new MemoryThrottleRepository.ThrottleCacheItem()
                {
                    Count = 1,
                    Expiration = new DateTime(2030, 1, 1)
                };

                cache
                    .Set(id, cacheItem, cacheItem.Expiration);

                // Act
                repository.AddOrIncrementWithExpiration(key, limiter);

                // Assert
                var item = (MemoryThrottleRepository.ThrottleCacheItem)cache.Get(id);
                Assert.Equal(2L, item.Count);
                Assert.Equal(new DateTime(2030, 1, 1), item.Expiration);
            }
开发者ID:gopangea,项目名称:BrakePedal,代码行数:28,代码来源:MemoryThrottleRepositoryTests.cs

示例3: NewObject_SetsCountToOneWithExpiration

            public void NewObject_SetsCountToOneWithExpiration()
            {
                // Arrange
                var key = new SimpleThrottleKey("test", "key");
                var limiter = new Limiter()
                    .Limit(1)
                    .Over(100);
                var cache = new MemoryCache("testing_cache");
                var repository = new MemoryThrottleRepository(cache);
                repository.CurrentDate = () => new DateTime(2030, 1, 1);

                string id = repository.CreateThrottleKey(key, limiter);

                // Act
                repository.AddOrIncrementWithExpiration(key, limiter);

                // Assert
                var item = (MemoryThrottleRepository.ThrottleCacheItem)cache.Get(id);
                Assert.Equal(1L, item.Count);
                // We're testing a future date by 100 seconds which is 40 seconds + 1 minute
                Assert.Equal(new DateTime(2030, 1, 1, 0, 1, 40), item.Expiration);
            }
开发者ID:gopangea,项目名称:BrakePedal,代码行数:22,代码来源:MemoryThrottleRepositoryTests.cs

示例4: Trim

		public void Trim ()
		{
			var config = new NameValueCollection ();
			config ["__MonoEmulateOneCPU"] = "true";
			var mc = new MemoryCache ("MyCache", config);

			for (int i = 0; i < 10; i++)
				mc.Set ("key" + i.ToString (), "value" + i.ToString (), null);

			// .NET doesn't touch the freshest 10 entries
			Assert.AreEqual (10, mc.GetCount (), "#A1-1");
			long trimmed = mc.Trim (50);
			Assert.AreEqual (0, trimmed, "#A1-2");
			Assert.AreEqual (10, mc.GetCount (), "#A1-3");

			mc = new MemoryCache ("MyCache", config);
			// Only entries 11- are considered for removal
			for (int i = 0; i < 11; i++)
				mc.Set ("key" + i.ToString (), "value" + i.ToString (), null);

			Assert.AreEqual (11, mc.GetCount (), "#A2-1");
			trimmed = mc.Trim (50);
			Assert.AreEqual (1, trimmed, "#A2-2");
			Assert.AreEqual (10, mc.GetCount (), "#A2-3");

			mc = new MemoryCache ("MyCache", config);
			// Only entries 11- are considered for removal
			for (int i = 0; i < 125; i++)
				mc.Set ("key" + i.ToString (), "value" + i.ToString (), null);

			Assert.AreEqual (125, mc.GetCount (), "#A3-1");
			trimmed = mc.Trim (50);
			Assert.AreEqual (62, trimmed, "#A3-2");
			Assert.AreEqual (63, mc.GetCount (), "#A3-3");

			// Testing the removal order
			mc = new MemoryCache ("MyCache", config);
			var removed = new List <string> ();
			var cip = new CacheItemPolicy ();
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				removed.Add (args.CacheItem.Key);
			};

			for (int i = 0; i < 50; i++)
				mc.Set ("key" + i.ToString (), "value" + i.ToString (), cip);

			object value;
			for (int i = 0; i < 50; i++)
				value = mc.Get ("key" + i.ToString ());

			trimmed = mc.Trim (50);
			Assert.AreEqual (25, mc.GetCount (), "#A4-1");
			Assert.AreEqual (25, trimmed, "#A4-2");
			Assert.AreEqual (25, removed.Count, "#A4-3");

			// OK, this is odd... The list is correct in terms of entries removed but the entries
			// are removed in the _MOST_ frequently used order, within the group selected for removal.
			for (int i = 24; i >= 0; i--) {
				int idx = 24 - i;
				Assert.AreEqual ("key" + i.ToString (), removed [idx], "#A5-" + idx.ToString ());
			}
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:62,代码来源:MemoryCacheTest.cs

示例5: TestCacheSliding

		public void TestCacheSliding ()
		{    
			var config = new NameValueCollection ();
			config["cacheMemoryLimitMegabytes"] = 0.ToString ();
			config["physicalMemoryLimitPercentage"] = 100.ToString ();
			config["__MonoEmulateOneCPU"] = true.ToString ();

			// it appears that pollingInterval does nothing, so we set the Mono timer as well
			config["pollingInterval"] = new TimeSpan (0, 0, 1).ToString ();
			config["__MonoTimerPeriod"] = 1.ToString ();

			using (var mc = new MemoryCache ("TestCacheSliding",  config)) {
				Assert.AreEqual (0, mc.GetCount (), "#CSL1");

				var cip = new CacheItemPolicy();
				cip.SlidingExpiration = new TimeSpan (0, 0, 1);
				mc.Add("slidingtest", "42", cip);

				mc.Add("expire1", "1", cip);
				mc.Add("expire2", "2", cip);
				mc.Add("expire3", "3", cip);
				mc.Add("expire4", "4", cip);
				mc.Add("expire5", "5", cip);

				Assert.AreEqual (6, mc.GetCount (), "#CSL2");

				for (int i = 0; i < 50; i++) {
					global::System.Threading.Thread.Sleep (100);

					var item = mc.Get ("slidingtest");
					Assert.AreNotEqual (null, item, "#CSL3-" + i);
				}

				Assert.AreEqual (1, mc.GetCount (), "#CSL4");

				global::System.Threading.Thread.Sleep (4 * 1000);

				Assert.AreEqual (0, mc.GetCount (), "#CSL5");
			}
		}
开发者ID:nlhepler,项目名称:mono,代码行数:40,代码来源:MemoryCacheTest.cs

示例6: TestNullKeyGet

 public void TestNullKeyGet()
 {
     ICache cache = new MemoryCache();
     cache.Put("nunit", "value");
     object item = cache.Get(null);
     Assert.IsNull(item);
 }
开发者ID:Leftyx,项目名称:nhcontrib,代码行数:7,代码来源:MemoryCacheFixture.cs

示例7: TestCacheSliding

		public void TestCacheSliding ()
		{    
			var config = new NameValueCollection ();
			config["cacheMemoryLimitMegabytes"] = 0.ToString ();
			config["physicalMemoryLimitPercentage"] = 100.ToString ();
			config["pollingInterval"] = new TimeSpan (0, 0, 1).ToString ();

			using (var mc = new MemoryCache ("TestCacheSliding",  config)) {
				Assert.AreEqual (0, mc.GetCount (), "#CSL1");

				var cip = new CacheItemPolicy();
				// The sliding expiration timeout has to be greater than 1 second because
				// .NET implementation ignores timeouts updates smaller than
				// CacheExpires.MIN_UPDATE_DELTA which is equal to 1.
				cip.SlidingExpiration = new TimeSpan (0, 0, 2);
				mc.Add("slidingtest", "42", cip);

				mc.Add("expire1", "1", cip);
				mc.Add("expire2", "2", cip);
				mc.Add("expire3", "3", cip);
				mc.Add("expire4", "4", cip);
				mc.Add("expire5", "5", cip);

				Assert.AreEqual (6, mc.GetCount (), "#CSL2");

				for (int i = 0; i < 50; i++) {
					global::System.Threading.Thread.Sleep (100);

					var item = mc.Get ("slidingtest");
					Assert.AreNotEqual (null, item, "#CSL3-" + i);
				}

				Assert.IsNull (mc.Get ("expire1"), "#CSL4-1");
				Assert.IsNull (mc.Get ("expire2"), "#CSL4-2");
				Assert.IsNull (mc.Get ("expire3"), "#CSL4-3");
				Assert.IsNull (mc.Get ("expire4"), "#CSL4-4");
				Assert.IsNull (mc.Get ("expire5"), "#CSL4-5");
				Assert.AreEqual (1, mc.GetCount (), "#CSL4");

				global::System.Threading.Thread.Sleep (4 * 1000);

				Assert.IsNull (mc.Get ("slidingtest"), "#CSL5a");
				Assert.AreEqual (0, mc.GetCount (), "#CSL5");
			}
		}
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:45,代码来源:MemoryCacheTest.cs

示例8: TestCacheExpiryOrdering

		public void TestCacheExpiryOrdering ()
		{
			var config = new NameValueCollection ();
			config["cacheMemoryLimitMegabytes"] = 0.ToString ();
			config["physicalMemoryLimitPercentage"] = 100.ToString ();
			config["pollingInterval"] = new TimeSpan (0, 0, 1).ToString ();

			using (var mc = new MemoryCache ("TestCacheExpiryOrdering",  config)) {
				Assert.AreEqual (0, mc.GetCount (), "#CEO1");

				// add long lived items into the cache first
				for (int i = 0; i < 100; i++) {
					var cip = new CacheItemPolicy ();
					cip.SlidingExpiration = new TimeSpan (0, 0, 10);
					mc.Add ("long-" + i, i, cip);
				}

				Assert.AreEqual (100, mc.GetCount (), "#CEO2");

				// add shorter lived items into the cache, these should expire first
				for (int i = 0; i < 100; i++) {
					var cip = new CacheItemPolicy ();
					cip.SlidingExpiration = new TimeSpan(0, 0, 1);
					mc.Add ("short-" + i, i, cip);
				}

				Assert.AreEqual (200, mc.GetCount (), "#CEO3");

				global::System.Threading.Thread.Sleep (4 * 1000);

				for (int i = 0; i < 100; i++) {
					Assert.IsNull (mc.Get ("short-" + i), "#CEO4-" + i);
				}
				Assert.AreEqual (100, mc.GetCount (), "#CEO4");
			}
		}
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:36,代码来源:MemoryCacheTest.cs

示例9: TestCacheShrink

		public void TestCacheShrink ()
		{
			const int HEAP_RESIZE_THRESHOLD = 8192 + 2;
			const int HEAP_RESIZE_SHORT_ENTRIES = 2048;
			const int HEAP_RESIZE_LONG_ENTRIES = HEAP_RESIZE_THRESHOLD - HEAP_RESIZE_SHORT_ENTRIES;			
			
			var config = new NameValueCollection ();
			config["cacheMemoryLimitMegabytes"] = 0.ToString ();
			config["physicalMemoryLimitPercentage"] = 100.ToString ();
			config["pollingInterval"] = new TimeSpan (0, 0, 1).ToString ();
			
			using (var mc = new MemoryCache ("TestCacheShrink",  config)) {	
				Assert.AreEqual (0, mc.GetCount (), "#CS1");
							
				// add some short duration entries
				for (int i = 0; i < HEAP_RESIZE_SHORT_ENTRIES; i++) {
					var expireAt = DateTimeOffset.Now.AddSeconds (3);
					mc.Add ("short-" + i, i.ToString (), expireAt);
				}
				
				Assert.AreEqual (HEAP_RESIZE_SHORT_ENTRIES, mc.GetCount (), "#CS2");
							
				// add some long duration entries				
				for (int i = 0; i < HEAP_RESIZE_LONG_ENTRIES; i++) {
					var expireAt = DateTimeOffset.Now.AddSeconds (12);
					mc.Add ("long-" + i, i.ToString (), expireAt);
				}															
				
				Assert.AreEqual (HEAP_RESIZE_LONG_ENTRIES + HEAP_RESIZE_SHORT_ENTRIES, mc.GetCount(), "#CS3");
				
				// wait for the cache thread to expire the short duration items, this will also shrink the size of the cache
				global::System.Threading.Thread.Sleep (5 * 1000);
				
				for (int i = 0; i < HEAP_RESIZE_SHORT_ENTRIES; i++) {
					Assert.IsNull (mc.Get ("short-" + i), "#CS4-" + i);
				}
				Assert.AreEqual (HEAP_RESIZE_LONG_ENTRIES, mc.GetCount (), "#CS4");	
				
				// add some new items into the cache, this will grow the cache again
				for (int i = 0; i < HEAP_RESIZE_LONG_ENTRIES; i++) {				
					mc.Add("final-" + i, i.ToString (), DateTimeOffset.Now.AddSeconds (4));
				}			
				
				Assert.AreEqual (HEAP_RESIZE_LONG_ENTRIES + HEAP_RESIZE_LONG_ENTRIES, mc.GetCount (), "#CS5");	
			}
		}
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:46,代码来源:MemoryCacheTest.cs

示例10: Trim

		public void Trim ()
		{
			var config = new NameValueCollection ();
			config ["__MonoEmulateOneCPU"] = "true";
			var mc = new MemoryCache ("MyCache", config);

			for (int i = 0; i < 10; i++)
				mc.Set ("key" + i.ToString (), "value" + i.ToString (), null);

			Assert.AreEqual (10, mc.GetCount (), "#A1-1");
			long trimmed = mc.Trim (50);
			Assert.AreEqual (5, trimmed, "#A1-2");
			Assert.AreEqual (5, mc.GetCount (), "#A1-3");

			mc = new MemoryCache ("MyCache", config);
			// Only entries 11- are considered for removal
			for (int i = 0; i < 11; i++)
				mc.Set ("key" + i.ToString (), "value" + i.ToString (), null);

			Assert.AreEqual (11, mc.GetCount (), "#A2-1");
			trimmed = mc.Trim (50);
			Assert.AreEqual (6, trimmed, "#A2-2");
			Assert.AreEqual (5, mc.GetCount (), "#A2-3");

			mc = new MemoryCache ("MyCache", config);
			// Only entries 11- are considered for removal
			for (int i = 0; i < 125; i++)
				mc.Set ("key" + i.ToString (), "value" + i.ToString (), null);

			Assert.AreEqual (125, mc.GetCount (), "#A3-1");
			trimmed = mc.Trim (50);
			Assert.AreEqual (63, trimmed, "#A3-2");
			Assert.AreEqual (62, mc.GetCount (), "#A3-3");

			// Testing the removal order
			mc = new MemoryCache ("MyCache", config);
			var removed = new List <string> ();
			var cip = new CacheItemPolicy ();
			cip.RemovedCallback = (CacheEntryRemovedArguments args) => {
				removed.Add (args.CacheItem.Key);
			};

			for (int i = 0; i < 50; i++)
				mc.Set ("key" + i.ToString (), "value" + i.ToString (), cip);

			object value;
			for (int i = 0; i < 50; i++)
				value = mc.Get ("key" + i.ToString ());

			trimmed = mc.Trim (50);
			Assert.AreEqual (25, mc.GetCount (), "#A4-1");
			Assert.AreEqual (25, trimmed, "#A4-2");
			Assert.AreEqual (25, removed.Count, "#A4-3");

			for (int i = 0; i < 25; i++)
				Assert.AreEqual ("key" + i.ToString (), removed [i], "#A5-" + i.ToString ());
		}
开发者ID:caomw,项目名称:mono,代码行数:57,代码来源:MemoryCacheTest.cs

示例11: MemorySpeedPerformance


//.........这里部分代码省略.........
             *                                          No Async
             * Serialized v Raw Get:      8.424x         8.2344x
             * Serialized v Raw Set:     16.329x        15.6698x
             * Compression v Raw Get: 1,046.349x       448.3221x
             * Compression v Raw Set:   175.661x        99.6618x
             */

            const int ITERATIONS = 30000;
            const string KEY = "impt-key";

            var value = new OneLong
                {
                    Id = GenerateId()
                };

            MemoryCache cM = new MemoryCache("cM");
            Stopwatch sw = Stopwatch.StartNew();

            // warmup
            byte[] serialize = ProtoBufSerializer.Serialize(value);
            byte[] compress = SmartCompressor.Instance.Compress(serialize);
            cM.Set(KEY, compress, null);

            for (int i = 0; i < ITERATIONS; i++)
            {
                serialize = ProtoBufSerializer.Serialize(value);
                compress = SmartCompressor.Instance.Compress(serialize);
                cM.Set(KEY, compress, null);
            }

            long compressSet = sw.ElapsedMilliseconds;

            // warmup
            byte[] compressed = (byte[])cM.Get(KEY);
            byte[] decompressed = SmartCompressor.Instance.Decompress(compressed);
            ProtoBufSerializer.Deserialize<OneLong>(decompressed);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < ITERATIONS; i++)
            {
                compressed = (byte[])cM.Get(KEY);
                decompressed = SmartCompressor.Instance.Decompress(compressed);
                ProtoBufSerializer.Deserialize<OneLong>(decompressed);
            }

            long compressGet = sw.ElapsedMilliseconds;

            MemoryCache sM = new MemoryCache("sM");

            // warmup
            serialize = ProtoBufSerializer.Serialize(value);
            sM.Set(KEY, serialize, null);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < ITERATIONS; i++)
            {
                serialize = ProtoBufSerializer.Serialize(value);
                sM.Set(KEY, serialize, null);
            }

            long serializeSet = sw.ElapsedMilliseconds;

            // warmup
            compressed = (byte[])sM.Get(KEY);
            ProtoBufSerializer.Deserialize<OneLong>(compressed);
开发者ID:JesseBuesking,项目名称:BB.Caching,代码行数:66,代码来源:SerializationAndCompressionTests.cs


注:本文中的System.Runtime.Caching.MemoryCache.Get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。