Keyword mapping refactoring

This commit is contained in:
Michael Schimmel
2025-12-09 12:59:36 +01:00
parent 43afbd6050
commit f8dc5b945c
13 changed files with 257 additions and 188 deletions
+14 -11
View File
@@ -11,12 +11,17 @@ uses
Myc.Futures;
type
TLoaderFunc<T> = reference to function(const Filename: String): TFuture<T>;
IDataFileCache<T> = interface
procedure Clear;
function GetOrAdd(const AFileName: string; const Loader: TLoaderFunc<T>): TFuture<T>;
end;
// Manages a cache for time-series data files loaded asynchronously.
// The cache is activated by assigning a function to the IsMemoryLow property.
// It evicts least-recently-used items when memory is low.
TDataFileCache<T> = class
type
TLoaderFunc = reference to function(const Filename: String): TFuture<T>;
TDataFileCache<T> = class(TInterfacedObject, IDataFileCache<T>)
private
type
// Holds metadata for a cached file.
@@ -29,10 +34,9 @@ type
end;
private
FCachedFiles: TDictionary<String, TCachedFile>;
FLoader: TLoaderFunc;
FLock: TCriticalSection;
public
constructor Create(const ALoader: TLoaderFunc);
constructor Create;
destructor Destroy; override;
// Removes all items from the cache, waiting for any pending operations to complete.
@@ -40,7 +44,7 @@ type
// Retrieves a data future from the cache or uses the provided loader function to create and cache it.
// Caching is only active if the IsMemoryLow property is assigned.
function GetOrAdd(const AFileName: string): TFuture<T>;
function GetOrAdd(const AFileName: string; const Loader: TLoaderFunc<T>): TFuture<T>;
end;
var
@@ -56,10 +60,9 @@ uses
{ TDataFileCache<T> }
constructor TDataFileCache<T>.Create(const ALoader: TLoaderFunc);
constructor TDataFileCache<T>.Create;
begin
inherited Create;
FLoader := ALoader;
FLock := TCriticalSection.Create;
FCachedFiles := TObjectDictionary<String, TCachedFile>.Create([doOwnsValues]);
end;
@@ -85,7 +88,7 @@ begin
end;
end;
function TDataFileCache<T>.GetOrAdd(const AFileName: string): TFuture<T>;
function TDataFileCache<T>.GetOrAdd(const AFileName: string; const Loader: TLoaderFunc<T>): TFuture<T>;
var
fileList: TArray<TCachedFile>;
i: Integer;
@@ -96,7 +99,7 @@ begin
// Simply execute the loader function and return the result.
if not Assigned(IsMemoryLow) then
begin
Result := FLoader(AFilename);
Result := Loader(AFilename);
exit;
end;
@@ -150,7 +153,7 @@ begin
end;
// Cache miss or stale entry. Execute the loader function.
Result := FLoader(AFilename);
Result := Loader(AFilename);
// Create a new cache entry for the loaded future.
cachedFile := TCachedFile.Create;