forked from Memnarch/Delphinus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDN.HttpClient.Cache.pas
294 lines (261 loc) · 6.62 KB
/
DN.HttpClient.Cache.pas
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
unit DN.HttpClient.Cache;
interface
uses
Classes,
Windows,
SysUtils,
SyncObjs,
Generics.Collections,
DN.HttpClient.Cache.Intf;
type
TDNHttpCache = class(TInterfacedObject, IDNHttpCache)
private
FEntries: TDictionary<string, IDNHttpCacheEntry>;
FDirectory: string;
FKey: string;
FMutex: TMutex;
FLockCount: Integer;
procedure LoadCache;
procedure SaveCache;
public
constructor Create(const ADirectory: string);
destructor Destroy; override;
procedure OpenCache;
procedure CloseCache;
procedure AddCache(const AUrl: string; AData: TStream; const ACacheControl: string; const AETag: string);
function TryGetCache(const AUrl: string; out AEntry: IDNHttpCacheEntry): Boolean;
end;
TDNHttpCacheEntry = class(TInterfacedObject, IDNHttpCacheEntry)
private
FID: string;
FETag: string;
FUrl: string;
FDirectory: string;
FMaxAge: Integer;
FLastModified: TDateTime;
function GetETag: string;
function GetID: string;
procedure SetETag(const Value: string);
procedure SetID(const Value: string);
function GetUrl: string;
procedure SetUrl(const Value: string);
function GetLastModified: TDateTime;
function GetMaxAge: Integer;
procedure SetLastModified(const Value: TDateTime);
procedure SetMaxAge(const Value: Integer);
public
constructor Create(const ADirectory: string);
procedure Store(const ASource: TStream);
procedure Load(const ATarget: TStream);
function IsExpired: Boolean;
property ID: string read GetID write SetID;
property ETag: string read GetETag write SetETag;
property Url: string read GetUrl write SetUrl;
property MaxAge: Integer read GetMaxAge write SetMaxAge;
property LastModified: TDateTime read GetLastModified write SetLastModified;
end;
implementation
uses
IOUtils,
DateUtils,
IniFiles;
const
CCacheIni = 'Cache.ini';
{ TDNHttpCache }
procedure TDNHttpCache.AddCache(const AUrl: string; AData: TStream;
const ACacheControl, AETag: string);
var
LEntry: IDNHttpCacheEntry;
LControl: TStringList;
LMaxAge: Integer;
begin
if not FEntries.TryGetValue(AUrl, LEntry) then
begin
LEntry := TDNHttpCacheEntry.Create(FDirectory);
LEntry.ID := TGuid.NewGuid.ToString();
LEntry.Url := AUrl;
FEntries.Add(LEntry.Url, LEntry);
end;
LEntry.ETag := AETag;
if ACacheControl <> '' then
begin
LControl := TStringList.Create();
try
LControl.CommaText := ACacheControl;
if TryStrToInt(LControl.Values['max-age'], LMaxAge) then
LEntry.MaxAge := LMaxAge;
finally
LControl.Free;
end;
end;
LEntry.Store(AData);
end;
procedure TDNHttpCache.CloseCache;
begin
Dec(FLockCount);
if FLockCount = 0 then
begin
SaveCache();
FMutex.Release();
end;
end;
constructor TDNHttpCache.Create(const ADirectory: string);
begin
inherited Create();
FEntries := TDictionary<string, IDNHttpCacheEntry>.Create();
FDirectory := ADirectory;
ForceDirectories(FDirectory);
FKey := StringReplace(ADirectory, '\', '/', [rfReplaceAll]);
FMutex := TMutex.Create(nil, False, FKey);
end;
destructor TDNHttpCache.Destroy;
begin
//If we are still locked, release ownership
if FLockCount > 0 then
begin
SaveCache();
FMutex.Release();
end;
FMutex.Free;
FEntries.Free;
inherited;
end;
procedure TDNHttpCache.LoadCache;
var
LIni: TMemIniFile;
LSections: TStringList;
LSection, LIniFile: string;
LEntry: IDNHttpCacheEntry;
begin
LIniFile := TPath.Combine(FDirectory, CCacheIni);
if not TFile.Exists(LIniFile) then
Exit;
LIni := TMemIniFile.Create(LIniFile);
try
LSections := TStringList.Create();
try
LIni.ReadSections(LSections);
for LSection in LSections do
begin
LEntry := TDNHttpCacheEntry.Create(FDirectory);
LEntry.ID := LSection;
LEntry.Url := LIni.ReadString(LSection, 'url', '');
LEntry.ETag := LIni.ReadString(LSection, 'ETag', '');
LEntry.MaxAge := LIni.ReadInteger(LSection, 'MaxAge', 0);
LEntry.LastModified := LIni.ReadDateTime(LSection, 'LastModified', 0);
FEntries.AddOrSetValue(LEntry.Url, LEntry);
end;
finally
LSections.Free;
end;
finally
LIni.Free;
end;
end;
procedure TDNHttpCache.OpenCache;
begin
if FLockCount = 0 then
begin
FMutex.Acquire();
LoadCache();
end;
Inc(FLockCount);
end;
procedure TDNHttpCache.SaveCache;
var
LIni: TMemIniFile;
LEntry: IDNHttpCacheEntry;
begin
LIni := TMemIniFile.Create(TPath.Combine(FDirectory, CCacheIni));
try
for LEntry in FEntries.Values do
begin
LIni.WriteString(LEntry.ID, 'url', LEntry.Url);
LIni.WriteString(LEntry.ID, 'ETag', LEntry.ETag);
LIni.WriteInteger(LEntry.ID, 'MaxAge', LEntry.MaxAge);
LIni.WriteDateTime(LEntry.ID, 'LastModified', LEntry.LastModified);
end;
LIni.UpdateFile();
finally
LIni.Free;
end;
end;
function TDNHttpCache.TryGetCache(const AUrl: string;
out AEntry: IDNHttpCacheEntry): Boolean;
begin
Result := FEntries.TryGetValue(AUrl, AEntry);
end;
{ TDNHttpCacheEntry }
constructor TDNHttpCacheEntry.Create(const ADirectory: string);
begin
inherited Create();
FDirectory := ADirectory;
end;
function TDNHttpCacheEntry.GetETag: string;
begin
Result := FETag;
end;
function TDNHttpCacheEntry.GetLastModified: TDateTime;
begin
Result := FLastModified;
end;
function TDNHttpCacheEntry.GetID: string;
begin
Result := FID;
end;
function TDNHttpCacheEntry.GetMaxAge: Integer;
begin
Result := FMaxAge;
end;
function TDNHttpCacheEntry.GetUrl: string;
begin
Result := FUrl;
end;
function TDNHttpCacheEntry.IsExpired: Boolean;
begin
Result := SecondsBetween(FLastModified, Now) >= FMaxAge;
end;
procedure TDNHttpCacheEntry.Load(const ATarget: TStream);
var
LFile: TFileStream;
begin
LFile := TFileStream.Create(TPath.Combine(FDirectory, FID), fmOpenRead);
try
ATarget.CopyFrom(LFile, LFile.Size);
finally
LFile.Free;
end;
end;
procedure TDNHttpCacheEntry.SetETag(const Value: string);
begin
FETag := Value;
end;
procedure TDNHttpCacheEntry.SetLastModified(const Value: TDateTime);
begin
FLastModified := Value;
end;
procedure TDNHttpCacheEntry.SetID(const Value: string);
begin
FID := Value;
end;
procedure TDNHttpCacheEntry.SetMaxAge(const Value: Integer);
begin
FMaxAge := Value;
end;
procedure TDNHttpCacheEntry.SetUrl(const Value: string);
begin
FUrl := Value;
end;
procedure TDNHttpCacheEntry.Store(const ASource: TStream);
var
LFile: TFileStream;
begin
LFile := TFileStream.Create(TPath.Combine(FDirectory, FID), fmCreate or fmOpenWrite);
try
LFile.CopyFrom(ASource, 0);
FLastModified := Now();
finally
LFile.Free;
end;
end;
end.