删除缓存数据
NCache 允许开发者通过显式删除项来移除缓存数据,从而释放内存并保持分布式缓存中的数据新鲜度。它支持 .NET、Java、Python 和 Node.js 应用程序的同步、异步和批量删除操作。 Remove 该方法通过显式地使过时的条目失效来维护数据一致性,而非通用方法 Remove 过载为性能关键型操作提供了一个不可返回的选项。
硬件需求
使用前 NCache 客户端 API 应确保满足以下先决条件:
- 在您的 .NET 客户端应用程序中安装以下 NuGet 包:
- 在您的应用程序中包含以下命名空间:
- 缓存必须正在运行。
- 确保要添加的数据是 可序列化.
- 有关 API 详细信息,请参阅: 缓存, 计数, 删除, 包含.
- 为您的 Java 客户端应用程序添加以下 Maven 依赖项: pom.xml 文件:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<!--for NCache-->
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
- 在 Java 客户端应用程序中导入以下包:
- 缓存必须正在运行。
- 确保要添加的数据是 可序列化.
- 有关 API 详细信息,请参阅: 缓存, 获取计数, 去掉.
- 在您的 Python 客户端应用程序中安装以下软件包:
- 在您的应用程序中导入以下包:
- 缓存必须正在运行。
- 确保要添加的数据是 可序列化.
- 有关 API 详细信息,请参阅: 缓存, 缓存项, 得到, 获取缓存项, 去掉.
- 在您的Node.js客户端应用程序中安装并包含以下模块:
- 在您的应用程序中包含以下类:
- 缓存必须正在运行。
- 确保要添加的数据是 可序列化.
- 有关 API 详细信息,请参阅: 缓存, 去掉.
- 在您的 .NET 客户端应用程序中安装以下任一 NuGet 包:
- NCache:
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 官方网站.
如何从缓存中移除对象
可以使用以下任何一种方法从缓存数据中删除自定义类的对象 Remove 方法。
重要提示
如果缓存数据中不存在某项,则返回 null。
注意:
验证物品是否已被移除的一种快捷方法是使用…… Count 财产或 Contains 缓存方法:
Count 返回缓存中存在的项目数。
Contains 验证缓存中是否存在指定的键。
以下示例删除与指定键关联的项。.NET 重载返回一个布尔值结果,并提供已删除的项。 客户 通过 out 参数。
try
{
// Precondition: Cache is already connected
// Unique cache key of product to remove
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");
}
}
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
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.");
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like NullPointerException or IllegalArgumentException
}
try:
# Precondition: 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.")
except Exception as error:
# Exception can occur due to:
# Connection Failures
# Operation Timeout
# Operation during state transfer
print("An error occurred:", str(error))
try
{
// Precondition: Cache is already connected
// This is an async method
// 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
}
}
catch(error)
{
// Handle errors
}
// Using NCache 4.9.1
try
{
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
// Create an object to store removed item
Customer customerRemoved = null;
// Remove specified item from cache
object isItemRemoved = cache.Remove(customerKey);
if (isItemRemoved != null && isItemRemoved is Customer)
{
customerRemoved = (Customer)isItemRemoved;
Console.WriteLine($"Customer with ID {customerRemoved.CustomerID} has been removed");
}
}
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
}
注意:
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
如果你想节省你的计算成本,你可以使用 void Remove 函数不返回任何内容,因此执行速度更快。 这是您可以使用它的方法。
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
// Delete specified item
cache.Remove(customerKey);
// Precondition: Cache is already connected
String customerKey = "ALFKI";
// Delete specified item
cache.delete(customerKey);
# Precondition: Cache is already connected
key = "Product:1001"
# Delete specified item
cache.delete(key)
// Precondition: Cache is already connected
var key = "Product:" + this.product.getProductID();
// Delete specified item
await this.cache.remove(key);
// Using NCache 4.9.1
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
// Delete specified item
cache.Delete(customerKey);
更多资讯
NCache 提供基本操作的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache。客户 命名空间。
Java的: com.alachisoft.ncache.client 命名空间。
Python: ncache.客户端 类。
节点.js: 缓存 类。