NCache 提供 JSON 支持,这是一种结构化的、人类可读的、轻量级的语法,用于解析和数据共享。提供的主要便利是 NCache 通过 Cache Data as JSON 对您来说,可以灵活地将缓存中任何自定义类的数据作为 JSON 检索。 此外,数据可以作为 JSON 添加并作为自定义类进行检索和解析。
NCache 通过以下方式支持 JSON:
NCache 允许您添加和检索 JSON 格式的缓存数据。 NCache 支持多种 JSON 结构以高效地插入和获取 JSON 数据。 这些结构是:
这两种结构都派生自一个抽象类,名称为 Json值库。以下段落解释了这些类及其用法 NCache 存储和检索数据。
对象 代表 对象 在 JSON 标准中 NCache 域。 对象 保存无序的名称-值对,并根据唯一键添加到缓存中,该唯一键稍后用于执行缓存操作。要将缓存数据检索为 JSON, NCache 允许将任何自定义类作为 对象.
你可以插入一个 对象 通过以下方式到缓存。 此示例显示如何填充 对象 使用序列化字符串并将其添加到缓存中。
var product = FetchProductFromDB("1001");
string jsonString = @$"{{
""ProductID"" : ""{product.ProductID}"",
""ProductName"" : ""{product.ProductName}"",
""Category"" : ""{product.Category}"",
""UnitsAvailable"" : ""{product.UnitsAvailable}""
}}";
JsonObject jsonObject = new JsonObject(jsonString, "Alachisoft.NCache.Sample.Product");
string key = "Product:" + product.ProductID;
// Inserting object in cache
cache.Insert(key, jsonObject);
Product product = Product.fetchProductFromDB("Product:1001");
String jsonString = "{\"ProductID\":\"" + product.getProductID() + "\"," +
"\"ProductName\":\"" + product.getProductName() + "\"," +
"\"Category\":\"" + product.getCategory() + "\"," +
"\"UnitsAvailable\":" + product.getUnitsAvailable() +
"}";
JsonObject jsonObject = new JsonObject(jsonString, "com.alachisoft.ncache.sample.Product");
String key = "Product:" + product.getProductID();
// Inserting object in Cache
cache.insert(key, jsonObject);
NCache 允许您以 a 的形式检索缓存的自定义对象 对象. 这将返回一个 JSON 字符串,其中包含您的自定义类的数据。 NCache 还允许您缓存一个 对象 并使用相同的 Get API 将其作为自定义对象获取。以下是如何将自定义对象作为 JSON 对象检索的示例:
var jsonObject = cache.Get<JsonObject>(key);
var jsonObject = cache.get(key, JsonObject.class);
JSON数组 in NCache 是 数组 在 JSON 标准中。 JSON数组 是一个有序的值列表。 这些值可以是字符串、数字、布尔值、对象或其他数组。
您可以添加 对象 甚至另一个 JSON数组 在 JSON数组. NCache 使用 indexer 属性在 JSON数组. 您可以创建一个 JSON数组 使用 对象 以下面的方式:
var jsonArray = new JsonArray();
...
var jsonObject1 = new JsonObject(jsonString1 );
jsonArray.Add(jsonObject1);
var jsonObject2 = new JsonObject(jsonString2);
jsonArray.Add(jsonObject2);
string key = “JsonArrray:1001”;
cache.Insert(key, jsonArray);
var jsonArray = new JsonArray();
//...
var jsonObject1 = new JsonObject(jsonString1, "com.alachisoft.ncache.sample.Product");
jsonArray.add(jsonObject1);
var jsonObject2 = new JsonObject(jsonString2, "com.alachisoft.ncache.sample.Product");
jsonArray.add(jsonObject2);
String key = "JsonArray:1001";
// Inserting JsonArray in Cache
cache.insert(key, jsonArray);
In NCache,您还可以在指定索引处添加 JSON 数据 JSON数组. 如果指定索引处已存在项目,则将其覆盖。 如果指定的索引不存在,则将该项目添加到该索引处,并将最后填充的索引和新索引之间的所有值设置为 杰森空 (在 JSON 标准中为空)。
NCache 允许您获取整个 JSON数组 从缓存中或从特定索引中检索值 JSON数组. 检索到后,您可以枚举它或对数据执行基于索引的操作。 这是你如何获取一个 JSON数组 或缓存中的特定值:
// Retrieve JsonArray from cache
string key = "JsonArray:1001";
JsonArray jsonArray = cache.Get<JsonArray>(key)
// Retrieve the value of the specified index
JsonValueBase value = jsonArray[2];
// Retrieve JSONArray from cache
String key = "JsonArray:1001";
JsonArray jsonArray = cache.get(key, JsonArray.class);
// Retrieve the value of the specified index
JsonValueBase value = jsonArray.getItem(2);
NCache 还允许您使用缓存上的 JsonEnumerator 通过以下方式获取所有 JSON 对象和 JSON 数组:
var enumerator = (IDictionaryEnumerator)_cache.GetJsonEnumerator();
while (enumerator.MoveNext())
{
DictionaryEntry entry = (DictionaryEntry)enumerator.Current;
JsonValueBase valueBase = (JsonValueBase)entry.Value;
//...
}
var enumerator = cache.asJsonIterator();
while (enumerator.hasNext())
{
Entry entry = (Entry) enumerator.next();
JsonValueBase valueBase = (JsonValueBase) entry.getValue();
//...
}
NCache 是一个功能丰富的分布式缓存,它提供了许多功能来帮助您轻松存储和检索数据。 就像原生和自定义对象一样, NCache 对于 JSON 类型的数据也支持此类功能。
NCache 允许您根据 JSON 数据的类型对其进行索引。
Product product = FetchProductFromDB();
string jsonString = @$"{{ ""ProductID"" : ""{product.ProductID}"", ""ProductName"" : ""{product.ProductName}"", ""Price"" : {product.Price} }}";
JsonObject jsonObject = new JsonObject(jsonString, "Alachisoft.NCache.Sample.Product");
string key = "Product:" + product.ProductID;
cache.Insert(key, jsonObject);
Product product = Product.fetchProductFromDB("Product:1001");
String jsonString = "{\"productID\":\"" + product.getProductID() + "\", \"productName\" : \""+ product.getProductName() + "\", \"price\":\"" + product.getPrice() + "\"}";
JsonObject jsonObject = new JsonObject(jsonString,"com.alachisoft.ncache.sample.Product" );
String key = "Product:" +product.getProductID();
cache.insert(key, jsonObject);
根据 JSON 数据的类型对其进行索引后,您可以执行类似 SQL 的查询来从缓存中获取或删除数据。有关如何在查询 JSON 数据时使用所有这些可搜索属性的更多信息,请参阅我们的文档: 查询 JSON 数据.
string query = "Select * FROM Alachisoft.NCache.Runtime.JSON.JsonObject WHERE Discount = ?";
QueryCommand queryCommand = new QueryCommand(query);queryCommand.Parameters.Add("Discount", 0.5);
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
}
String query = "Select * FROM Alachisoft.NCache.Runtime.JSON.JsonObject WHERE Discount = ?";
QueryCommand queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("Discount", 0.5);
var queryResult = cache.getSearchService().executeReader(queryCommand);
if (queryResult.getFieldCount() > 0) {
while (queryResult.read()) {
// Perform operation according to your logic
}
}
else {
// No data
}
在缓存中存储数据时, NCache 允许您确保您正在使用的数据不是过时的。为了确保这一点, NCache 提供了以下功能,使您的缓存和数据库保持同步。
在 JSON 序列化中,对象在序列化时转换为其 JSON 等价物,然后在反序列化时转换回其自定义对象。
NCache 为 .NET 和 Java 对象提供 JSON 序列化支持。
让我们假设您在 .NET 中具有以下产品属性。您可以将此对象作为自定义对象存储在缓存中,如下所示:
Product product = new Product();
product.Name = "Pizza";
product.Expiry = new DateTime(2020, 3, 28);
product.Sizes = new string[] { "Large", "Medium" };
Product product = new Product();
product.setName("Pizza");
product.setExpiry(new Date(2020, 3, 28));
product.setSizes(new String[] { "Large", "Medium" });
或者,使用 JSON 序列化序列化此数据。 当您选择 JSON 序列化时,此数据将被序列化为以下 JSON 字符串:
{
"Name": "Pizza",
"Expiry": "2020-3-28T00:00:00",
"Sizes": ["Large", "Medium"]
}
JSON 序列化具有内存高效、可移植且完全自动化的特点,无需更改代码即可实现。 NCache 还允许您指定要序列化哪些属性以及保留哪些属性。