Check-in [3f0e26decd]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added cache class, which should be thread-safe?
Timelines: family | ancestors | descendants | both | feature/async-refresh
Files: files | file ages | folders
SHA1:3f0e26decde8763fb3a9f0fcf64cc0edc342d62c
User & Date: tinus 2015-11-27 21:19:01
Context
2015-11-27
21:20
Added cache unit. Leaf check-in: ad4c0f6a93 user: tinus tags: feature/async-refresh
21:19
Added cache class, which should be thread-safe? check-in: 3f0e26decd user: tinus tags: feature/async-refresh
21:18
Added overloaded operators for TVersion. check-in: 6062443d50 user: tinus tags: feature/async-refresh
Changes

Added src/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
unit Cache;

////////////////////////////////////////////////////////////////////////////////////////////////////
interface
uses
  System.Generics.Collections;

type
  TCache<T> = class
  private
    FRoots: TDictionary<string,string>;
    FInfos: TDictionary<string,T>;
  public
    constructor Create;
    destructor  Destroy; override;

    function  GetAssociatedRoot(const Dir: string): string;
    procedure SetAssociatedRoot(const Dir, RootDir: string);
    function  GetInfo(const RootDir: string; out Repo: T): Boolean;
    procedure SetInfo(const RootDir: string; const Info: T);
  end;


////////////////////////////////////////////////////////////////////////////////////////////////////
implementation
uses
  u_FinalPathName;

const
  cLockTimeout = 1000;

{ TCache<T> }

constructor TCache<T>.Create;
begin
  FRoots := TDictionary<string,string>.Create;
  FInfos := TDictionary<string,T>.Create;
end;

destructor TCache<T>.Destroy;
begin
  FRoots.Free;
  FInfos.Free;
  inherited;
end;

function TCache<T>.GetInfo(const RootDir: string; out Repo: T): Boolean;
begin
  Result := TMonitor.Enter(FInfos, cLockTimeout);
  if Result then
  try
    Result := FInfos.TryGetValue(RootDir, Repo) or FInfos.TryGetValue(GetFinalPathName(RootDir), Repo);
  finally
    TMonitor.Exit(FInfos);
  end;
end;

function TCache<T>.GetAssociatedRoot(const Dir: string): string;
begin
  if TMonitor.Enter(FRoots, cLockTimeout) then begin
    try
      if not (FRoots.TryGetValue(Dir, Result) or FRoots.TryGetValue(GetFinalPathName(Dir), Result)) then
          Result := '';
    finally
      TMonitor.Exit(FRoots);
    end;
  end else begin
    Result := '';
  end;
end;

procedure TCache<T>.SetInfo(const RootDir: string; const Info: T);
begin
  TMonitor.Enter(FInfos);
  try
    FInfos.AddOrSetValue(GetFinalPathName(RootDir), Info);
  finally
    TMonitor.Exit(FInfos);
  end;
end;

procedure TCache<T>.SetAssociatedRoot(const Dir, RootDir: string);
var
  FinalDir: string;
begin
  TMonitor.Enter(FRoots);
  try
    FinalDir := GetFinalPathName(Dir);
    if RootDir = '' then begin
      FRoots.Remove(FinalDir);
    end else begin
      FRoots.AddOrSetValue(GetFinalPathName(Dir), RootDir);
    end;
  finally
    TMonitor.Exit(FRoots);
  end;
end;

end.