Skip to content

Commit

Permalink
Add Netkan option to overwrite cached files
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Nov 19, 2018
1 parent e5fc437 commit ca1c813
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Netkan/CmdLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal class CmdLineOptions
[Option("prerelease", HelpText = "Index GitHub prereleases")]
public bool PreRelease { get; set; }

[Option("overwrite-cache", HelpText = "Overwrite cached files")]
public bool OverwriteCache { get; set; }

[Option("version", HelpText = "Display the netkan version number and exit")]
public bool Version { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Netkan/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static int Main(string[] args)
new KSPManager(new ConsoleUser(false)),
new Win32Registry()
);
http = new CachingHttpService(cache);
http = new CachingHttpService(cache, Options.OverwriteCache);

var netkan = ReadNetkan();
Log.Info("Finished reading input");
Expand Down
15 changes: 12 additions & 3 deletions Netkan/Services/CachingHttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ namespace CKAN.NetKAN.Services
internal sealed class CachingHttpService : IHttpService
{
private readonly NetFileCache _cache;
private HashSet<Uri> _requestedURLs = new HashSet<Uri>();
private HashSet<Uri> _requestedURLs = new HashSet<Uri>();
private bool _overwriteCache = false;

public CachingHttpService(NetFileCache cache)
public CachingHttpService(NetFileCache cache, bool overwrite = false)
{
_cache = cache;
_cache = cache;
_overwriteCache = overwrite;
}

public string DownloadPackage(Uri url, string identifier, DateTime? updated)
{
if (_overwriteCache && !_requestedURLs.Contains(url))
{
// Discard cached file if command line says so,
// but only the first time in each run
_cache.Remove(url);
}

_requestedURLs.Add(url);

var cachedFile = _cache.GetCachedFilename(url, updated);
Expand Down

0 comments on commit ca1c813

Please sign in to comment.