기존 캐시 데이터 검색
DaVinci에는 NCache 또한 캐시 데이터 쿼리를 지원하며 저장소의 키-값 아키텍처를 활용하여 데이터 검색 방법을 최대화합니다. 항목과 연결된 고유 키를 사용하여 캐시에서 사용자 정의 개체, CacheItem 또는 대량 항목을 검색할 수 있습니다. NCache 다양한 과부하를 제공합니다. Get
지정된 키를 기반으로 캐시 데이터에서 항목을 가져오는 메서드입니다.
기존 캐시 데이터를 검색하기 위한 사전 요구 사항
캐시 데이터 검색
주의 사항
이 기능은 다음에서도 사용할 수 있습니다. NCache Professional.
다양한 오버로드를 사용하여 캐시 데이터에서 사용자 정의 개체를 가져올 수 있습니다. Get
캐시 항목의 키를 지정하여 메서드. 객체는 템플릿으로 검색되므로 사용자 정의 클래스 객체인 경우 그에 따라 유형 변환해야 합니다.
중대한
키가 캐시에 없으면 null 값이 반환됩니다.
다음 예에서는 빠른 지정된 키가 있는 클래스. 명심하십시오 Get
그에 따라 캐스팅되어야 하는 템플릿을 반환하므로 객체는 Customer
이 예를 입력하세요.
// Precondition: Cache is already connected
string customerKey = "Customer:ALFKI";
// Retrieve the Customer object
Customer customer = cache.Get<Customer>(customerKey);
if (customer != null)
{
Console.WriteLine($"Customer: {customer.ContactName}, Address : {customer.Address}");
}
// Precondition: Cache is already connected
String customerKey = "ALFKI";
// Retrieve the Customer object
Customer customer = cache.get(customerKey, Customer.class);
System.out.println("Customer with key " + customerKey + " has value " + customer);
// This is an async method
// Precondition: Cache is already connected
// Get Product from database against given ProductID
var product = this.fetchProductFromDB(1001);
// Get key to fetch from cache
var key = "Product:" + this.product.getProductID();
// Create a CacheItem
var cacheItem = new ncache.CacheItem(product);
await this.cache.insert(key , cacheItem);
// Get item against key from cache in given Class type
var retrievedItem = await this.cache.get(key, Product);
if (retrievedItem != null)
{
// Perform your logic
}
else
{
// Null returned; no key exists
}
# Precondition: Cache is already connected
# Get Product from database against given ProductID
product = fetch_product_from_db()
# Get key to fetch from cache
key = "Product:" + product.get_product_id()
# Create a CacheItem
cache_item = ncache.CacheItem(product)
cache.insert(key, cache_item)
# Get item against key from cache in given Class type
retrieved_item = cache.get(key, Product)
if retrieved_item is not None:
# Perform your logic
print("Item received")
else:
# Null returned; no key exists
print("Key does not exist")
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
캐시에서 CacheItem 검색
주의 사항
이 기능은 다음에서도 사용할 수 있습니다. NCache Professional.
NCache 기존 검색을 허용합니다 CacheItem
전용을 사용하여 캐시에서 GetCacheItem
API. 이것은 반환 CacheItem
지정된 키와 연결됩니다. 그것의 Value
속성은 데이터를 캡슐화합니다.
다음 예에서는 기존 CacheItem
지정된 키를 사용하여 결과가 다음과 같은지 확인합니다. 빠른 type 및 type 은 그에 따라 캐스팅합니다.
string customerKey = "Customer:ALFKI";
// Retrieve the CacheItem
CacheItem retrievedCacheItem = cache.GetCacheItem(customerKey);
if (retrievedCacheItem != null)
{
Customer customer = retrievedCacheItem.GetValue<Customer>();
Console.WriteLine($"Customer: {customer.ContactName}, Address : {customer.Address}");
}
String customerKey = "ALFKI";
// Retrieve the CacheItem from cache
CacheItem retrievedCacheItem = cache.getCacheItem(customerKey);
if (retrievedCacheItem != null) {
// Get the Customer object from the CacheItem
Customer customer = retrievedCacheItem.getValue(Customer.class);
System.out.println("Customer: " + customer.getContactName() + ", Phone: " + customer.getPhone());
}
// This is an async method
// Get key to fetch from cache
var key = "Product:" + this.product.getProductID();
// Get CacheItem from cache
var retrievedCacheItem = this.cache.getCacheItem(key);
// Get item against key from cache in given Class type
if (retrievedCacheItem != null)
{
this.cache.getValue(Product);
// Perform your logic
}
else
{
// Null returned; no key exists
}
# Generate key to fetch from cache
key = "Product:1001"
# Get CacheItem from cache
retrieved_cache_item = cache.get_cacheitem(key)
# Get item against key from cache in given Class type
if retrieved_cache_item is not None:
value = retrieved_cache_item.get_value(Product)
# Perform your logic
else:
# Null returned no key exists
print("Key does not exist")
캐시에서 대량 항목 검색
주의 사항
이 기능은 다음에서도 사용할 수 있습니다. NCache Professional.
NCache 단일 호출로 항목을 동기식으로 대량 검색할 수 있어 네트워크 비용이 절감됩니다. 다양한 과부하 GetBulk
메소드는 지정된 캐시 키에 대한 캐시 데이터에서 객체를 검색합니다. 값은 템플릿으로 검색되므로 사용자 정의 클래스 객체인 경우 그에 따라 유형 변환이 필요합니다.
중대한
키가 캐시 항목의 캐시 사전에 존재하는 경우 해당 키가 반환됩니다.
다음 예제는 빠른 수업. 결과는 IDictionary
키의 실제 값을 얻기 위해 열거할 수 있는 키 및 값. 부터 GetBulk
(캐스트가 필요한) 템플릿을 반환하면 객체는 Customer
이 예를 입력하세요. 지정된 키가 캐시에 없으면 null 값이 반환됩니다.
// Create an array of all keys to fetch
String[] keys = new String[]
{
"Customer:ALFKI", "Customer:ANATR", "Customer:ANTON", "Customer:AROUT",
"Customer:BERGS"
};
// Get items from cache
IDictionary<string, Customer> retrievedItems = cache.GetBulk<Customer>(keys);
// Retrieve customers and their addresses from dictionary
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedItems)
{
Console.WriteLine($"Customer: {retrievedItem.Value.ContactName}, Address : { retrievedItem.Value.Address}");
}
// Create an list of all keys to fetch
List<String> keys = List.of(
"Customer:ALFKI",
"Customer:ANATR",
"Customer:ANTON",
"Customer:AROUT",
"Customer:BERGS"
);
// Get items from cache
Map<String, Customer> retrievedItems = cache.getBulk(keys, Customer.class);
// Retrieve customers and their addresses from dictionary
for (Map.Entry<String, Customer> retrievedItem : retrievedItems.entrySet())
{
System.out.println("Customer: " + retrievedItem.getValue().getContactName() + ", Address: " + retrievedItem.getValue().getAddress());
}
// This is an async method
// Get Product from database against given ProductID
var products = await this.fetchProductFromDB();
// Get keys to fetch from cache
var keys = [products.length];
var index = 0;
products.forEach(product => {
keys[index] ="Product:" + this.product.getProductID();
index++;
});
// Get bulk from cache
var retrievedItems = await this.cache.getBulk(keys,Product);
// Check if any keys have failed to be retrieved
if(retrievedItems.size() == keys.length)
{
retrievedItems.forEach(entry => {
if(entry.getValue() instanceof Product)
{
// Perform operations according to business logic
}
else
{
// Object not of Product type
}
});
}
else
{
// Not all of the keys are present in cache
}
# Get Products from database
products = fetch_products_from_db()
# Get keys to fetch from cache
keys = []
index = 0
for product in products:
keys.append( "Product:" + product.get_product_id())
# Get bulk from cache
retrieved_items = cache.get_bulk(keys, Product)
# Check if any keys have failed to be retrieved
if len(retrieved_items) is len(keys):
# Perform operations according to business logic
print("All the items were retrieved successfully")
else:
# Not all the keys are present in cache
print("Some of the keys were not found")
캐시에서 대량의 CacheItem 검색
주의 사항
이 기능은 다음에서도 사용할 수 있습니다. NCache Professional.
당신은 또한 대량 검색할 수 있습니다 CacheItem
를 사용하여 GetCacheItemBulk
방법. 다음은 예입니다.
// Create an array of all keys to fetch
String[] keys = new String[]
{
"Customer:ALFKI", "Customer:ANATR", "Customer:ANTON", "Customer:AROUT",
"Customer:BERGS"
};
// Get items from cache
IDictionary<string, Customer> retrievedItems = cache.GetBulk<Customer>(keys);
// Retrieve customers and their addresses from dictionary
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedItems)
{
Console.WriteLine($"Customer: {retrievedItem.Value.ContactName}, Address : { retrievedItem.Value.Address}");
}
// Fetch a products array
Product[] products = fetchProducts();
// Get keys to fetch from cache
ArrayList<String> keys = new ArrayList<>(products.length);
for (Product product : products)
{
String key = "Product:" + product.getProductID();
keys.add(key);
cache.insert(key , product);
}
// Retrieve items from cache in a Map
java.util.Map<String, CacheItem> retrievedItems = cache.getCacheItemBulk(keys);
// Check if any keys have failed to be retrieved
if (retrievedItems.size() == keys.size())
{
for (java.util.Map.Entry<String, CacheItem> entry : retrievedItems.entrySet())
{
if (entry.getValue() != null)
{
Product prod = entry.getValue().<Product>getValue(Product.class);
// Perform operations according to business logic
}
else
{
// Object not of Product type
}
}
}
else
{
// Not all of the keys are present in cache
for (String key : keys)
{
if (!retrievedItems.containsKey(key))
{
// This key does not exist in cache
}
}
}
// This is an async method
// Get Product from database against given ProductID
var products = await this.fetchProductFromDB();
// Get keys to fetch from cache
var keys = [products.length];
var index = 0;
products.forEach(product => {
keys[index] ="Product:" + this.product.getProductID();
index++;
});
// Get bulk from cache
var retrievedItems = this.cache.getCacheItemBulk(keys);
// Check if any keys have failed to be retrieved
if(retrievedItems.size() == keys.length)
{
retrievedItems.forEach(entry => {
if(entry.getValue() instanceof ncache.CacheItem)
{
var prod = entry.getValue();
// Perform operations according to business logic
}
else
{
// Object not of Product type
}
});
}
else
{
// Not all of the keys are present in cache
keys.forEach(key => {
if(retrievedItems.containsKey(key) == false)
{
// key does not exist in cache
}
});
}
# Get Products from database
products = fetch_products_from_db()
# Get keys to fetch from cache
keys = []
index = 0
for product in products:
keys.append( "Product:" + product.get_product_id())
# Get bulk from cache
retrieved_items = cache.get_cacheitem_bulk(keys)
# Check if any keys have failed to be retrieved
if len(retrieved_items) is len(keys):
for item in retrieved_items:
product = retrieved_items[item].get_value(Product)
# Perform operations according to business logic
else:
# Not all the keys are present in cache
print("Some of the keys were not found")
추가 자료
NCache 기본 작업에 대한 샘플 응용 프로그램을 제공합니다. GitHub의.
도 참조
.그물: Alachisoft.NCache.고객 네임 스페이스.
자바 : COM.alachisoft.ncache.고객 네임 스페이스.
Node.js : 캐시 클래스입니다.
파이썬 : ncache.고객 클래스입니다.