asg*_*012 1 delphi tstringlist delphi-xe2
我正在实现本地缓存以加速DNS查找(IP->主机名).缓存从CSV文件("1.1.1.1host.example.com")加载到具有两个字段的TStringList中:
TStringList[0] := IPAddress;
TStringList[1] := HostName;
Run Code Online (Sandbox Code Playgroud)
因为我将通过IP查询TStringList,所以我想要对第一个字段进行排序:
TStringList.sorted := True;
Run Code Online (Sandbox Code Playgroud)
这会照顾它,以便我可以更快找到
IPResolved:=TStringList[TStringList.IndexOf('1.1.1.1'),1];
Run Code Online (Sandbox Code Playgroud)
?
谢谢!
TLa*_*ama 12
免责声明:
这不会回答您如何排序字符串列表或如何将数据加载到字符串列表中.它将为您提供一个哈希表,它比为您的目的使用字符串列表更有效(40k名称,值与名称搜索对).
替代方案:
由于你有Delphi XE2,你可以使用泛型TDictionary类.它将包含IP地址作为键,主机名作为值.在下面的代码中显示了如何填充字典以及如何通过给定密钥(IP地址)搜索值(主机名):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Generics.Collections;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
IPList: TDictionary<string, string>;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// create the TDictionary instance
IPList := TDictionary<string, string>.Create;
// here you will read your CSV file and add the items in a loop
// I've used here some of the major IP addresses for Sweden
IPList.Add('77.244.224.0', 'Insat Net AB');
IPList.Add('79.138.128.0', 'Hi3G Access AB');
IPList.Add('62.181.192.0', 'DGC Access AB');
IPList.Add('81.216.128.0', 'TDC Swerige AB');
IPList.Add('80.252.176.0', 'Phonera Networks AB');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// release a dictionary instance
IPList.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
HostName: string;
begin
// and how to search by the IP address and get the host name if found
if IPList.TryGetValue('81.216.128.0', HostName) then
ShowMessage(HostName)
else
ShowMessage('IP address not found!');
end;
end.
Run Code Online (Sandbox Code Playgroud)
延期:
上面的解决方案,您可以简单地扩展到使用一个结构来存储不仅仅是一个主机名,例如也是一个主机位置:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Generics.Collections;
type
TIPData = record
HostName: string;
HostLocation: string;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
IPList: TDictionary<string, TIPData>;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
IPData: TIPData;
begin
IPList := TDictionary<string, TIPData>.Create;
IPData.HostName := 'Broadnet Europe France';
IPData.HostLocation := 'France';
IPList.Add('78.155.128.0', IPData);
IPData.HostName := 'DNA Palvelut Oy';
IPData.HostLocation := 'Finland';
IPList.Add('62.113.160.0', IPData);
IPData.HostName := 'CD-Telematika a.s.';
IPData.HostLocation := 'Czech republic';
IPList.Add('89.203.128.0', IPData);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
IPList.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
IPData: TIPData;
begin
if IPList.TryGetValue('89.203.128.0', IPData) then
ShowMessage('Provider ' + IPData.HostName + ' from ' + IPData.HostLocation)
else
ShowMessage('IP address not found!');
end;
end.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1904 次 |
| 最近记录: |