检索缓存数据
NCache 它采用线性可扩展的高性能键值存储架构,从而能够从分布式环境中实现低延迟数据检索。通过使用唯一键,您可以从缓存中检索自定义对象、CacheItem 或批量数据项。 NCache 提供基于指定键的数据检索优化 API,以最大限度地减少网络开销并最大限度地提高应用程序吞吐量。
硬件需求
使用前 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 官方网站.
如何从缓存中检索对象
您可以使用各种重载从缓存数据中获取自定义对象 Get 该方法通过指定缓存项的键来实现,从而在分布式应用程序中实现基于键的快速缓存数据检索。 Get 当只需要获取数据值时,该方法是最直接的检索数据的方式,对于简单的读取操作,其计算开销最小。
以下示例获取现有的项目 客户 具有指定键的类。.NET 示例使用 Get<Customer> 返回缓存值 Customer 目的。
try
{
// Precondition: Cache is already connected
string customerKey = "Customer:ALFKI";
// Retrieve the Customer object
Customer customer = cache.Get<Customer>(customerKey);
Console.WriteLine($"Customer: {customer.ContactName}, Address : {customer.Address}");
}
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";
// Retrieve the Customer object
Customer customer = cache.get(customerKey, Customer.class);
System.out.println("Customer with key " + customerKey + " has value " + customer);
}
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
# 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)
# Insert item into cache
cache.insert(key, cache_item)
# Retrieve the Customer object
retrieved_item = cache.get(key, Product)
# Perform your logic
print("Item received")
except Exception as error:
print("An error occurred:", str(error))
try
{
// Precondition: Cache is already connected
// This is an async method
// 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);
// Retrieve the Customer object
var retrievedItem = await this.cache.get(key, Product);
}
catch(error)
{
// Handle errors
}
// Using NCache 4.9.1
try
{
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
Customer customer = FetchCustomerFromDB(customerKey);
// Retrieve the Customer object
object result = cache.Get(customerKey);
Console.WriteLine($"Customer: {customer.ContactName}, Address : {customer.Address}");
}
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 允许检索现有的 CacheItem 使用专用从缓存中 GetCacheItem API。 这会返回一个 CacheItem 与指定的键相关联。 它的 Value 属性封装了数据。使用 GetCacheItem 当您的应用程序逻辑需要将元数据(例如标签、过期时间或版本控制)与实际缓存值一起使用时,建议使用此功能。
以下示例检索现有的 CacheItem 使用指定的键并检查结果是否为 客户 type 和 type 相应地转换它。
// Precondition: Cache is already connected
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}");
}
// Precondition: Cache is already connected
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());
}
# Precondition: Cache is already connected
# 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")
// Precondition: Cache is already connected
// 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
}
// Using NCache 4.9.1
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";
// Retrieve the CacheItem
CacheItem retrievedCacheItem = cache.GetCacheItem(customerKey);
if (retrievedCacheItem != null)
{
Customer customer = (Customer)retrievedCacheItem.Value;
Console.WriteLine($"Customer: {customer.ContactName}, Address : {customer.Address}");
}
更多资讯
NCache 提供了基本操作的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache。客户 命名空间。
Java的: com.alachisoft.ncache.client 命名空间。
Python: ncache.客户端 类。
节点.js: 缓存 类。