type
TCache = specialize TFPGMap<TKey, TData>;
var
Cache: TCache;
...
function m(Key: TKey): TData;
begin
if not Assigned(Cache) then
Cache := TCache.Create;
if not Cache.TryGetData(Key, Result) then
begin
Result := f(Key);
Cache.Add(Key, Result);
end;
end;
TKey and TData can be of any kind (as long as they are not a class).
The variable Cache should be freed upon exiting the program.
The variable Cache should be freed upon exiting the program.