问题描述
在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 (将#修改为@)