캐시에 데이터 업데이트/삽입
NCache 선형적으로 확장 가능한 기능을 제공합니다. Insert 이 API는 고성능 데이터 업데이트를 지원하기 위해 해당 메서드와 오버로드를 제공합니다. 기존 항목을 덮어쓰거나 키가 존재하지 않을 경우 새 항목을 원자적으로 추가하여 데이터 일관성을 보장합니다. Insert이러한 애플리케이션은 네트워크 직렬화 오버헤드를 줄이고 전체 시스템 처리량을 향상시킵니다.
사전 조건
사용하기 전에 NCache 클라이언트 측 API를 사용할 때는 다음 필수 조건을 충족해야 합니다.
- .NET 클라이언트 애플리케이션에 다음 NuGet 패키지를 설치하세요.
- 애플리케이션에 다음 네임스페이스를 포함합니다.
- 캐시가 실행 중이어야 합니다.
- 추가되는 데이터가 직렬화 가능.
- API 세부 정보는 다음을 참조하세요. 아이캐시, 캐시 항목 버전, 캐시 아이템, 끼워 넣다.
- Java 클라이언트 애플리케이션에 대해 다음 Maven 종속성을 추가합니다.
pom.xml 파일 :
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<!--for NCache Enterprise-->
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
- Java 클라이언트 애플리케이션에서 다음 패키지를 가져옵니다.
- 캐시가 실행 중이어야 합니다.
- 추가되는 데이터가 직렬화 가능.
- API 세부 정보는 다음을 참조하세요. 캐시, 캐시 항목 버전, 캐시 아이템, 삽입하다.
- Python 클라이언트 애플리케이션에 다음 패키지를 설치하세요.
- 애플리케이션에서 다음 패키지를 가져옵니다.
- 캐시가 실행 중이어야 합니다.
- 추가되는 데이터가 직렬화 가능.
- API 세부 정보는 다음을 참조하세요. 캐시, 캐시 아이템, 캐시 항목 버전, 삽입하다.
- Node.js 클라이언트 애플리케이션에 다음 모듈을 설치하고 포함시키세요.
- 애플리케이션에 다음 클래스를 포함합니다.
- 캐시가 실행 중이어야 합니다.
- 추가되는 데이터가 직렬화 가능.
- API 세부 정보는 다음을 참조하세요. 캐시, 캐시 항목 버전, 캐시 아이템, 삽입하다.
- .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 웹 사이트.
캐시에 있는 객체를 업데이트하는 방법
The Insert 이 메서드는 애플리케이션 계층과 분산 저장소 간의 기본 동기화를 위한 표준적인 접근 방식입니다. 다양한 오버로드된 메서드를 사용하여 캐시에 있는 사용자 지정 객체를 업데이트할 수 있습니다. Insert 방법. 이것은 CacheItemVersion.
중대한
- 키가 이미 있는 경우 기존 캐시 항목을 동일한 키로 덮어씁니다.
- 키가 없으면 작업에서 캐시에 항목을 대신 추가합니다.
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
다음 예에서는 기존 개체의 값을 가져와 동일한 키에 대해 캐시에 삽입합니다. 키가 존재하지 않으면 캐시에 추가합니다.
try
{
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
// Get updated customer from database against given customer key
Customer customer = FetchCustomerFromDB(customerKey);
// Insert Customer object to cache
CacheItemVersion version = cache.Insert(customerKey, customer);
// Item updated in cache successfully
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
try
{
// Precondition: Cache is already connected
// Get updated product from database against given product ID
Product product = fetchProductFromDB(1001);
// Insert Product object to cache
CacheItemVersion version = cache.insert("1001", product);
// Item updated in cache successfully
}
catch (OperationFailedException ex)
{
// 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
# Get updated product from database against given product ID
product = fetch_product_from_db(1001)
# Generate a unique cache key for this product
key = "Product:" + product.get_product_id()
# Insert Product object to cache
version = cache.insert(key, product)
# Item updated in cache successfully
except Exception as error:
# Exception can occur due to:
# Connection Failures
# Operation Timeout
# Operation performed during state transfer
print(f"An error occurred: {error}")
try
{
// Precondition: Cache is already connected
//Get updated product from database against given product ID
product = await this.fetchProductFromDB(1001);
// Generate a unique cache key for this product
var key = "Product:" + product.getProductID();
// Insert Product object to cache
var version = await this.cache.insert(key, product);
// Item updated in cache successfully
}
catch(error)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
// Using NCache Enterprise 4.9.1
try
{
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
// Get updated customer from database against given customer key
Customer customer = FetchCustomerFromDB(customerKey);
// Insert Customer object to cache
CacheItemVersion version = cache.Insert(customerKey, customer);
// Item updated in cache successfully
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
만료일을 사용하여 객체를 업데이트하는 방법
CacheItem 에서 제공하는 사용자 정의 클래스입니다. NCache 이 클래스는 캐시에 데이터를 추가하는 데 사용할 수 있으며, 객체와 관련된 추가 사양을 이 클래스의 속성으로 설정할 수도 있습니다.
The CacheItem 고유 키를 기준으로 캐시가 업데이트됩니다. 만료 정책을 할당함으로써 CacheItem캐시는 자동화된 메모리 관리를 가능하게 하여 오래되거나 검증되지 않은 데이터가 캐시에 남아 있는 것을 방지합니다.
중대한
- 키가 이미 있는 경우 기존 캐시 항목을 동일한 키로 덮어씁니다.
- 키가 없으면 작업에서 캐시에 항목을 대신 추가합니다.
다음 예제에서는 기존 값의 업데이트된 값을 가져옵니다. CacheItem 동일한 키에 대해 캐시에 삽입합니다. 키가 없으면 캐시에 추가합니다.
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
// Get the customer from the database
Customer customer = FetchCustomerFromDB(customerKey);
// Update Customer's Phone
customer.Phone = "0921-12 34 65";
// Create CacheItem
CacheItem cacheItem = new CacheItem(customer);
cacheItem.Expiration = new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5));
// Update customer in DB and Cache
if (UpdateDB(customer))
{
cache.Insert(customerKey, cacheItem);
}
// Precondition: Cache is already connected
String customerKey = "ALFKI";
// Get the customer from the database
Customer customer = fetchCustomerFromDB(customerKey);
// Update Customer's Phone
customer.setPhone("0921-12 34 65");
// Create CacheItem
CacheItem cacheItem = new CacheItem(customer);
cacheItem.setExpiration(new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5)));
// Update customer in DB and Cache
cache.insert(customerKey, cacheItem);
# Precondition: Cache is already connected
# Get the customer from the 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
# You can OPTIONALLY specify multiple properties e.g.Priority, Expiration
# These properties are explained in successive chapters
cache_item = ncache.CacheItem(product)
# Insert CacheItem to cache
version = cache.insert(key, cache_item)
// Precondition: Cache is already connected
// Get the customer from the database
product = await this.fetchProductFromDB();
// Generate a unique cache key for this product
var key = "Product:" + product.getProductID();
// Create a new CacheItem for this product
// You can OPTIONALLY specify multiple properties e.g. Priority, Expiration
// These properties are explained in successive chapters
var cacheItem = new ncache.CacheItem(product);
// Insert CacheItem to cache
var version = await this.cache.insert(key,cacheItem);
// Using NCache Enterprise 4.9.1
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
// Get the customer from the database
Customer customer = FetchCustomerFromDB(customerKey);
// Update Customer's Phone
customer.CustomerPhone = "0921-12 34 65";
CacheItem cacheItem = new CacheItem(customer);
cache.Insert(customerKey, customer, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0), CacheItemPriority.Default);
추가 자료
NCache 기본 작업에 대한 샘플 응용 프로그램을 제공합니다. GitHub의.
도 참조
.그물: Alachisoft.NCache.고객 네임 스페이스.
자바 : com.alachisoft.ncache.client 네임 스페이스.
파이썬 : ncache.클라이언트 클래스입니다.
Node.js : 캐시 클래스입니다.