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