数据库和缓存齐头并进,长期以来一直密切相关。 作为中间层的缓存通过将数据保存在缓存中以加快处理速度,从而减轻数据库的压力。 NCache 是适用于 .NET 和 Java 应用程序的内存中分布式缓存解决方案。 多年, NCache 一直通过减少数据库负载来提供高性能和可扩展性。 它还确保您的关键任务应用程序的响应时间更快。
JSON 一直是现代应用程序之间最著名的数据交换格式之一。 由于其模式化的性质,它是一种独立于平台、轻量级且非常易读和易于理解的语言。 它速度极快,可以被现代编程语言使用,并且被所有主要的 JavaScript 框架支持。
把上面提到的两件事分开讲,我们就进入了我们这里讨论的话题,就是对 JSON 的支持。 NCache. NCache 具有极强的可扩展性和分布式支持缓存中的 JSON 数据存储以及数据的 JSON 序列化。 您可以将 JSON 文档存储在缓存中,以便快速轻松地进行操作。 NCache 为 JSON 数据存储提供自己的类,基于其属性派生自抽象类 JsonValueBase。 这些类是:
- JSON值
- 对象
- JSON数组
- 杰森空
.NET 和 Java 之间的快速 JSON 示例
首先,让我们看一个快速示例,展示 JSON 数据在 NCache. 您可以使用 .NET 应用程序在缓存中添加 JSON 文档,并且此数据可以由 Java 应用程序检索,因为 JSON 与平台无关。
下面给出的示例显示了使用 .NET 在缓存中添加 JSON 文档作为 JsonObject。
1 2 3 4 5 6 7 8 9 10 |
// Pre-Condition: Cache is JSON serialized // The data provided in the string is a jsonObject string jsonString = "{ 'ProductID' : 1001, 'ProductName' : 'Chai', 'Category': 'Beverages', 'UnitPrice' : 500, 'UnitsInStock' : 2000 }"; // Populate an instance of the JsonObject by passing the string var jsonProduct = new JsonObject(jsonString); // Insert the jsonObject in the cache with a key string key = “Product:ProductID:1001”; cache.Insert(key, jsonProduct); |
可以使用 Java 应用程序检索上面添加的 JSON 文档。 下面给出的代码显示了通过 Java 检索相同的 JSON 文档。 它进一步删除了 JsonObject 的属性,然后将 JsonObject 重新插入缓存中。
1 2 3 4 5 6 7 |
JsonObject jsonProduct = cache.get(key, JsonObject.class); // Remove the attribute UnitPrice from the JsonObject boolean result = jsonProduct2.removeAttribute("UnitPrice"); // Re-insert the item in the cache with the removed attribute cache.insert(key, jsonProduct); |
JSON支持 NCache
NCache 是主要的缓存解决方案,允许在缓存中添加自定义数据并以 JSON 格式检索它。 同样,您可以在缓存中添加 JSON 数据并将其作为自定义数据对象检索。
如前面提到的, NCache 提供自己的从 JsonValueBase 派生的类,然后这些类进一步用于在缓存中添加不同格式的 JSON 数据。 我们将一一仔细研究这些类:
1.json值
JSON值 表示 JSON 约定中的原始数据类型。 您可以添加任何原始类型的值,包括其他类型的值,例如 .NET 中的字符串、日期时间或小数,作为缓存中的 JsonValue。
1 2 3 4 5 |
string keyInt = $"Product:{product.ProductID}"; JsonValue jsonValue = 1001; // Insert a JsonValue in the cache with the key cache.Insert(keyInt,jsonValue); |
2.JsonObject
对象 是无序名称/值对的容器,其中名称或键是属性的名称,值可以是任何原始数据类型。 您可以解析包含 JSON 数据的整个字符串并从中填充 JsonObject。 同样,自定义对象也可以作为 JsonObject 检索,反之亦然。 下面的示例显示了创建具有属性及其值的 JsonObject,然后将其添加到缓存中。 然后显示对 JsonObject 的进一步操作,例如删除特定属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
string key = "jsonKey"; // Create a new JsonObject with the attributes and insert in the cache JsonObject jsonProduct = new JsonObject(); jsonProduct.AddAttribute("ProductID", 1001); jsonProduct.AddAttribute("ProductName", (JsonValue)"Chai"); cache.Insert(key, jsonProduct); // Retrieve the jsonObject previously added the attribute from jsonObject var jsonProduct2 = cache.Get<JsonObject>(key); // Remove the attribute from jsonObject without changing the entire object bool result = jsonProduct2.RemoveAttribute("UnitPrice"); cache.Insert(key, jsonProduct2); |
3.JsonArray
JSON数组 是项目的集合,表示 JArray NCache的域。 就像 JsonObject 一样,您可以指定从 JsonValueBase 派生的任何值或对象。 您可以在任何指定索引处添加、检索甚至删除 JsonArray 中的值。
1 2 3 4 |
string jsonProductIds = " ['ProductIDs', { 'ProductID' : 1001 }, { 'ProductID' : 1002 }, { 'ProductID' : 1003 } ]"; // Create a JsonArray with from the JSON document provided var jsonArray = new JsonArray(jsonProductIds); |
4.JsonNull
此类表示 JSON 标准中的空值。 给定的示例显示 JsonObject 的属性的空值。 在进一步检索具有 null 值的属性时,它会引发异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Create a new JsonObject with attributes and insert in the cache var jsonProduct = new JsonObject(); jsonProduct.AddAttribute("ProductID", product.ProductID); jsonProduct.AddAttribute("ProductName", (JsonValue)product.ProductName); jsonObject.AddAttribute("Category", null); cache.Insert(key, jsonProduct); // Retrieve the same jsonObject previously created var jsonProduct2 = cache.Get<JsonObject>(key); var productCategory = jsonObject.GetAttributeValue("Cateogry"); // Check if the attribute value is null if(category.DataType == Runtime.Enum.JsonDataType.Null) { // Throw category null Exception } |
NCache 更多信息 将数据缓存为 JSON 文档 JSON 序列化
索引 JSON 文档 NCache
如果您的缓存是 JSON 存储,您可以使用以下任一方法为您的数据建立索引:
- 组别:缓存中的项目之间有关系或属于相似类别可以分组 NCache 运用 NCache “群组”功能。 使用 组别,您可以对数据进行逻辑分区以提高效率。
- 标签:标签是基于字符串的标识符,与您的数据相关联。 使用 标签,您可以将关键字与缓存项相关联,这有助于使用这些关键字对数据执行各种缓存操作。
- 命名标签: 命名标签 是由提供的功能 NCache 这是标签的增强形式。 它们在运行时以键值对的形式将特定信息与项目相关联。
JSON 文档使用这些标识符添加到缓存中,并且 NCache 根据这些标识符索引数据。 您可以稍后使用由 NCache 对其执行操作。 下面的示例显示了在缓存中添加一个 JSON 文档,并带有与之关联的命名标签。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
string jsonString = "{'ProductID' : 1001, 'ProductName' : 'Chai', 'Category': 'Beverages', 'UnitPrice' : 500, 'UnitsInStock' : 2000 }"; // Populate an instance of the JsonObject by passing the string var jsonProduct = new JsonObject(jsonString); string key = $"Products:{product.ProductID}"; var item = new CacheItem(jsonProduct); var productNamedTag = new NamedTagsDictionary(); // Add NamedTag 'Discount' to the dictionary with the value productNamedTag.Add("Discount", 0.5); item.NamedTags = productNamedTag; // Add CacheItem with the NamedTag associated cache.Insert(key, item); |
使用 SQL 搜索 JSON 文档 NCache
NCache 提供类似 SQL 的查询,用于检索和删除数据。 您可以使用对索引数据的查询来获取任何格式的 JSON 数据 NCache.
给定的示例显示了查询上面示例中索引的数据,这是一个添加了命名标签的 JSON 文档。 该查询从缓存中检索折扣值为 0.5 的所有 JsonObject。
1 2 3 4 5 6 7 |
// Create a query for fetching item with NamedTag string query = "Select * FROM Alachisoft.NCache.Runtime.JSON.JsonArray 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 |
持久化 JSON 文档
NCache 具有许多用于对主数据源执行直接操作的服务器端功能。 这些功能提供了从主数据源读取数据或向主数据源写入数据以及在缓存中预加载数据的灵活性。 这些功能之一称为 支持来源 为您提供以下便利:
NCache 调用由您编写的提供程序,使用您的自定义逻辑从主数据源加载数据或将数据写入主数据源。 然后提供者 部署 运用 NCache Web Manager 使用 Read-Through/Write-Through。
对于基于 JSON 的主数据源(例如 Cosmos DB),在缓存中使用 JSON 数据还有一个额外的好处。 与 .NET 不同,对于 JSON,您可以为 JSON 中的所有应用程序编写一个通用提供程序,以部署在您的缓存上,以直接从数据库加载数据。 您可以通过使用这些通用提供程序来实现更高的持久性,因为 JSON 是独立于平台的。 .NET 要求您为每个应用程序编写不同的提供程序,因为需要对数据进行序列化然后反序列化。 当您的缓存和数据库都是 JSON 存储时,这些功能为您提供了非常高级的持久性,从而提高了应用程序性能。 下面给出的代码显示了直写如何与 JSON 一起工作。
1 2 3 4 5 6 7 8 9 10 11 12 |
string jsonString = "{ 'ProductID' : 1001, 'ProductName' : 'Chai', 'Category': 'Beverages', 'UnitPrice' : 500, 'UnitsInStock' : 2000 }"; // Populate an instance of the JsonObject by passing the string var jsonProduct = new JsonObject(jsonString); string key = $"Products:{product.ProductID}"; // Enable write through for the cacheItem created var writeThruOptions = new WriteThruOptions(); writeThruOptions.Mode = WriteMode.WriteThru; // Add the item in the cache with WriteThru enabled CacheItemVersion itemVersion = cache.Insert(key, cacheItem, writeThruOptions); |
您可以获得完整的指南 NCache的服务器端功能.
.NET 和 Java 对象的 JSON 序列化
In NCache,所有添加到缓存中的数据都被序列化了。 序列化可以是二进制或 JSON。 它是一种独立于平台的语言,具有广泛支持的优势。 几乎所有编程语言都支持在其标准库中序列化和反序列化 JSON。 NCache 获取您的自定义对象,JSON 通过将其转换为 JSON 对其进行序列化,然后将其反序列化回自定义对象。
JSON 序列化 您的数据在 NCache 它使用 JSON.NET 为您提供了很大的灵活性,因此它具有多种语言的可移植性,并根据用户要求反序列化数据。 它不需要更改代码,并让您可以轻松指定不可序列化的属性。
查看下面的示例,了解 .NET 中的数据,它是 JSON 的等价物。
。NET | JSON |
Product product = new Product(); |
{ |
数据共享是 JSON 序列化数据的核心优势之一。 JSON 相当简单、易于理解并且没有复杂的库,让您可以提供需要反序列化的数据类型。 考虑到反序列化时的属性相同,它具有非常灵活的序列化模式。 不仅如此,它甚至可以让您映射使用 JSON 序列化的同一类的两个不同版本。 您可以 JSON 序列化来自 .NET 类的数据,Java 应用程序可以使用 name 属性访问序列化数据。 图 1 说明了如何使用缓存中的 JSON 序列化在 .NET 和 Java 应用程序之间共享数据。
结论
这将我们带到了博客的结尾,这表明了 NCache 作为 JSON 存储。 NCache 是一个内存中的分布式缓存,可让您在缓存中使用 JSON 文档。 它允许您将持久数据保留在缓存中,并具有 JSON 序列化和 .NET 和 Java 应用程序之间的数据共享的额外好处。