高效的数据管理需要能够根据特定属性快速搜索应用程序数据。 随着数据量的增长,这种需求变得更加重要。 通常,数据库已经具有便于数据分类的字段,并且这些字段可以用作缓存数据时的索引。 但是,在某些情况下,您可能希望根据缓存中不存在的属性(例如标签或其他自定义属性)对数据进行分组。
NCache,您不仅可以根据自定义属性对应用程序数据进行分类,还可以根据这些属性进行搜索。 除了传统的索引, NCache 提供使用分类数据的选项 组别, 标签及 命名标签,允许在数据分类和检索方面具有更大的灵活性。
通过组缓存数据分类
对于已缓存的项目集合,可以通过 Group 名称来标识,您可以创建一个 Group。 此名称可以帮助检索属于组的任何项目。 例如,您可以创建一个组,其中包含与特定客户或特定产品类别关联的所有产品。 简单地调用这个名字会导致 NCache 返回与其关联的所有缓存项。 请注意,一个群组只能关联一个项目。 下面给出的示例显示了来自不同地区(在本例中为东海岸)的客户如何在在线商店中分组。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Pre-conditions: Cache is already connected. // Create a unique cache key for this customer. string customerKey = $"Customer:ALFKI"; // Get customer from cache Customer customer = cache.Get<Customer>(customerKey); // Get customer from database if not found in cache if (customer == null) { customer = FetchCustomerFromDB("ALFKI"); // Create a new CacheItem var cacheItem = new CacheItem(customer); // Specify the group cacheItem.Group = "East Coast Customers"; // Add customer object to cache with group cache.Add(customerKey, cacheItem); } |
您可以使用 SQL 查询来使用关键字 $Group$ 搜索组中包含的缓存对象。 下面的代码示例演示了如何使用 SQL 查询查看来自特定区域(在本例中为西海岸)的所有客户。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// Pre-conditions: Items with groups exist in the cache // Custom class is query indexed through the NCache Manager or config.ncconf // Create an SQL Query with the specified criteria // Make sure to use the Fully Qualified Name (FQN) string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Group$ = ? "; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Providing parameters for query queryCommand.Parameters.Add("$Group$", "West Coast Customers"); // Executing the Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string>("CustomerID"); string customerName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{customerName}' having ID '{customerID}' belongs to West Coast."); } } else { Console.WriteLine($"No customers from West Coast found"); } |
除了提高效率之外,组还使用户可以轻松地 取回 or 删除 基于这些逻辑类别的数据,可提高应用程序的可扩展性。 此外,Groups 可以缓存符合特定搜索条件的常用数据,避免重复运行数据库搜索的成本。
通过标签对缓存数据进行分类
标签 in NCache 提供一种使用多个标签对缓存项进行分类的方法。 缓存的对象可以分配一个或多个标签,这些标签可用于检索项目。 例如,您可以根据地区、语言或产品特性对项目进行分类。 调用标签的名称将导致 NCache 返回所有具有该标签并被缓存的项目。 标签使您可以将元数据链接到缓存的对象,从而简化搜索和数据操作。 以下示例显示了如何将已从东海岸地区登录的客户添加到缓存中。
1 2 3 4 5 6 7 8 9 10 11 |
// Create an array of tags assigned to customer Tag[] tags = new Tag[2]; tags[0] = new Tag("East Coast Customers"); tags[1] = new Tag("VIP Customers"); // Setting the tag property of the cacheItem cacheItem.Tags = tags; cache.Add(customerKey, cacheItem); // CacheItem added successfully |
同样,您可以查询具有特定标签关键字 $Tag$ 的项目。 下面示例中的 SQL 查询返回链接到标签的每个项目 贵宾客户。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Create a query for search string query = "Select CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Tag$ = ?"; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); queryCommand.Parameters.Add("$Tag$", "VIP Customers"); // Executing Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string> ("CustomerID"); string contactName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{contactName}' having ID '{customerID}' is a VIP Customer."); } } else { Console.WriteLine($"No VIP Customers found"); } |
使用标签为用户提供与每种特定类型的高度相关性。 要了解 Tags 提供的其他 CRUD 操作,请参阅 NCache 文件.
通过命名标签对缓存数据进行分类
缓存的对象可以通过列表被赋予多个命名标签,然后可以使用这些命名标签来检索项目。 每个命名标签都有两个参数:一个是表示标签名称的字符串的键,另一个是任何原始类型的值。 例如,您可以根据商品的价格范围或可用性将命名标签分配给商品。 调用标签名称和值将导致 NCache 当您需要获取具有该标签的所有项目时,返回所有具有指定命名标签的存储项目。
类似于组和标签,您可以 检索项目 使用对象查询从带有命名标签的缓存中获取。 当某些命名标签已经添加到缓存中的项目时, NCache 为用户提供更新这些命名标签的机会。 使用 NCache的自定义类,即CacheItem,可以向缓存中添加数据。 作为财产的 命名标签 类,CacheItem 还使您能够指定与项目相关的其他规范。 在下面给出的示例中,具有 VIP 会员资格的客户(VIP 会员可享受 10% 的折扣),通过将命名标签分配为 CacheItem 的属性来设置命名标签。
1 2 3 4 5 6 7 8 9 |
// Creating a Named Tags Dictionary var namedTags = new NamedTagsDictionary(); // Adding Named Tags to the Dictionary where Keys are the names of the tags as string type and Values are of primitive type namedTags.Add("VIP_Membership_Discount", 0.10); // Setting the named tag property of the cacheItem cacheItem.NamedTags = namedTags; cache.Add(customerKey, cacheItem); |
当一个项目是 使用命名标签添加到缓存,您可以通过 SQL 查询轻松检索它。 在下面的示例中,用户希望查看所有拥有 12% VIP 会员折扣的客户。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Create an SQL Query with the specified criteria string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE VIP_Membership_Discount = 0.12 "; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Executing the Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if the result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string>("CustomerID"); string customerName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{customerName}' with ID '{customerID}' has VIP membership discount."); } } else { Console.WriteLine($"No VIP members found"); } |
结论
明确地 NCache 提供各种强大的功能,可通过组、标签和命名标签进行有效的数据缓存和搜索,根据特定属性对缓存数据进行分类并使用 NCache API 或类似 SQL 的查询。 这些特性使它们成为更有吸引力的缓存选项,因为它们可以加快数据访问速度、减少数据库负载并提高应用程序性能。 利用由 NCache 立即开始免费试用!