2012-08-14 82 views
1

我正在实施本地缓存以加快DNS查找(IP->主机名)。 缓存从CSV文件(“1.1.1.1host.example.com”)成TStringList中装有两个领域:德尔福排序第一个字段有两个字段

TStringList[0] := IPAddress; 
TStringList[1] := HostName; 

因为我将通过IP来查询的TStringList,我想obliously第一现场进行排序:

TStringList.sorted := True; 

这会否照顾它,这样我可以找到

IPResolved:=TStringList[TStringList.IndexOf('1.1.1.1'),1]; 

更快?

谢谢!

+0

很明显,CSV文件在IP和主机名之间有一个空格(被剪切掉) – asg2012 2012-08-14 14:21:27

+0

您使用的是什么版本的Delphi?我要求让您的解决方案更好地优化(泛型)。你能编辑你的问题并添加你的Delphi版本作为下一个标签吗?无论如何,使用'TStringList'将会更好地使用'Name = Value'对,像'StringList [0]:='IPAddress = HostName''这样的模式。 – TLama 2012-08-14 14:30:01

+2

TStringList绝对是错误的数据类型。您需要一个包含两个字符串字段IPAddress和HostName的记录。然后你想用TList 来保存这些记录。 – 2012-08-14 14:39:25

回答

12

免责声明:

这不会回答你如何排序字符串列表或如何将数据加载到一个字符串列表。它会为您提供一个哈希表,它比使用字符串列表更有效(40k名称,值与名称搜索配对)。

备选:

既然你有德尔福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. 

扩展:

的上面的解决方案,你可以简单地扩展到使用一个结构来存储不仅仅是一个主机名,例如也是一个主机位置:

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.