在dotnet核心中使用内存中的集合-如何使所有类可用

问题描述

在dotnet核心项目中,如何使集合在任何给定时间在内存中可用于任何类? 我在想以下方法

public interface IInMemoryCache
{
    public void Setup();
}

public class InMemoryCache : IInMemoryCache
{
    private List<MyObject> cache;
    public void Setup() // entered once when app is started 
    {
        cache = new List<MyObject>() { 
                new MyObject("param1","param2"),new MyObject("param3","param4") 
    } 
    public IList<MyObject> GetCollection() 
    {
        return this.collection;
    }
}

,然后在“启动”中:

public void Configure(IApplicationBuilder app,IWebHostEnvironment env,IInMemoryCache cache)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    cache.Setup();

还有:

public void ConfigureServices(IServiceCollection services) { 
    services.AddSingleton<IInMemoryCache,InMemoryCache>();

这是一个方法吗,是否意味着我可以在任何类中注入IInMemoryCache并能够访问缓存对象? -在应用程序的整个生命周期中(意味着在它启动并运行时,如果我重新启动它,则该集合有望通过运行Setup方法再次初始化)

因此,现在在任何课程中,我只需添加IInMemoryCache cache 然后cache.GetCollection()检索集合(该集合在应用程序启动时就已设置)。

是否有更好的方法(如本机功能)来缓存所有类可用的集合?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)