缓存中的滑动过期
NCache 允许您配置“滑动过期”以在应用程序正在使用缓存数据时保留缓存数据,并删除在特定时间段内未使用的任何数据。 这对于维护会话特别有用,因为在特定时间段内空闲的任何会话都被视为不活动,并且可以被缓存删除。
滑动过期可确保如果在指定时间间隔内访问数据,则缓存项的生命周期将延长该间隔值。 例如,添加一个过期时间为 10 分钟的会话。 会话在第 6 分钟被访问,因此会话在缓存中的寿命将再延长 10 分钟。
在缓存中使用滑动过期的先决条件
添加/更新带有滑动过期的项目
备注
此功能也可用于 NCache Professional.
您可以添加/更新缓存项以设置过期,使用 插页 方法。
CacheItem
是由提供的自定义类 NCache 它可用于将数据添加到缓存,还允许您设置与此类对象关联的附加元数据。 此元数据定义项目的属性,如过期、依赖项等。
下面的示例设置 Expiration
属性滑动到期 5 分钟 CacheItem
如果密钥不存在,则将其添加到缓存中 - 如果存在则更新该项目。 这个 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.Sliding, 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.Sliding, 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.Sliding, 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.SLIDING, ncache.TimeSpan.from_minutes(5))
cache_item.set_expiration(expiration)
# Add / Update item in cache with expiration
cache.insert(key, cache_item)
您可以使用 API 在 API 中指定滑动过期时间 DefaultSliding
和 DefaultSlidingLonger
中配置的值 NCache 管理中心或 配置文件。 这提供了根据需要从外部更改值的灵活性,而无需更改代码。
以下代码将配置的默认过期时间(本例中为 DefaultSliding)与某个项目关联起来(如果缓存中不存在该键,则将其添加到缓存中);如果该键存在,则使用过期时间更新该项目。 如果在配置的时间间隔内未访问该项目,则该项目将过期。 否则,该物品的寿命将延长相同的时间间隔。
重要
要启用默认过期,必须通过以下方式启用它: NCache 管理中心 或config.ncconf。 如果您传递默认过期的 API 并且未通过配置 NCache 管理中心或 配置文件,它不会工作。
// Pre-condition: 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 TimeSpan
var expiration = new Expiration(ExpirationType.DefaultSliding);
cacheItem.Expiration = expiration;
//Add/Update item into the cache with 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.DefaultSliding);
cacheItem.setExpiration(expiration);
//Add/Update item into the cache with 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 type
var expiration = new ncache.Expiration(ncache.ExpirationType.DefaultSliding);
cacheItem.setExpiration(expiration);
//Add/Update item into the cache with 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_SLIDING)
cache_item.set_expiration(expiration)
# Add/Update item in cache with expiration
version = cache.insert(key, cache_item)
更多资讯
NCache 提供了一个滑动过期的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.运行时.缓存 命名空间。
Java的: COM。alachisoft.ncache.runtime.caching 命名空间。
节点.js: 過期 类。
Python: ncache.runtime.caching 类。