Skip to content

Commit

Permalink
Why the cache is not updated? #276
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Dec 8, 2023
1 parent 91d4cd1 commit b6dad1b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
39 changes: 23 additions & 16 deletions source/NetCoreServer/FileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ private class FileCacheEntry

public FileCacheEntry(FileCache cache, string prefix, string path, string filter, InsertHandler handler, TimeSpan timespan)
{
_prefix = prefix;
_path = path;
_prefix = prefix.Replace('\\', '/').RemoveSuffix('/');
_path = path.Replace('\\', '/').RemoveSuffix('/');
_handler = handler;
_timespan = timespan;
_watcher = new FileSystemWatcher();
Expand Down Expand Up @@ -242,8 +242,8 @@ private static bool IsDirectory(string path)

private static void OnCreated(object sender, FileSystemEventArgs e, FileCache cache, FileCacheEntry entry)
{
var key = e.FullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix);
var file = e.FullPath.Replace('\\', '/');
var key = e.FullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/');
var file = e.FullPath.Replace('\\', '/').RemoveSuffix('/');

// Skip missing files
if (!File.Exists(file))
Expand All @@ -260,8 +260,8 @@ private static void OnChanged(object sender, FileSystemEventArgs e, FileCache ca
if (e.ChangeType != WatcherChangeTypes.Changed)
return;

var key = e.FullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix);
var file = e.FullPath.Replace('\\', '/');
var key = e.FullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/');
var file = e.FullPath.Replace('\\', '/').RemoveSuffix('/');

// Skip missing files
if (!File.Exists(file))
Expand All @@ -275,18 +275,18 @@ private static void OnChanged(object sender, FileSystemEventArgs e, FileCache ca

private static void OnDeleted(object sender, FileSystemEventArgs e, FileCache cache, FileCacheEntry entry)
{
var key = e.FullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix);
var file = e.FullPath.Replace('\\', '/');
var key = e.FullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/');
var file = e.FullPath.Replace('\\', '/').RemoveSuffix('/');

cache.RemoveFileInternal(entry._path, key);
}

private static void OnRenamed(object sender, RenamedEventArgs e, FileCache cache, FileCacheEntry entry)
{
var oldKey = e.OldFullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix);
var oldFile = e.OldFullPath.Replace('\\', '/');
var newKey = e.FullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix);
var newFile = e.FullPath.Replace('\\', '/');
var oldKey = e.OldFullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/');
var oldFile = e.OldFullPath.Replace('\\', '/').RemoveSuffix('/');
var newKey = e.FullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/');
var newFile = e.FullPath.Replace('\\', '/').RemoveSuffix('/');

// Skip missing files
if (!File.Exists(newFile))
Expand Down Expand Up @@ -339,12 +339,10 @@ private bool InsertPathInternal(string root, string path, string prefix, TimeSpa
{
try
{
string keyPrefix = (string.IsNullOrEmpty(prefix) || (prefix == "/")) ? "/" : (prefix + "/");

// Iterate through all directory entries
foreach (var item in Directory.GetDirectories(path))
{
string key = keyPrefix + HttpUtility.UrlDecode(Path.GetFileName(item));
string key = prefix + "/" + HttpUtility.UrlDecode(Path.GetFileName(item));

// Recursively insert sub-directory
if (!InsertPathInternal(root, item, key, timeout, handler))
Expand All @@ -353,7 +351,7 @@ private bool InsertPathInternal(string root, string path, string prefix, TimeSpa

foreach (var item in Directory.GetFiles(path))
{
string key = keyPrefix + HttpUtility.UrlDecode(Path.GetFileName(item));
string key = prefix + "/" + HttpUtility.UrlDecode(Path.GetFileName(item));

// Insert file into the cache
if (!InsertFileInternal(root, item, key, timeout, handler))
Expand Down Expand Up @@ -475,4 +473,13 @@ public WriteLock(ReaderWriterLockSlim locker) : base(locker.ExitWriteLock)
locker.EnterWriteLock();
}
}

/// <summary>
/// String extensions utility class.
/// </summary>
public static class StringExtensions
{
public static string RemoveSuffix(this string str, char toRemove) => str.EndsWith(toRemove) ? str.Substring(0, str.Length - 1) : str;
public static string RemoveSuffix(this string str, string toRemove) => str.EndsWith(toRemove) ? str.Substring(0, str.Length - toRemove.Length) : str;
}
}
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Version>8.0.3.0</Version>
<Version>8.0.4.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2023 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down

0 comments on commit b6dad1b

Please sign in to comment.