Entity Framework (EF) Core 是 Entity Framework 对象关系映射引擎的重新架构和重写版本,用于 .NET Core 应用程序。 它非常轻量级、可扩展和跨平台。
不过成交量大 .NET Core 应用程序使用 EF Core 人脸表现 峰值负载下数据库层的可扩展性瓶颈。 这是因为尽管您可以通过添加更多应用程序服务器来线性扩展应用程序层,但您无法添加更多数据库服务器来对其进行扩展。
但是,如果您使用分布式缓存,例如 NCache 在您的 .NET Core 应用程序,您可以快速删除这些 性能和可扩展性瓶颈 并处理极端事务负载。
扩展方法是 C# 的一项功能,它允许您在不创建派生类的情况下扩展现有类和类型。 扩展方法是一种特殊的静态方法,但它被称为扩展类的实例方法。
NCache 已通过扩展方法将其缓存功能集成到 EF Core 中。 这使得使用 NCache 从一个 EF 核心应用程序 非常简单直接。 下面是 EF Core 扩展方法的列表,由 NCache.
EF Core 中最常见的缓存数据是事务数据。 这是在运行时创建的频繁更改的数据(例如客户、帐户、活动等),您只将其缓存一小段时间,在此期间您的应用程序会多次读取它。
FromCache() 扩展方法允许您通过缓存您的结果集来缓存事务数据 LINQ 查询. 请参阅下面的示例。
//Load suppliers into the cache with the expiration options
IEnumerable<Suppliers> suppliers = (from supplier in database.Suppliers
where supplier.SupplierId < 10
select supplier).FromCache(options);
在这里,在去数据库获取这个数据之前,FromCache() 方法检查这个数据的缓存。 如果找到,它将从缓存中返回。 否则,它从数据库中获取它,首先缓存它,以便下次它会在那里,然后将结果返回给调用者。
您可以选择将整个集合作为一个项目缓存在缓存中,也可以拆分其所有实体并单独缓存它们。 如果您还想单独获取或更新这些实体,则拆分很有用。 以下是此方法的异步版本。
//Load suppliers into the cache with the expiration options asynchronously
Task<IEnumerable><Suppliers> suppliersTask = (from supplier in database.Suppliers
where supplier.SupplierId < 10
select supplier).FromCacheAsync(options);
suppliersTask.Wait();
IEnumerable<Suppliers> suppliers = suppliersTask.Result.ToList();
参考数据是你应该完全缓存的东西。 这允许您运行 LINQ 查询 只在缓存上,从不去数据库。 请注意,如果缓存中没有整个数据集,则 LINQ 查询结果不正确,因为它们只检查缓存并假设整个数据集都在缓存中。
使用 LoadIntoCache() 扩展方法,您可以使用所有参考数据填充 EF Core 缓存。 请参阅下面的示例以填充缓存。
//Load ALL products into the cache as reference data
IEnumerable<Products> products = (from product in database.Products
select product)
.LoadIntoCache(out string cacheKey, options);
将所有参考数据加载到缓存后,就可以对其运行 LINQ 查询而不是数据库。 这可以减轻您的数据库压力并提高您的应用程序性能和可扩展性。 请参见下面的示例。
//Query for discontinued products from the reference data cache
IEnumerable<Products> products = (from product in database.Products
where product.Discontinued == true
select product).FromCacheOnly();
请注意,您的 EF Core 参考数据缓存必须包含整个数据集(例如本例中的产品)。 否则,您的 FromCacheOnly() 扩展方法将只返回缓存中的任何内容,因为它不检查数据库。 而且,这可能是不正确的。
每当您对 EF Core 中的实体进行更改时,您都可以通过获取“缓存”句柄并调用适当的更新方法来使用它们更新 EF Core 缓存。 下面是一个例子。
// Initialize the database context
NorthwindContext database = new NorthwindContext();
...
// Add some new entities to the database
database.Shippers.Add(shipperPandaExpress);
database.Shippers.Add(shipperTitanic);
database.SaveChanges();
Cache cache = database.GetCache();
cache.Insert(shipperPandaExpress, out cacheKey, options);
cache.Insert(shipperTitanic, out cacheKey, options);
//By obtain a Cache class handle, you can directly add, update, or remove entities from EF Core. And, then updated data shows up in your LINQ queries. This gives you a lot more control over updating entities when your modifying data yourself. Below is the interface of Cache class.
namespace Alachisoft.NCache.EntityFrameworkCore
{
public class Cache
{
public void Insert(object entity, out string cacheKey, CachingOptions options);
public void Remove(object entity);
public void Remove(string cacheKey);
public void RemoveByQueryIdentifier(Tag tag);
}
}
Entity Framework Core 没有附带它的缓存框架(比如 休眠 做)。 然而, NCache 为您开发了一个非常灵活、强大且非常简单的缓存框架。 您应该出于以下原因使用它:
请查看全部详情 NCache 功能 版本比较 页。 阅读更多关于 NCache 并下载完整的 30 天试用版 NCache 从下面的链接。