本文整理汇总了C#中System.Runtime.Caching.MemoryCache.GetCount方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryCache.GetCount方法的具体用法?C# MemoryCache.GetCount怎么用?C# MemoryCache.GetCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.Caching.MemoryCache
的用法示例。
在下文中一共展示了MemoryCache.GetCount方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ClearAllCachesTest
public void ClearAllCachesTest()
{
const string prefix = "123123af";
var fk1 = FullKey(prefix, "111");
var fk2 = FullKey(prefix, "222");
var memoryCache = new MemoryCache("sdgkmnsdlkghn");
_objectCacheFactory.Setup(x => x.Create()).Returns(memoryCache).Verifiable();
Assert.AreEqual(0, memoryCache.GetCount());
memoryCache.Add(fk1, new MemoryCacherTests.One(), new CacheItemPolicy());
memoryCache.Add(fk2, new MemoryCacherTests.One(), new CacheItemPolicy());
Assert.AreEqual(2, memoryCache.Count());
new CacheCleaner(_objectCacheFactory.Object).Clean();
Assert.AreEqual(0, memoryCache.GetCount());
}
示例2: TestCount
public void TestCount()
{
MemoryCache memoryCount = new MemoryCache("count");
Desenvolvedor dev = new Desenvolvedor();
dev.Nome = "Ninja";
dev.Linguagem = "C#";
dev.AnosExperiencia = 15;
CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(2);
memoryCount.Add("devRemove", dev, policy);
Assert.AreEqual(1, memoryCount.GetCount());
}
示例3: 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 ());
}
}
示例4: 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");
}
}
示例5: TestCacheExpiryOrdering
public void TestCacheExpiryOrdering ()
{
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 ("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);
Assert.AreEqual (100, mc.GetCount (), "#CEO4");
}
}
示例6: TestExpiredGetValues
public void TestExpiredGetValues ()
{
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, 10).ToString ();
config["__MonoTimerPeriod"] = 10.ToString ();
using (var mc = new MemoryCache ("TestExpiredGetValues", config)) {
Assert.AreEqual (0, mc.GetCount (), "#EGV1");
var keys = new List<string> ();
// add some short duration entries
for (int i = 0; i < 10; i++) {
var key = "short-" + i;
var expireAt = DateTimeOffset.Now.AddSeconds (1);
mc.Add (key, i.ToString (), expireAt);
keys.Add (key);
}
Assert.AreEqual (10, mc.GetCount (), "#EGV2");
global::System.Threading.Thread.Sleep (1000);
// we have waited but the items won't be expired by the timer since it wont have fired yet
Assert.AreEqual (10, mc.GetCount (), "#EGV3");
// calling GetValues() will expire the items since we are now past their expiresAt
mc.GetValues (keys);
Assert.AreEqual (0, mc.GetCount (), "#EGV4");
}
}
示例7: 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["__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 ("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);
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");
}
}
示例8: 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");
}
}
示例9: 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 ());
}