캐시의 슬라이딩 만료
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에서 슬라이딩 만료를 지정할 수 있습니다. DefaultSliding
및 DefaultSlidingLonger
에 구성된 값 NCache 관리센터 또는 config.ncconf. 이는 필요한 경우 코드를 변경하지 않고도 외부에서 값을 변경할 수 있는 유연성을 제공합니다.
다음 코드는 구성된 기본 만료(이 경우 DefaultSliding)를 항목과 연결하여 키가 캐시에 없으면 해당 항목을 캐시에 추가하고 키가 있으면 만료로 항목을 업데이트합니다. 구성된 시간 간격 내에 항목에 액세스하지 않으면 항목이 만료됩니다. 그렇지 않으면 항목의 수명이 동일한 시간 간격으로 연장됩니다.
중대한
기본 만료를 활성화하려면 다음을 통해 활성화해야 합니다. NCache 관리 센터 또는 config.ncconf. 기본 만료를 위해 API를 전달했지만 이를 통해 구성되지 않은 경우 NCache 관리센터 또는 config.ncconf, 이거 작동 안 할거야.
// 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의.
도 참조
.그물: Alachisoft.NCache.런타임.캐싱 네임 스페이스.
자바 : COM.alachisoft.ncache.런타임.캐싱 네임 스페이스.
Node.js : 만료 클래스입니다.
파이썬 : ncache.런타임.캐싱 클래스입니다.