캐시 데이터 제거
NCache 캐시 항목 키를 입력 매개 변수로 허용하여 캐시 데이터에서 객체를 제거하기 위해 서로 다른 반환 유형을 가진 다음 두 가지 오버로드를 제공합니다.
bool Remove<T>
void Remove
사전 조건
캐시에서 개체 제거
주의 사항
이 기능은 다음에서도 사용할 수 있습니다. NCache Professional.
다음 중 하나를 사용하여 사용자 정의 클래스의 개체를 캐시 데이터에서 제거할 수 있습니다. Remove
방법.
중대한
캐시 데이터에 항목이 없으면 null이 반환됩니다.
팁
항목이 제거되었는지 확인하는 빠른 방법 중 하나는 캐시 클래스의 다음 속성 중 하나를 사용하는 것입니다.
Count
캐시에 있는 항목 수를 반환합니다.
Contains
캐시에 지정된 키가 있는지 확인합니다.
다음 예제에서는 지정된 키에 해당하는 항목을 제거하고 반환된 객체를 빠른 객체인지 여부를 확인하는 객체 빠른 유형 여부. 그렇지 않은 경우 캐시에 다시 추가됩니다.
// Precondition: Cache is already connected
// Unique cache key of product to remove
string key = $"Product:{product.ProductID}";
string customerKey = $"Customer:ALFKI";
// Create an object to store removed item
Customer customerRemoved = null;
// Remove specified item from cache
bool isItemRemoved = cache.Remove(customerKey, out customerRemoved);
if (isItemRemoved)
{
Console.WriteLine($"Customer with ID {customerRemoved.CustomerID} has been removed");
}
String customerKey = "ALFKI";
// Remove the cache item from the cache
cache.remove(customerKey, Customer.class);
System.out.println("Cache item with key " + customerKey + " has been removed.");
// This is an async method
// Pre-condition: Cache is already connected
// Unique cache key of product to remove
var key = "Product:" + this.product.getProductID();
// To return item removed
var itemRemoved = new Product();
// Remove specified item
await this.cache.remove(key,itemRemoved);
// Check if object is returned
if (itemRemoved != null)
{
// Check if it is of Product type
if (itemRemoved instanceof Product)
{
// Perform operations
}
else
{
// The object removed was not of Product type
// Add it back to the cache
var ver = await this.cache.add(key, itemRemoved);
}
}
else
{
// Item does not exist in cache
}
# Pre-condition: Cache is already connected
# Unique cache key of product to remove
key = "Product:1001"
# Remove specified item
item_removed = cache.remove(key, Product)
# Check if object is returned
if item_removed is not None:
# Perform operations
print(item_removed)
else:
# Item does not exist in cache
print("Key not found.")
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
계산 비용을 절약하려면 다음을 사용할 수 있습니다. void Remove
아무 것도 반환하지 않으므로 더 빠르게 수행되는 함수입니다. 사용 방법은 다음과 같습니다.
string customerKey = $"Customer:ALFKI";
cache.Remove(customerKey);
String customerKey = "ALFKI";
cache.delete(customerKey);
// Unique cache key of product to remove
var key = "Product:" + this.product.getProductID();
// Remove specified item
await this.cache.remove(key);
# Unique cache key of product to remove
key = "Product:1001"
# Remove specified item
cache.delete(key)
캐시에서 대량 항목 제거
주의 사항
이 기능은 다음에서도 사용할 수 있습니다. NCache Professional.
NCache 를 제공합니다 RemoveBulk
지정된 캐시 키 배열에 대해 대량의 캐시 항목을 제거하는 방법입니다. 캐시에서 제거된 키와 개체의 사전을 반환합니다.
중대한
- 지정된 항목이 캐시에 있는 경우 제거된 키 및 개체의 사전이 반환됩니다.
팁
항목이 제거되었는지 확인하는 빠른 방법 중 하나는 캐시 클래스의 다음 속성 중 하나를 사용하는 것입니다.
Count
캐시에 있는 항목 수를 반환합니다.
Contains
캐시에 지정된 키가 있는지 확인합니다.
다음 예는 기존 캐시 항목의 대부분을 제거하고 반환된 객체를 다음으로 캐스트합니다. 빠른 사물.
// Create an array of all keys to remove
String[] keysToRemove = new String[]
{
"Customer:ALFKI", "Customer:ANATR", "Customer:ANTON", "Customer:AROUT", "Customer:BERGS"
};
// Create dictionary to store removed items
IDictionary<string, Customer> removedItems;
// Remove items from DB
if (DeleteFromDB(keysToRemove))
{
// Remove bulk items from cache
cache.RemoveBulk(keysToRemove, out removedItems);
if (removedItems.Count != keysToRemove.Length)
Console.WriteLine($"Failed to remove {keysToRemove.Length - removedItems.Count} items from cache.");
}
// Create an array of all keys to remove
var keysToRemove = List.of("Customer:ALFKI", "Customer:ANATR", "Customer:ANTON", "Customer:AROUT", "Customer:BERGS");
// Create dictionary to store removed items
Map<String, Customer> removedItems;
// Remove items from DB
if (deleteFromDB(keysToRemove)) {
// Remove bulk items from cache
removedItems = cache.removeBulk(keysToRemove, Customer.class);
if (removedItems.size() != keysToRemove.size())
System.out.println("Failed to remove " + (keysToRemove.size() - removedItems.size()) + " items from 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 removedItems = await this.cache.removeBulk(keys);
// Check if any keys have failed to be retrieved
if(removedItems.size() > 0)
{
removedItems.forEach(entry => {
if(entry.getValue() instanceof Product)
{
var prod = entry.getValue();
// Perform operations according to business logic
}
else
{
// Object not of Product type
}
});
}
else
{
// No objects removed
}
# Get Products from database
products = fetch_products_from_db()
# Get keys to remove from cache
keys = []
index = 0
for product in products:
keys[index] = "Product:" + product.get_product_id()
index = index + 1
# Remove bulk from cache
removed_items = cache.remove_bulk(keys, Product)
# Check if any keys have failed to be removed
if len(removed_items) is len(keys):
# Perform operations according to business logic
print("All the items were removed successfully")
else:
# Not all the keys are present in cache
print("Some of the keys were not removed")
비동기 API를 사용하여 캐시에서 개체 제거
RemoveAsync
의 객체를 반환합니다. 작업 클래스 클라이언트 응용 프로그램의 비즈니스 요구 사항에 따라 추가로 사용할 수 있습니다. NCache 작업의 성공 또는 실패를 알리기 위해 세 가지 다른 상태 플래그를 제공합니다.
중대한
같지 않은 Remove
및 RemoveBulk
, RemoveAsync
비동기 작업이고 가져와야 하므로 일반적으로 제거된 개체를 응용 프로그램에 반환하지 않습니다.
string customerKey = $"Customer:ALFKI";
// Remove specified item from cache
Task<Customer> task = cache.RemoveAsync<Customer>(customerKey);
//This task object can be used as per your business needs
if (task.IsCompleted)
{
// Get Customer object from task result
Customer customer = task.Result;
Console.WriteLine($"Item {customer.CustomerID} has been removed.");
}
String customerKey = "Customer:ALFKI";
cache.removeAsync(customerKey, Customer.class);
System.out.println("Item '" + customerKey + "' has been removed.");
# Generate a unique cache key for this product
key = f"Product:1001"
# Remove Product object from cache asynchronously
async def remove_async():
task = cache.remove_async(key, Product)
value = await task
asyncio.run(remove_async())
# This task object can be used as per your business needs
추가 자료
NCache 기본 작업에 대한 샘플 응용 프로그램을 제공합니다. GitHub의.
도 참조
.그물: Alachisoft.NCache.고객 네임 스페이스.
자바 : COM.alachisoft.ncache.고객 네임 스페이스.
Node.js : 캐시 클래스입니다.
파이썬 : ncache.고객 클래스입니다.