Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET Core-使用EF Core更新IMemoryCache中缓存列表的1个元素_C#_Entity Framework_Asp.net Core - Fatal编程技术网

C# ASP.NET Core-使用EF Core更新IMemoryCache中缓存列表的1个元素

C# ASP.NET Core-使用EF Core更新IMemoryCache中缓存列表的1个元素,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,我正在使用ASP.Net Core Razor页面和EF Core构建一个webshop。为了加速大量计算,我缓存了所有产品~100.000。信息来自我们的数据库 模型 在我的DBContext中,我定义每一个poductionInformation都有一个确切的产品 缓存方法 无论我在哪里显示ProductInformation,我都会调用UpdateProduct。然后我只重新检查我展示的产品的“旧”价格。这比重新计算整个缓存要有效得多。但是现在我需要在缓存的ProductInformati

我正在使用ASP.Net Core Razor页面和EF Core构建一个webshop。为了加速大量计算,我缓存了所有产品~100.000。信息来自我们的数据库

模型 在我的DBContext中,我定义每一个poductionInformation都有一个确切的产品

缓存方法 无论我在哪里显示ProductInformation,我都会调用UpdateProduct。然后我只重新检查我展示的产品的“旧”价格。这比重新计算整个缓存要有效得多。但是现在我需要在缓存的ProductInformation中建立一个DB连接。我不能让它工作

方法3

因为方法2中的问题是我没有DB连接,所以我可以在模型之外使用UpdateProduct方法,并将其放在我有DB连接的存储库中。无论在哪里显示产品信息,我都需要调用类似于_productrepository.UpdateProductref ProductInformation;的东西;。此方法如下所示:

public void UpdateProduct(ref ProductInformation pi){
    pi.Product = _dbContext.Product.Where(p => p.productnumber == pi.productnumber);
   // Of course I also need to do the calculations from the caching-method in GetProductInformationList()
}
但这感觉不对。实体框架为我组织了ProductInformation和Product之间的连接,那么是否可以这样重新定义Product?我认为这不是一个好办法

问题 我认为很多人在同样的情况下使用IMemoryCaching,您只想更新缓存列表中的一个元素,或者只更新price/stock/…中的一些元素。。。缓存列表元素的。我们如何处理这个问题?

1将LastChanged datetime[offset]列添加到您的产品数据库中,并在更新您的价格时要求其他系统也更新它。使用此功能,您可以轻松地将MaxLastChanged保存在缓存中,并仅查询已更改的产品,从而减少更新的时间和大小,并且可以更频繁地执行此操作

2将数据放入缓存时添加。我想你不会把它们更新回DB,所以没有跟踪会让一切都加快一点。此外,这将使您不必担心ProductInformation与Product EF之间的关系,因为EF无论如何都不会跟踪它们,而pi.Product将是一个通常的对象属性,没有任何隐藏的魔力


3在每个页面上检查价格更新是不好的。更新应在其他线程/后台运行。设置一些后台任务,检查更新的价格并将更新的对象重新加载到缓存中-使用1,您可以每5分钟或更短时间运行更新。检查此处以获取DbContext。

如果您知道对象何时更改,您只需在之后调用一个方法即可。MyCacheManager.UpdateProductDbContext myContext,ProductInformation pi{…更新缓存…}True。但我不知道什么时候产品发生了变化。然后是方法3的问题:是否可以这样更新缓存项?它会影响我的“实体框架关系”吗?如果您不知道某个项何时发生了更改,那么缓存可能是错误的解决方案。或者您必须编写一些查询,在db记录上设置一些[LastUpdateOn]标志。您应该为每个产品设置不同的密钥。因此,如果您有1m个产品,您应该有1m个缓存密钥,然后您还可以轻松地删除/更新cacheScheduled task works,并正确地获得需要更新的产品。但是,看起来我无法使用某种类型的更新缓存列表:GetProductInformationList.FirstOrDefaultpi=>pi.ProductNumber==newProductInformation.ProductNumber.Product=newProductInformation.Product;。正确的方法是什么?我需要为每个“ProductInformation”创建一个缓存条目吗?对不起,我的测试过程不对:上面的方法有效!谢谢!
private readonly IMemoryCache _cache; // is set in the constructor
private readonly DBContext _dbContext; // is set in the constructor
public List<ProductInformation> GetProductInformationList(){
    List<ProductInformation> = _cache.GetOrCreate("ProductInformations", entry =>
    {
        entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24);
        List<ProductInformation> list =_dbContext.ProductInformation.ToList();
        // do a lot of calculations on the list-elements
        return list;
    });
}
class ProductInformation{
    // some information about how to display a product
    public string productnumber {get; set;}
    public Product product {get; set;}

    pubic DateTime ProductTimeStamp {get; set;}
    public UpdateProduct(){
        // If ProductTimeStamp is more than 15 minutes in the past
        // get Product from the DB
        // and update the timestamp
    }
}
public void UpdateProduct(ref ProductInformation pi){
    pi.Product = _dbContext.Product.Where(p => p.productnumber == pi.productnumber);
   // Of course I also need to do the calculations from the caching-method in GetProductInformationList()
}