查询缓存中的 JSON 数据
您可以使用查询索引(正如我们在下面的步骤中详细介绍的那样)或使用与其关联的可搜索属性来查询 Json 数据。可搜索的属性包括:
- 标签
- 命名标签
- 组别
备注
此功能也可用于 NCache Professional.
查询缓存中 JSON 数据的先决条件
第1步:定义用于查询的JSON数据索引
要使用索引查询数据,请按照我们的详细信息对查询对象进行索引 管理员指南 使用 NCache 管理中心或命令行工具。您必须在其中指定类型名称或类(例如 Alachisoft.NCache.Customer),以及您稍后将查询的属性(例如,CompanyName)。
步骤 2:指定 JSON 索引查询的类型信息
您必须提供类型名称(与之前定义的索引中的类型名称相同)才能查询 JsonObject
当将此对象添加到缓存时。 否则,您将无法成功执行任何查询。 您可以在我们的部分中了解如何在代码片段中指定此类信息 使用类型名称将 JsonObject 添加到缓存
步骤 3:使用 JSON 查询索引查询 JsonObjects
如果您已完成前两个步骤,之后您可以根据您需要的属性(例如,CompanyName)进行搜索,如下面的代码片段所示。 有必要添加类型信息以确保从缓存检索到的对象正确反序列化和验证。
// Preconditions:
// (1) Cache is already connected
// (2) Cache is JSON Serialized
// Retrieve the CacheItem using the CompanyName with a query
string query = "SELECT * FROM Alachisoft.NCache.Customer WHERE $CompanyName$ = ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$CompanyName$", "CustomerItem");
var queryResult = cache.SearchService.ExecuteReader(queryCommand);
// QueryResult contains all the keys and metadata of result
if (queryResult.FieldCount > 0)
{
while (queryResult.Read())
{
// Perform operation according to your logic
}
}
else
{
// No data containing the given CompanyName exist
}
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
使用组、标签或命名标签查询 JsonObjects
您可以添加 JsonObject
在您的缓存中作为 CacheItem
. CacheItem
是由提供的自定义类 NCache 可用于将数据添加到缓存中。 CacheItem 允许您向其添加各种属性,例如, 锁定, 依赖, 标签, 命名标签及 组别.
警告
属性不能包含对对象本身的引用。 如果这样做,则会引发异常。
下面的例子添加了一个 CacheItem
到缓存 JsonObject
对客户的 作为价值 CacheItem
. 标签被添加到 CacheItem
然后使用查询命令使用标签获取项目。
Customer customer = FetchCustomerFromDB(1001);
// Create a unique key for the object
string key = $"Customer:{customer.CustomerID}";
// Create a new JSON object and set attributes
// string values need to be added with JsonValue
var jsonCustomer = new JsonObject("Alachisoft.NCache.Customer");
jsonCustomer.AddAttribute("CustomerID", customer.CustomerID);
jsonCustomer.AddAttribute("ContactName", (JsonValue)customer.ContactName);
jsonCustomer.AddAttribute("CompanyName", (JsonValue)customer.CompanyName);
jsonCustomer.AddAttribute("Phone", customer.Phone);
jsonCustomer.AddAttribute("Address", customer.Address);
// Create a new CacheItem
var item = new CacheItem(jsonCustomer);
// Add Tag to the cacheItem
item.Tags = new Tag[] { new Tag("CustomerItem") };
// Add CacheItem in the cache with the value
cache.Insert(key, item);
// Retrieve the CacheItem using the tag with a query
string query = "SELECT * FROM Alachisoft.NCache.Customer WHERE $Tag$ = ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "CustomerItem");
var queryResult = cache.SearchService.ExecuteReader(queryCommand);
// QueryResult contains all the keys and metadata of result
if (queryResult.FieldCount > 0)
{
while (queryResult.Read())
{
// Perform operation according to your logic
}
}
else
{
// No data containing the named tag(s) exist
}
更多资讯
NCache 为缓存数据作为 JSON 提供示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.运行时.JSON 命名空间。
Java的: COM。alachisoft.ncache.runtime.json 命名空间。