Size的限制:SizeLimit设为100,每个Item的Size设定为1,则打出来的Count永远不会超过100
using Microsoft.Extensions.Caching.Memory;
using System;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()
{
SizeLimit = 100
});
for (int i = 0; i < 1000; i++)
{
memoryCache.Set<string>(i.ToString(), i.ToString(), new MemoryCacheEntryOptions()
{
Size = 1
});
Console.WriteLine(memoryCache.Count);
}
Console.ReadLine();
}
}
}
容量限制:如果把itemSize设为i.ToString().Length,此时打出来的为33,感觉SizeLimit就是容量的意思,总长度为100,单个如果为3个,则为33
using Microsoft.Extensions.Caching.Memory;
using System;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions()
{
SizeLimit = 100
});
for (int i = 0; i < 1000; i++)
{
memoryCache.Set<string>(i.ToString(), i.ToString(), new MemoryCacheEntryOptions()
{
Size = i.ToString().Length
}) ;
Console.WriteLine(memoryCache.Count);
}
Console.ReadLine();
}
}
}