Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Clear the SFTP File-Attribute Cache

See more SFTP Examples

Demonstrates the Chilkat SFtp.ClearCache method, which clears the internal remote file-attribute cache used when the EnableCache property is enabled. It takes no arguments.

Background: When caching is on, Chilkat remembers file attributes it has already fetched so that repeated lookups — common when listing then stat-ing many files — avoid extra round trips. The trade-off is staleness: if files change on the server, the cache can report old sizes or timestamps. ClearCache discards those remembered values so the next lookup queries the server fresh, which you would do after a known change or before a check that must be current.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.SFtp;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sftp: TSFtp;
  port: Integer;
  password: string;

begin
  success := False;

  //  Demonstrates the SFtp.ClearCache method, which clears the internal remote file-attribute cache
  //  used when the EnableCache property is True.  It takes no arguments.

  sftp := TSFtp.Create;

  //  Connect, authenticate, and initialize the SFTP subsystem.
  port := 22;
  success := sftp.Connect('sftp.example.com',port);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Normally you would not hard-code the password in source.  You should instead obtain it
  //  from an interactive prompt, environment variable, or a secrets vault.
  password := 'mySshPassword';

  success := sftp.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  success := sftp.InitializeSftp();
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  With caching enabled, repeated attribute lookups for the same file are served from memory.
  sftp.EnableCache := True;

  //  ... perform operations that populate the cache ...

  //  Clear the cache so that subsequent attribute lookups query the server again, for example
  //  after files on the server are known to have changed.
  sftp.ClearCache();
  WriteLn('Attribute cache cleared.');

  sftp.Disconnect();


  sftp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.