blob: 3777915715dcb3d6f0441f1d78ae61d5dbf2df53 (
plain)
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
|
{ Кеш для строковых функций}
unit StringCache;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fgl;
type
TStringCache = class
private
memoizedCache: specialize TFPGMap<String, String>;
public
constructor Create;
destructor Destroy; override;
function isKeyExists(const key: String): Boolean;
function getValueByKey(const key: String): String;
procedure storeKeyValue(const key: String; const value: String);
procedure clearPairs;
function Count: Integer; // Added method to get cache size
end;
implementation
constructor TStringCache.Create;
begin
inherited Create;
memoizedCache := specialize TFPGMap<String, String>.Create;
memoizedCache.Sorted := True; // Enable binary search for better performance
end;
destructor TStringCache.Destroy;
begin
memoizedCache.Free;
inherited Destroy;
end;
function TStringCache.isKeyExists(const key: String): Boolean;
begin
Result := memoizedCache.IndexOf(key) >= 0;
end;
function TStringCache.getValueByKey(const key: String): String;
var
index: Integer;
begin
index := memoizedCache.IndexOf(key);
if index >= 0 then
Result := memoizedCache.Data[index]
else
Result := '';
end;
procedure TStringCache.storeKeyValue(const key: String; const value: String);
var
index: Integer;
begin
index := memoizedCache.IndexOf(key);
if index >= 0 then
memoizedCache.Data[index] := value
else
memoizedCache.Add(key, value);
end;
procedure TStringCache.clearPairs;
begin
memoizedCache.Clear;
end;
function TStringCache.Count: Integer;
begin
Result := memoizedCache.Count;
end;
end.
|