如今,每个企业都需要数据来管理其运营。 存储组织良好的数据集合的最佳解决方案是数据库。 同样,用于在数据库中存储和检索数据的最常用的计算机语言是 SQL(结构化查询语言)。 在如此快节奏的现代世界中,能够快速有效地定位信息至关重要。
NCache 通过在主键用于查找数据的位置指定相关键来满足此需求。 此外, NCache 还雇用 索引 在许多模块中以提高缓存的搜索性能并对其进行跟踪。 与现实世界的情况一样,您需要灵活的搜索功能,以便能够使用主键以外的标准来查找信息。
然而,所有可搜索的属性都应该被索引以供搜索。 存储索引所需的额外计算存储以及更新发生所需的时间与信息检索期间节省的时间相平衡。
定义索引的方法
要定义索引, NCache 提供 静止 和 动态 索引。 在这两种情况下,都必须预定义自定义类的查询索引,以便在缓存中搜索该类的项目。 通过这样做,缓存以一种易于查询的方式存储信息。 您可以通过指定静态索引 配置 或通过定义 以编程方式索引. 运行时索引在使用前不需要任何配置。
通过编程方式查询索引 NCache
NCache 为用户提供使用多种方法构建索引的多功能性。 您可以根据需要为特定字段或整个类生成索引。 NCache 使你能够 以编程方式查询索引 使用以下注释:
- 可索引查询
- 查询索引
- 查询索引[“索引名称”]
- 非查询索引
为类配置查询索引
使用属性 QueryIndexable,您可以索引您的类。 它是在类级别定义的,表示可以对整个类进行索引。 当一个类被标记为 QueryIndexable 时,所有公共属性和字段都将被自动索引。 如果你的类中的私有字段需要被索引,你必须用 QueryIndexed 注释明确地指出它。
以下示例演示了对名为的类的字段进行索引 产品 使用 QueryIndexable 属性。 这将索引 ID、名称、数量、制造商名称、 和 单价.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[QueryIndexable] class Product { public int ID { get; set; } public string Name { get; set; } public string Quantity { get; set; } public string ManufacturerName { get; set; } public Decimal UnitPrice { get; set; } } |
被索引的类的对象被添加到缓存中。 下面的代码示例显示了如何获取 产品 通过查询上面提到的类。
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 |
// Pre-conditions: Cache is already connected // Products are already present in the cache // Provide Fully Qualified Name (FQN) of your Product class // Create a query string query = "SELECT $Value$ FROM FQN.Product WHERE Quantity < ?"; // Use QueryCommand for query execution QueryCommand queryCommand = new QueryCommand(query); // Providing parameters for query queryCommand.Parameters.Add("Quantity", 500); // Executing QueryCommand through ICacheReader ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Checking if the result set is not empty if (reader.FieldCount > 0) { // Getting all Products with Quantity less then 500 while (reader.Read()) { Product product = reader.GetValue<Product>(1); // Perform required operations } } else { // Null query result set retrieved } |
只有当您希望查询索引所有或大部分类的属性或字段时,您才应使用此策略。 否则,不鼓励这样做,因为它可能会索引不必要的特征,最终会消耗额外的内存。
为特定属性配置查询索引
当你只需要索引一个类的几个属性或字段时,使用 QueryIndexed 属性 因为它节省了性能和内存成本。 为了指示标记的属性或字段可以被索引,它在原始属性或字段级别定义。 带有注释的字段或属性会自动建立索引。
班级 飞行 下面的演示展示了如何使用 QueryIndexed 索引所需的属性而不索引整个类。 仅针对字段 ID 和 TotalPassengers, NCache 会自动创建索引。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Flight { [QueryIndexed] public int ID { get; set; } [QueryIndexed] public int TotalPassangers { get; set; } public string FlightType; private string DepartAirport { get; set; } public AirLine Airline { get; set; } } |
当你的类中只有少量的属性或字段需要被索引时,你可以使用该方法来构造索引。 它使您可以选择所需的属性或字段并仅对它们进行索引。 此注释使您免于查询整个类而可能出现的内存问题,而您只需要查询其中的某些属性。
用户提供的查询索引配置
由于使用 QueryIndexed 属性进行选择性索引会导致此默认行为 - 索引名称与字段名称相同。 通过QueryIndexed(“indexName”)注解, NCache 确实允许您为将被索引的字段或属性设置唯一的名称。 如果是,则使用构造函数中用户指定的名称创建索引。 在使用可能不同的字段名称以多种语言编写应用程序代码时,这会很有帮助。
在课堂里 员工, 田野 EmployeeId、DateOfBirth, 和 薪水 将被索引,因为它们标有 QueryIndexed 属性。 但是,要创建包含字段的索引, EmployeeId 和 DateOfBirth, 构造函数中用户指定的名称 “ID” 和 “出生日期” 将被使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Employee { public string Name { get; set; } [QueryIndexed("ID")] public int EmployeeId { get; set; } [QueryIndexed("DOB")] public string DateOfBirth { get; set; } [QueryIndexed] public double Salary { get; set; } public string Position { get; set; } } |
从索引中排除字段
当您的类被索引时,NonQueryIndexed 属性可用于停止可选属性或字段的索引。 当一个类声明为 QueryIndexable 时,属性 非查询索引 在原始属性或字段级别定义,以指定不应为属性或字段编制索引。 公共字段和属性都将被自动索引。 NonQueryIndexed 属性指示不应为哪些字段编制索引。
以下示例演示如何显式声明字段 顾客姓名 作为 NonQueryIndexed 而 订购产品 类被索引。 这将忽略注释字段的索引。
1 2 3 4 5 6 7 8 9 10 11 12 |
[QueryIndexable] class Order { public int OrderId { get; set; } public DateTime OrderDate { get; set; } public string OrderDestination { get; set; } [NonQueryIndexed] public string CustomerName { get; set; } } |
场 顾客姓名 不会添加到缓存中。 查询后,您将获得以下值 顾客姓名 为 null,而您必须显式检索所有其他字段。
当除少数属性之外的所有属性都需要索引时,您可以将 QueryIndexable 与 NonQueryIndexed 结合使用。 当您拥有大量数据并且只想排除一小部分以使性能保持不变时,这可能很有用。
结论
NCache 提供了一种快速有效的机制,使您可以根据既定条件查询索引。 有了这个特性,您就不必担心查询索引整个类可能导致的内存问题,因为您只需要索引其中的几个字段。 无疑, NCache不同的查询索引注释是一个真正的祝福。 你可以看看其他的 NCache 功能 从我们的网站。