명명된 태그를 캐시 항목과 연결하려면 명명된 태그 목록을 제공해야 하며, 각각에는 다음과 같은 두 개의 매개변수가 있습니다. 키 태그의 이름인 문자열로 가치 키에 할당된 값인 기본 유형으로. NCache 그런 다음 개체를 이러한 명명된 태그와 연결할 수 있습니다. 명명된 태그를 사용하여 데이터 캐시에 항목을 추가한 다음 나중에 이전에 추가된 명명된 태그를 사용하여 캐시에서 이러한 항목을 검색할 수 있습니다.
항목이 특정 명명된 태그와 함께 캐시에 이미 추가된 경우, NCache 데이터 캐시에 이미 존재하는 명명된 태그를 업데이트할 수 있는 옵션을 사용자에게 제공합니다.
사전 조건
NCache 맞춤 클래스를 제공하고, CacheItem
데이터 캐시에 데이터를 추가하는 데 사용할 수 있습니다. 이 클래스는 데이터를 값 속성으로 캡슐화합니다. CacheItem
또한 개체와 관련된 추가 사양을 property
NamedTags 클래스의
다음 예에서는 명명된 태그를 다음과 같이 할당하여 설정합니다. property
of CacheItem
.
// Precondition: Cache is already connected
// In an online-store, VIP members are offered 10% discount
// A customer with VIP membership has signed in to the website
string customerKey = $"Customer:ALFKI";
Customer customer = cache.Get<Customer>(customerKey);
// Get customer from database if not found in cache
if (customer == null)
{
// Get customer from the database
customer = FetchCustomerFromDB("ALFKI");
// Create a new CacheItem
var cacheItem = new CacheItem(customer);
// Creating a Named Tags Dictionary
var namedTags = new NamedTagsDictionary();
/* Adding Named Tags to the Dictionary where
Keys are the names of the tags as string type and Values are of primitive type*/
namedTags.Add("VIP_Membership_Discount", 0.10 );
// Setting the named tag property of the cacheItem
cacheItem.NamedTags = namedTags;
cache.Add(customerKey, cacheItem);
}
String customerKey = "Customer:ALFKI";
Customer customer = cache.get(customerKey, Customer.class);
if (customer == null) {
// Fetch customer from database and create a com.alachisoft.ncache.sample.NamedTags.Customer object
customer = Customer.fetchCustomerFromDB("Customer:ALFKI");
}
NamedTagsDictionary namedTags = new NamedTagsDictionary();
namedTags.add("VIP_Membership_Discount", 0.10);
CacheItem cacheItem = new CacheItem(customer);
cacheItem.setNamedTags(namedTags);
cache.insert(customerKey, cacheItem);
System.out.println("Customer added to cache: " + customerKey);
// This is an async method
// Precondition: Cache is already connected
// Get product from database against given productID
var product = this.fetchProductFromDB(1001);
// Create a unique cache key for this product
var key = "Product:" + product.getProductID();
// Creating a Named Tags Dictionary
var productNamedTag = new ncache.NamedTagsDictionary();
// Adding Named Tags to the Dictionary where
// Keys are the names of the tags as string type
// Values are the instances of data, of primitive data structures.
productNamedTag.add("FlashSaleDiscount", 0.5);
// Create a new CacheItem
// You alose need to specify the FQN(Fully Qualified Name) of the class
var cacheItem = new ncache.CacheItem(product,"FQN.Product");
// Setting the named tag property of the CacheItem
cacheItem.setNamedTags(productNamedTag);
// Add product object to the cache
var version = await this.cache.add(key, cacheItem);
// CacheItem added successfully
# Precondition: Cache is already connected
# Get product from database against given productID
product = fetch_product_from_db()
# Create a unique cache key for this product
key = "Product:" + product.get_product_id()
# Creating a Named Tags Dictionary
product_named_tag = ncache.NamedTagsDictionary()
# Adding Named Tags to the Dict where
# Keys are the names of the tags as string type
# Values are the instances of data, of primitive data structures.
product_named_tag.add("FlashSaleDiscount", 0.5)
# Create a new CacheItem
cache_item = ncache.CacheItem(product)
# Setting the named tag property of the CacheItem
cache_item.set_named_tags(product_named_tag)
# Add product object to the cache
version = cache.add(key, cache_item)
# CacheItem added successfully
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
다음을 통해 명명된 태그 업데이트 CacheItem
항목을 먼저 가져와서 명명된 태그를 수정한 다음 다음을 사용하여 캐시에 다시 삽입해야 합니다. 끼워 넣다 방법.
다음 예제는 먼저 CacheItem
, 새로 명명된 태그로 목록을 생성한 다음 CacheItem
수정된 명명된 태그 목록과 함께 캐시에 저장됩니다. 따라서 기존 명명된 태그의 값을 덮어씁니다.
경고
제공 Null
명명된 태그 배열은 ArgumentNullException을 발생시킵니다.
// Store Owner has increased VIP membership discount to 12%
string customerKey = $"Customer:ALFKI";
Customer customer = cache.Get<Customer>(customerKey);
// Get the customer from database if not found in the cache
if (customer == null)
{
customer = FetchCustomerFromDB("ALFKI");
}
// Create a new CacheItem
var cacheItem = new CacheItem(customer);
// Creating a Named Tags Dictionary
var newNamedTags = new NamedTagsDictionary();
// Add Named Tag with the updated discount value to the Dictionary
newNamedTags.Add("VIP_Membership_Discount", 0.12);
// Updates the Named Tags with the new Named Tags
// Overrides the value of the existing Named Tags
cacheItem.NamedTags = newNamedTags;
// Reinserts the updated cacheItem into the cache with the modified Named Tags
cache.Insert(customerKey, cacheItem);
String customerKey = "Customer:ALFKI";
Customer customer = cache.get(customerKey, Customer.class);
if (customer == null) {
// Fetch customer from database and create a com.alachisoft.ncache.sample.NamedTags.Customer object
customer = Customer.fetchCustomerFromDB("Customer:ALFKI");
}
// Create a new CacheItem
CacheItem cacheItem = new CacheItem(customer);
NamedTagsDictionary namedTags = new NamedTagsDictionary();
namedTags.add("VIP_Membership_Discount", 0.50);
cacheItem.setNamedTags(namedTags);
cache.insert(customerKey, cacheItem);
System.out.println("Customer with updated named tags inserted into cache: " + customerKey);
// This is an async method
// Precondition: Cache is already connected
// Get cache item from cache against given key
var key = "Product:1001";
var cacheItem = await this.cache.getCacheItem(key);
// Create a Named Tags Dictionary
var newProductNamedTag = new ncache.NamedTagsDictionary();
// Adding Named Tags to the Dictionary where
// Keys are the names of the tags as string type
// Values are the updated instances of data, of primitive data types.
newProductNamedTag.add("FlashSaleDiscount", 0.7);
// Updates the Named Tags with the new Named Tags
// Overrides the value of the existing Named Tags
cacheItem.setNamedTags(newProductNamedTag);
//Re-inserts the cacheItem into cache with modified Named Tags
var version = await this.cache.insert(key, cacheItem);
// cacheItem is successfully added in cache with modified Named Tags
# Precondition: Cache is already connected
# Get cache item from cache against given key
key = "Product:1001"
cache_item = cache.get_cacheitem(key)
# Create a Named Tags Dictionary
new_product_named_tag = ncache.NamedTagsDictionary()
# Adding Named Tags to the Dict where
# Keys are the names of the tags as string type
# Values are the updated instances of data, of primitive data types.
new_product_named_tag.add("FlashSaleDiscount", 0.7)
# Updates the Named Tags with the new Named Tags
# Overrides the value of the existing Named Tags
cache_item.set_named_tags(new_product_named_tag)
# Re-insert the cacheItem into cache with modified Named Tags
version = cache.insert(key, cache_item)
# cacheItem is successfully added in cache with modified Named Tags
추가 자료
NCache 명명된 태그에 대한 샘플 애플리케이션을 제공합니다. GitHub의.
도 참조
태그 캐시 데이터
NamedTags를 사용한 SQL 쿼리
NamedTags를 사용한 SQL 삭제
데이터 만료