缓存中的绝对数据过期
NCache 允许您使用缓存项配置绝对过期时间。 这将使项目在客户指定的确切日期和时间到期。 可以对缓存中有限时间内所需的项目指定绝对过期时间。 例如,一家公司宣布对某些产品进行 24 小时限时抢购。 然后,这些商品可以以 24 小时的绝对过期时间进行缓存,因此一旦销售结束,它们就会从缓存中过期。
而且,这个 DateTime
值转换为 UTC时间 以满足缓存服务器和客户端位于不同时区的情况。 因此,这些项目将在用户指定的确切时间过期。
先决条件
添加/更新具有绝对有效期的项目
备注
此功能也可用于 NCache Professional.
您添加/更新缓存项以设置过期,使用 插页 方法。
CacheItem
是由提供的自定义类 NCache 它可用于将数据添加到缓存,还允许您设置与此类对象关联的附加元数据。 此元数据定义项目的属性,如过期、依赖项等。
下面的示例设置 Expiration
如果键不存在,则将 CacheItem 的绝对过期时间设置为 5 分钟,将其添加到缓存中 - 如果存在,则更新该项目。 这 CacheItem
然后将在 5 分钟后从缓存中删除。
// Precondition: Cache is already connected
// Get product from database against given product ID
Product product = FetchProductFromDB(1001);
// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";
// Create a new CacheItem for this product
var cacheItem = new CacheItem(product);
// Set Expiration TimeSpan
var expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(5));
cacheItem.Expiration = expiration;
// Add/Update item with expiration
cache.Insert(key, cacheItem);
// Precondition: Cache is already connected
// Get product from database against given product ID
Product product = fetchProductFromDB(1001);
// Generate a unique cache key for this product
String key = "Product:" + product.getProductID();
// Create a new CacheItem for this product
CacheItem cacheItem = new CacheItem(product);
// Set Expiration type and TimeSpan
Expiration expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(5));
cacheItem.setExpiration(expiration);
// Add/Update item in cache with expiration
cache.insert(key, cacheItem);
// This is an async method
// Precondition: Cache is already connected
// Get product from database against given product ID
var product = this.fetchProductFromDB(1001);
// Generate a unique cache key for this product
var key = "Product:" + product.getProductID();
// Create a new CacheItem for this product
// You also need to specify the FQN (Fully Qualified Name) of the class
var cacheItem = new ncache.CacheItem(product,"FQN.Product");
// Set Expiration type and TimeSpan
var expiration = new ncache.Expiration(ncache.ExpirationType.Absolute, ncache.TimeSpan.FromMinutes(5));
cacheItem.setExpiration(expiration);
// Add/Update item in cache with expiration
await this.cache.insert(key, cacheItem);
# Precondition: Cache is already connected
# Get product from database
product = fetch_product_from_db()
# Generate a unique cache key for this product
key = "Product:" + product.get_product_id()
# Create a new CacheItem for this product
cache_item = ncache.CacheItem(product)
# Set Expiration type and TimeSpan
expiration = ncache.Expiration(ncache.ExpirationType.ABSOLUTE, ncache.TimeSpan.from_minutes(5))
cache_item.set_expiration(expiration)
# Add / Update item in cache with expiration
cache.insert(key, cache_item)
您可以使用 API 在 API 中指定绝对过期时间 DefaultAbsolute
和 DefaultAbsoluteLonger
中配置的值 NCache 管理中心或 配置文件。 这提供了根据需要从外部更改值的灵活性,而无需更改代码。
以下代码将配置的默认过期时间(本例中为 DefaultAbsolute)与某个项目关联起来(如果缓存中不存在该键,则将其添加到缓存中);如果该键存在,则使用过期时间更新该项目。 一旦超过配置的时间,该项目就会过期。
重要
要启用默认过期,必须通过以下方式启用它: NCache 管理中心 or 配置文件。 如果您传递 > [!IMPORTANT] 的 API 并且它不是通过 NCache 管理中心或 配置文件,它不会工作。
// Precondition: Cache is already connected
// Get product from database against given product ID
Product product = FetchProductFromDB(1001);
// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";
// Create a cacheItem instance for product
var cacheItem = new CacheItem(product);
// Set Expiration type
var expiration = new Expiration(ExpirationType.DefaultAbsolute);
cacheItem.Expiration = expiration;
// Add/Update item into the cache with default expiration
CacheItemVersion version = cache.Insert(key,cacheItem);
// Precondition: Cache is already connected
// Get product from database against given product ID
Product product = fetchProductFromDB(1001);
// Generate a unique cache key for this product
String key = "Product:" + product.getProductID();
// Create a cacheItem instance for product
CacheItem cacheItem = new CacheItem(product);
// Set Expiration type
Expiration expiration = new Expiration(ExpirationType.DefaultAbsolute);
cacheItem.setExpiration(expiration);
//Add/Update item into the cache with default expiration
CacheItemVersion version = cache.insert(key, cacheItem);
// This is an async method
// Precondition: Cache is already connected
// Get product from database against given product ID
var product = this.fetchProductFromDB(1001);
// Generate a unique cache key for this product
var key = "Product:" + product.getProductID();
// Create a cacheItem instance for product
// You also need to specify the FQN(Fully Qualified Name) of the class
var cacheItem = new ncache.CacheItem(product,"FQN.Product");
// Set Expiration TimeSpan
var expiration = new ncache.Expiration(ncache.ExpirationType.DefaultAbsolute);
cacheItem.setExpiration(expiration);
var version = await this.cache.insert(key, cacheItem);
# Precondition: Cache is already connected
# Get product from database
product = fetch_product_from_db()
# Generate a unique cache key for this product
key = "Product:" + product.get_product_id()
# Create a new CacheItem for this product
cache_item = ncache.CacheItem(product)
# Set Expiration type and TimeSpan
expiration = ncache.Expiration(ncache.ExpirationType.DEFAULT_ABSOLUTE)
cache_item.set_expiration(expiration)
# Add/Update item in cache with expiration
version = cache.insert(key, cache_item)
将绝对过期设置为现有项目
备注
此功能也可用于 NCache Professional.
NCache 还提供了将绝对过期设置为现有缓存项的灵活性,而无需将其重新插入缓存。 这是通过 CacheItemAttribute
类,其中有 AbsoluteExpiration
要针对缓存项设置的属性。 这设置了项目应该从缓存中失效的确切日期和时间。 然后根据项目的现有键设置属性,使用 UpdateAttributes
的方法 ICache
.
以下示例为缓存中已存在且未设置过期的项目设置绝对过期时间。 这不需要将项目重新添加或重新插入到缓存中。
// Precondition: Cache is already connected
// Precondition: Item already exists in cache
string key = "Product:1001";
// Create a CacheItemAttribute for absolute expiration
// and set its value to 5 seconds
var attr = new CacheItemAttributes();
attr.AbsoluteExpiration = DateTime.Now.AddSeconds(5);
// Set Absolute Expiration of 5 seconds against existing key
cache.UpdateAttributes(key, attr);
// Precondition: Cache is already connected
// Precondition: Item already exists in cache
String key = "Product:1001";
// Create a CacheItemAttribute for absolute expiration and set its value to 5 seconds
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 5);
CacheItemAttributes attr = new CacheItemAttributes();
attr.setAbsoluteExpiration(calendar.getTime());
// Set Absolute Expiration of 5 seconds against existing key
cache.updateAttributes(key, attr);
// This is an async method
// Precondition: Cache is already connected
// Precondition: Item already exists in cache
var key = "Product:1001";
// Create a CacheItemAttribute for absolute expiration
// and set its value to 5 seconds
var time = new Date().getSeconds()+5;
var attr = new ncache.CacheItemAttributes();
attr.setAbsoluteExpiration(time);
// Set Absolute Expiration of 5 seconds against existing key
await this.cache.updateAttributes(key, attr);
# Precondition: Cache is already connected
# Precondition: Item already exists in cache
key = "Product:1001"
# Create a CacheItemAttribute for absolute expiration and set its value to 5 seconds
time = datetime.now() + timedelta(seconds=5)
attr = ncache.CacheItemAttributes()
attr.set_absolute_expiration(time)
# Set Absolute Expiration of 5 seconds against existing key
cache.update_attributes(key, attr)
更多资讯
NCache 提供了 Absolute Expiration on 的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.运行时.缓存 命名空间。
Java的: COM。alachisoft.ncache.runtime.caching 命名空间。
节点.js: 過期 类。
Python: ncache.runtime.caching 类。