我们可以通过使用不同的存储选项创建两个不同大小的本地存储。
首先,在Startup.cs文件中注册服务:
services.AddMemoryCache(options =>
{
options.SizeLimit = 10000;
});
services.AddDistributedMemoryCache(options =>
{
options.SizeLimit = 50000;
});
通过使用AddMemoryCache和AddDistributedMemoryCache方法,我们可以分别创建两个本地缓存,并设置它们的大小选项。
在需要使用缓存的地方,可以通过构造函数注入对应的缓存服务:
public class MyService
{
private readonly IMemoryCache _memoryCache;
private readonly IDistributedCache _distributedCache;
public MyService(IMemoryCache memoryCache, IDistributedCache distributedCache)
{
_memoryCache = memoryCache;
_distributedCache = distributedCache;
}
// Use _memoryCache or _distributedCache according to your need
}
这样,我们就可以在一个应用程序中使用两个不同的本地缓存,它们具有不同的大小选项。