명명된 태그를 캐시 항목과 연결하려면 명명된 태그 목록을 제공해야 하며, 각각에는 다음과 같은 두 개의 매개변수가 있습니다. 키 태그의 이름인 문자열로, 가치 키에 할당된 값인 기본 유형으로. NCache 그러면 객체를 이러한 명명된 태그와 연결할 수 있습니다. 명명된 태그를 사용하여 캐시에 항목을 추가한 다음, 이전에 추가한 명명된 태그를 사용하여 캐시에서 해당 항목을 검색할 수 있습니다. 특정 명명된 태그가 있는 항목이 이미 캐시에 추가된 경우, NCache 이 기능은 사용자가 캐시에 이미 존재하는 명명된 태그를 업데이트할 수 있는 옵션을 제공합니다. 이를 통해 개발자는 런타임에 캐시 항목에 대한 검색 가능한 인덱스를 생성할 수 있습니다.
사전 조건
사용하기 전에 NCache 클라이언트 측 API를 사용할 때는 다음 필수 조건을 충족해야 합니다.
- Java 클라이언트 애플리케이션에 대해 다음 Maven 종속성을 추가합니다.
pom.xml 파일 :
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<!--for NCache Enterprise-->
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
- .NET 클라이언트 애플리케이션에 다음 NuGet 패키지 중 하나를 설치하세요.
- 기업 :
Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
- 새로운 콘솔 애플리케이션을 만듭니다.
- 추가되는 데이터가 직렬화 가능.
- 추가 NCache 참고자료 위치를 찾아서
%NCHOME%\NCache\bin\assembly\4.0 추가 Alachisoft.NCache.Web Alachisoft.NCache.Runtime 적절한.
- 포함시키다
Alachisoft.NCache.Web.Caching 애플리케이션의 네임스페이스.
- 에 대한 자세한 내용을 보려면 NCache 레거시 API를 다운로드하세요 NCache 4.9 문서는 다음과 같이 사용 가능합니다. .지퍼 에 파일 Alachisoft 웹 사이트.
NCache 맞춤 클래스를 제공하고, CacheItem 캐시에 데이터를 추가하는 데 사용할 수 있습니다. 이 클래스는 데이터를 value 속성으로 캡슐화합니다. CacheItem 또한 개체와 관련된 추가 사양을 property 명명된 태그 클래스의. 다음 예제에서 명명된 태그는 다음과 같이 할당하여 설정됩니다. property of CacheItem.
try
{
// 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);
}
}
catch (OperationFailedException ex)
{
// NCache specific exception
if(ex.ErrorCode == NCacheErrorCodes.KEY_ALREADY_EXISTS)
{
// An item with the same key already exists
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// 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(customerKey, Customer.class);
// Get customer from database if not found in cache
if (customer == null) {
// Get customer from the database
customer = Customer.fetchCustomerFromDB("ALFKI");
}
// Create a new CacheItem
CacheItem cacheItem = new CacheItem(customer);
// Creating a Named Tags Dictionary
NamedTagsDictionary 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.setNamedTags(namedTags);
cache.add(customerKey, cacheItem);
}
catch (OperationFailedException ex)
{
if (ex.getErrorCode() == NCacheErrorCodes.KEY_ALREADY_EXISTS)
{
// An item with the same key already exists
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like IllegalArgumentException or NullPointerException
}
try:
# 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
customer_key = "Customer:ALFKI"
cache_item = cache.get_cacheitem(customer_key)
# Get customer from cache item if exists
customer = cache_item.get_value(Customer) if cache_item else None
# Get customer from database if not found in cache
if customer is None:
# Get customer from the database
customer = Customer.fetch_customer_from_db("ALFKI")
# Create a new CacheItem
cache_item = ncache.CacheItem(customer)
# Creating a Named Tags Dictionary
customer_named_tag = ncache.NamedTagsDictionary()
# Adding Named Tags to the Dictionary
# Where keys are the names of the tags as string type and values are of primitive type
customer_named_tag.insert("VIP_Membership_Discount", 0.10)
# Setting the NamedTag property of the cacheItem
cache_item.set_named_tags(customer_named_tag)
version = cache.add(customer_key, cache_item)
except Exception as error:
# Exception can occur due to:
# Connection Failures
# Operation Timeout
# Operation during state transfer
print("An error occurred:", str(error))
try
{
// This is an async method
// 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
const customerKey = "Customer:ALFKI";
let customer = cache.get(customerKey);
// Get customer from database if not found in cache
if (!customer)
{
// Get customer from the database
customer = await Customer.fetchCustomerFromDB("ALFKI");
}
// Create a new CacheItem
// You also need to specify the FQN (Fully Qualified Name) of the class
var cacheItem = new ncache.CacheItem(product,"FQN.Product");
// 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 types
productNamedTag.add("VIP_Membership_Discount", 0.10);
// 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);
}
catch (error)
{
// Handle errors
}
// Using NCache Enterprise 4.9.1
try
{
// In an online-store, VIP members are offered 10% discount
// A customer with VIP membership has signed in to the website
// Precondition: Cache is already connected
string customerKey = "Customer:ALFKI";
Customer customer = (Customer)cache.Get(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
CacheItem 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;
// Add cacheItem to cache
cache.Add(customerKey, cacheItem);
}
}
catch (OperationFailedException ex)
{
// NCache specific exception
if(ex.ErrorCode == NCacheErrorCodes.KEY_ALREADY_EXISTS)
{
// An item with the same key already exists
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
다음을 통해 명명된 태그 업데이트 CacheItem 항목을 먼저 가져와서 명명된 태그를 수정한 다음 다음을 사용하여 캐시에 다시 삽입해야 합니다. 끼워 넣다 방법.
다음 예제는 먼저 CacheItem, 새로 명명된 태그로 목록을 생성한 다음 CacheItem 수정된 명명된 태그 목록과 함께 캐시에 저장됩니다. 따라서 기존 명명된 태그의 값을 덮어씁니다.
경고
제공 Null 명명된 태그 배열은 ArgumentNullException을 발생시킵니다.
// Precondition: Cache is already connected
// 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);
// Precondition: Cache is already connected
// Store Owner has increased VIP membership discount to 12%
String customerKey = "Customer:ALFKI";
Customer customer = cache.get(customerKey, Customer.class);
// Get the customer from database if not found in the cache
if (customer == null) {
// Fetch customer from database and create a Customer object
customer = Customer.fetchCustomerFromDB("ALFKI");
}
// Create a new CacheItem
CacheItem cacheItem = new CacheItem(customer);
// Creating a Named Tags Dictionary
NamedTagsDictionary namedTags = new NamedTagsDictionary();
// Add Named Tag with the updated discount value to the Dictionary
namedTags.add("VIP_Membership_Discount", 0.12);
// Updates the Named Tags with the new Named Tags
// Overrides the value of the existing Named Tags
cacheItem.setNamedTags(namedTags);
// Reinserts the updated cacheItem into the cache with the modified Named Tags
cache.insert(customerKey, cacheItem);
# Precondition: Cache is already connected
# Store Owner has increased VIP membership discount to 12%
customer_key = "Customer:ALFKI"
cache_item = cache.get_cacheitem(key)
# Get customer from database if not found in cache
if customer is None:
# Get customer from the database
customer = Customer.fetch_customer_from_db("ALFKI")
# Create a Named Tags Dictionary
new_product_named_tag = ncache.NamedTagsDictionary()
# Add Named Tag with the updated discount value to the Dictionary
new_product_named_tag.add("VIP_Membership_Discount", 0.12)
# 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)
# Reinsert the updated cacheItem into cache with modified Named Tags
version = cache.insert(key, cache_item)
// Precondition: Cache is already connected
// This is an async method
// Store Owner has increased VIP membership discount to 12%
const customerKey = "Customer:ALFKI";
let customer = cache.get(customerKey);
if (!customer) {
customer = await fetchCustomerFromDB("ALFKI");
}
// Create a new CacheItem
const cacheItem = new CacheItem(customer);
// Creating a Named Tags Dictionary
var newProductNamedTag = new ncache.NamedTagsDictionary();
// Add Named Tag with the updated discount value to the Dictionary
newProductNamedTag.add("VIP_Membership_Discount", 0.12);
// Updates the Named Tags with the new Named Tags
// Overrides the value of the existing Named Tags
cacheItem.setNamedTags(newProductNamedTag);
// Reinserts the cacheItem into cache with modified Named Tags
var version = await this.cache.insert(key, cacheItem);
추가 자료
NCache 명명된 태그에 대한 샘플 애플리케이션을 제공합니다. GitHub의.
도 참조
.그물: Alachisoft.NCache.런타임.캐싱 네임 스페이스.
자바 : com.alachisoft.ncache.런타임.캐싱 네임 스페이스.
파이썬 : ncache.런타임.캐싱 클래스입니다.
Node.js : NamedTags사전 클래스입니다.