配置查询索引
NCache 查询索引将分布式键值存储转换为可搜索的格式。通过在类属性上定义显式索引, NCache 它无需进行昂贵的全缓存扫描,即可实现类似 SQL 的查询。这对于需要毫秒级延迟进行复杂搜索的大规模集群至关重要。
重要提示
对于 。NET在部署你的 。dll文件 文件,请确保您已安装适当的 .NET 版本,如 NCache 安装指南.
定义查询索引
您需要在缓存上定义查询索引才能对它们执行操作。 这可以通过以下方式完成:
使用 NCache 管理中心
启动 NCache 管理中心通过浏览 http://localhost:8251 or
<server-ip>:8251.在左侧导航栏中,单击 集群缓存 or 本地缓存,基于将添加查询索引的缓存。 针对缓存名称,单击 查看详情. 确保缓存已停止。

这将打开缓存的详细配置页面。 前往 高级设置 标签并点击 查询索引 在左侧栏中。
这打开了 查询索引 页面,点击 添加.

在下一页中,单击以选择包含要存储在缓存中以供查询的类的程序集。 浏览 按钮。
在 浏览过的程序集 框,显示所有序列化的类。 检查您要索引的那些,然后单击 添加选定的类.
注意:
此 NCache 管理中心显示所选类的所有属性和特性。

- 添加的类及其属性将显示在 所选课程列表. 如果您不想为类属性编制索引,可以取消选中它们。 您还可以通过单击删除选定的类 删除选定的类.

- 添加的查询索引将显示在 查询索引 页。 点击 保存更改 将此配置应用于缓存。
使用PowerShell
- 此 添加查询索引 该 cmdlet 允许用户为要添加到服务器 20.200.20.39 缓存中的对象添加查询索引。以下命令用于添加索引。 Id, 姓名, 类别, 单位可用和 单价 类的属性 Alachisoft.NCache.样本.数据.产品 在查询索引中 演示缓存.
Add-QueryIndex -CacheName demoCache -AssemblyPath C:\Data.dll -Type Data.Customer `
-Attributes "Id'$'Name'$'Category'$'UnitsAvailable'$'UnitPrice" `
-Server 20.200.20.39
- 此命令使用安全机制在缓存中添加 CustomerID 和 FirstName 属性的查询索引。 证书.
Add-QueryIndex -CacheName demoCache -AssemblyPath C:\Data.dll `
-Class Data.Customer -Attributes CustomerID$FirstName `
-Credentials(Get-Credential john_smith)
手动编辑 NCache 配置
您还可以手动编辑 NCache 配置来配置查询索引,如下所述。
重要提示
确保在进行任何配置更改之前停止缓存。
- 通过指定查询索引 配置文件 通过添加
<query-indexes>和<query-class>标签下的<cache-settings>标签。 下面的例子添加了两个类的属性, 产品 和 客户 作为索引:
<cache-settings ...>
<query-indexes>
<query-class id="Alachisoft.NCache.Sample.Data.Customer" name="Alachisoft.NCache.Sample.Data.Customer">
<query-attributes id="CustomerID" name="CustomerID" data-type="System.String"/>
<query-attributes id="ContactName" name="ContactName" data-type="System.String"/>
<query-attributes id="CompanyName" name="CompanyName" data-type="System.String"/>
<query-attributes id="Fax" name="Fax" data-type="System.String"/>
</query-class>
<query-class id="Alachisoft.NCache.Sample.Data.Product" name="Alachisoft.NCache.Sample.Data.Product">
<query-attributes id="UnitPrice" name="UnitPrice" data-type="System.Decimal"/>
<query-attributes id="UnitsAvailable" name="UnitsAvailable" data-type="System.Int32"/>
<query-attributes id="Name" name="Name" data-type="System.String"/>
<query-attributes id="Category" name="Category" data-type="System.String"/>
<query-attributes id="Id" name="Id" data-type="System.Int32"/>
</query-class>
</query-indexes>
...
</cache-settings>
- 修改完成后重启 NCache 服务,并启动您的缓存。
删除查询索引
可以按照下面的说明删除预定义的查询索引。
使用 NCache 管理中心
启动 NCache 管理中心通过浏览 http://localhost:8251 or
<server-ip>:8251.在左侧导航栏中,单击 集群缓存 or 本地缓存 要从中删除已配置的查询索引。 针对缓存名称,单击 查看详情.

这将打开缓存的详细配置页面。 前往 高级设置 标签并点击 查询索引 在左侧栏中。
这打开了 查询索引 显示查询索引的现有类的页面。
选择要删除的类或属性,然后单击 删除.

- 点击 保存更改 将此配置应用于缓存。
使用PowerShell
- 此 删除查询索引 cmdlet 允许用户删除要添加到缓存的对象的预定义查询索引。以下命令将删除类的查询索引 客户 从缓存中 演示缓存.
Remove-QueryIndex -CacheName demoCache -Class Alachisoft.NCache.Sample.Data.Customer
- 此命令删除服务器 20.200.20.11 上客户类的查询索引。
Remove-QueryIndex -CacheName demoCache -Class Data.Customer/
-AttributesList "CustomerID'$'FirstName" -Server 20.200.20.11
- 此命令使用安全机制删除 Customer 类的查询索引。 证书.
Remove-QueryIndex -CacheName demoCache -Class Data.Customer -Credentials (Get-Credential john_smith)