来自 EF Core 上下文的缓存句柄
NCache的 EF Core 扩展方法还允许直接通过缓存处理实体时的灵活性。这些API不涉及数据源,因此数据直接进入缓存并从缓存中删除,而无需修改数据源。对于不经常更改的数据,此类 API 非常方便。
备注
此功能也可用于 NCache Professional.
以利用 NCache EF Core API,在您的应用程序中包含以下命名空间:
NCache 提供以下 API 用于缓存目的:
EF Core 上下文:获取缓存实例
缓存句柄是通过 获取缓存, NCache 应用程序扩展上的 EF Core 扩展方法 DbContext
类。
public partial class NorthwindContext : DbContext
{
// Class configures cache with NCacheConfiguration.Configure() method
}
仅缓存 API 调用是通过 Cache
包装器返回时 获取缓存 方法被调用。 缓存包装器与正在使用的上下文相关联。 在调用必要的操作之前,会验证实体及其各自的请求的类型。 此验证是在手头的上下文的帮助下进行的,该上下文在 获取缓存 已拨打电话。 因此,要成功执行请求,发出请求时上下文必须处于活动状态(未释放)。 如果不这样做将会抛出 System.ObjectDisposedException
.
因此,在代码中,建议在上下文中执行仅缓存操作:
重要
如果缓存没有初始化,就会抛出异常,所有需要进一步执行的操作都会失败,直到缓存初始化。
using (var context = new NorthwindContext())
{
Cache cache = context.GetCache(); // get NCache instance
// Perform cache only operations
}
插页
插页 方法将实体直接添加到缓存中,而不依赖于数据库。 您可能正在数据库中存储新客户,并且已经将所有先前添加的客户提取到缓存中。 您无需稍后执行 LINQ 查询来将新客户从数据源获取到缓存中,只需调用 插页 将客户添加到数据库后(当 SaveChanges
被调用)与用于插入数据库的实体的相同实例。 这节省了一个实体的数据库访问成本。
可以使用以下内容缓存实体 缓存选项:
QueryIdentifier
Priority
AbsoluteExpirationTime
SlidingExpirationTime
重要
Insert
方法的行为如下:
案例 | 宠物行为研究 |
---|---|
缓存中不存在实体 | 将实体添加到缓存中 |
缓存中存在的实体 | 更新缓存中的实体 |
插页 返回一个缓存键,该键是为存储在缓存中的每个实体内部生成的。 可以保存此缓存密钥以供以后用于验证目的或删除缓存实体。
国际私人包机价格项目范例
- 下面的示例插入一个 客户实体 带有查询标识符的缓存 客户实体 和
Default
优先。
using (var database = new NorthwindContext())
{
var cust = new Customers
{
CustomerId = "HANIH",
ContactName = "Hanih Moos",
ContactTitle = "Sales Representative",
CompanyName = "Blauer See Delikatessen"
};
var options = new CachingOptions
{
QueryIdentifier = new Tag("CustomerEntity"),
Priority = Runtime.CacheItemPriority.Default
};
Cache cache = database.GetCache(); // get NCache instance
cache.Insert(cust, out string cacheKey, options);
string key = cacheKey; // can be saved for future use such as removing cache
}
- 此示例缓存插入到数据库中的相同实体:
using (var database = new NorthwindContext())
{
// Customer entity to be cached and stored to database
var customerEntity = new Customers
{
CustomerId = "HANIH",
ContactName = "Hanih Moos",
ContactTitle = "Sales Representative ",
CompanyName = "Blauer See Delikatessen"
};
// Add customer entity to database
database.Customers.Add(customerEntity);
database.SaveChanges();
// Caching options for cache
var options = new CachingOptions
{
QueryIdentifier = new Tag("CustomerEntity"),
Priority = Runtime.CacheItemPriority.Default,
};
// Add customer entity to cache
Cache cache = database.GetCache();
cache.Insert(cust, out string cacheKey, options);
string key = cacheKey; // can be saved for future use such as removing cache
}
删除
删除 方法从缓存中删除实体,而不从数据库中删除它们。 如果您对缓存的实体进行了一些临时更改或者实体可能已过时,这非常有用。 从缓存中删除实体允许从数据源加载新数据,无论是通过 FromCache
or LoadIntoCache
方法。
该 API 有两个重载,一个重载接受实体,另一个重载接受实体对应的缓存键。 该密钥在 Insert/FromCache/LoadIntoCache
当实体添加到缓存并可以保存时调用。
public void Remove(object entity);
public void Remove(string cacheKey);
国际私人包机价格项目范例
- 以下示例根据提供给它的实体删除缓存实体。 如果缓存中存在,则将其删除。
using (var database = new NorthwindContext())
{
var cust = new Customers
{
CustomerId = "HANIH",
ContactName = "Hanih Moos",
ContactTitle = "Sales Representative",
CompanyName = "Blauer See Delikatessen"
};
ICache cache = database.GetCache();
cache.Remove(cust);
}
- 以下示例将缓存键作为参数。 如果缓存中存在针对该键的实体,则会将其从缓存中删除。
using (var database = new NorthwindContext())
{
Cache cache = database.GetCache();
cache.Remove(cacheKey); // cacheKey saved during Insert()/FromCache()/LoadIntoCache() calls
}
删除查询标识符
删除查询标识符 方法根据指定的从缓存中删除所有实体 QueryIdentifier
in 缓存选项 (插入缓存时)。 如果具有该标识符的实体存在,则所有关联实体都会从缓存中删除,但不会从实际数据源中删除。
using (var database = new NorthwindContext())
{
var options = new CachingOptions
{
QueryIdentifier = new Tag("CustomerEntity"),
};
Cache cache = database.GetCache(); // get NCache instance
cache.RemoveByQueryIdentifier(options.QueryIdentifier);
}
参见
.NET: Alachisoft.NCache.EntityFrameworkCore 和 Alachisoft.NCache.运行时.缓存 命名空间。