如何解析单例对象内部的范围服务

问题描述

我有MemoryCache个对象(应用程序,配置等),我将它们注册Singleton。还有一些作用域存储库,可以从db中选择数据来填充缓存。

例如,这是Singleton注册的课程,

public class ApplicationCache : MultipleLoadCache<Application>
{
    public ApplicationCache() 
    {
            
    }
}

MultipleLoadCache会覆盖CacheItemPolicy(也有SingleLoadCache

public class MultipleLoadCache<TEntity> : SmartCache<TEntity> where TEntity : class
{
    public MultipleLoadCache()
    {
    }

    protected override CacheItemPolicy SetPolicy()
    {
        return new CacheItemPolicy()
        {
           AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(15)
        };
    }
 }

基类是

public class SmartCache<TEntity> : Idisposable where TEntity : class
{
      public bool TryGetList(IRepository<TEntity> repository,out List<TEntity> valueList)
      {
          valueList = null;
          lock (cacheLock)
          {
              GenerateCacheIfNotExists(repository,out valueList);
              if (valueList == null || valueList.Count == 0)
              {
                valueList = (List<TEntity>)_memoryCache.Get(key);
              }
          }

          return valueList != null;
      }

我知道不能将范围服务注入到单例类中。所以我更喜欢使用方法注入。

private void GenerateCacheIfNotExists(IRepository<TEntity> repository,out List<TEntity> list)
{
          list = null;
          if (!_memoryCache.Any(x => x.Key == key)) // if key not exists,get db records from repo.
          {
              IEnumerable<TEntity> tempList = repository.GetList();
              list = tempList.ToList();
              _cacheItemPolicy = SetPolicy();
              SetCacheList(list);
          }
      }
}

在控制器上,我尝试获取缓存值,但是这对我来说似乎是错误的。如果我尝试获取缓存值,则不应将存储库作为参数传递。

private readonly ApplicationCache _appCache;
public LogController(ApplicationCache appCache)
{
   _appCache = appCache;
}

[HttpPost]
[Route("Register")]
public List<Application> Register([FromServices] IApplicationRepository repository)
{
   List<Application> cf;
   _appCache.TryGetList(repository,out cf);

   return cf;
}

此外,通过执行方法注入。我也无法使用RemovedCallBack的{​​{1}}事件。因为,当回调触发(重新加载缓存)时,我需要存储库才能再次从db获取记录。

这个设计看起来不错,通过使用CacheItemPolicy的回调事件来做到这一点的最佳设计是什么?

更新1-

MemoryCache

谢谢

解决方法

我有同样的问题。由于静态类是在开始时编译的,因此无法在以后注入所需的服务。我通过使用IServiceScopeFactory弄清楚了。

您基本上将IServiceScopeFactory serviceScopeFactory注入到构造函数中。

static SampleClass(IServiceScopeFactory serviceScopeFactory){  
   //serviceScopedFactory will act as Singleton,since it is a static class
   _serviceScopeFactory = serviceScopeFactory;
}

并在方法中像这样使用它:

using (var scope = _serviceScopeFactory.CreateScope())
{
  var service = scope.ServiceProvider.GetRequiredService<IService>();
  //Here you can use the service. This will be used as Scoped since it will be 
  //recreated everytime it is called
   
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...