Skip to content

Commit

Permalink
Purge cache hashes on external deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Sep 3, 2024
1 parent f1ad2ed commit 5bda12a
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 273 deletions.
35 changes: 23 additions & 12 deletions Core/Net/NetFileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,15 @@ public string GetInProgressFileName(Uri url, string description)
/// </summary>
private void OnCacheChanged(object source, FileSystemEventArgs e)
{
log.Debug("File system watcher event fired");
log.DebugFormat("File system watcher event {0} fired for {1}",
e.ChangeType.ToString(),
e.FullPath);
OnCacheChanged();
if (e.ChangeType == WatcherChangeTypes.Deleted)
{
log.DebugFormat("Purging hashes reactively: {0}", e.FullPath);
PurgeHashes(null, e.FullPath);
}
}

/// <summary>
Expand Down Expand Up @@ -231,10 +238,7 @@ public bool IsMaybeCachedZip(Uri url, DateTime? remoteTimestamp = null)
// Local file too old, delete it
log.Debug("Found stale file, deleting it");
File.Delete(file);
File.Delete($"{file}.sha1");
File.Delete($"{file}.sha256");
sha1Cache.Remove(file);
sha256Cache.Remove(file);
PurgeHashes(null, file);
}
}
else
Expand Down Expand Up @@ -411,7 +415,7 @@ public string Store(Uri url,

TxFileManager tx_file = new TxFileManager();

// Make sure we clear our cache entry first.
// Clear our cache entry first
Remove(url);

string hash = CreateURLHash(url);
Expand All @@ -420,7 +424,7 @@ public string Store(Uri url,

Debug.Assert(
Regex.IsMatch(description, "^[A-Za-z0-9_.-]*$"),
"description isn't as filesystem safe as we thought... (#1266)");
$"description {description} isn't as filesystem safe as we thought... (#1266)");

string fullName = string.Format("{0}-{1}", hash, Path.GetFileName(description));
string targetPath = Path.Combine(cachePath, fullName);
Expand Down Expand Up @@ -467,13 +471,20 @@ public bool Remove(Uri url)
return false;
}

private void PurgeHashes(TxFileManager tx_file, string file)
private void PurgeHashes(TxFileManager? tx_file, string file)
{
tx_file.Delete($"{file}.sha1");
tx_file.Delete($"{file}.sha256");
try
{
sha1Cache.Remove(file);
sha256Cache.Remove(file);

sha1Cache.Remove(file);
sha256Cache.Remove(file);
tx_file ??= new TxFileManager();
tx_file.Delete($"{file}.sha1");
tx_file.Delete($"{file}.sha256");
}
catch
{
}
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions Netkan/Services/CachingHttpService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;

using log4net;

using CKAN.NetKAN.Model;

namespace CKAN.NetKAN.Services
Expand Down
258 changes: 0 additions & 258 deletions Tests/Core/Cache.cs

This file was deleted.

Loading

0 comments on commit 5bda12a

Please sign in to comment.