Skip to content

Commit b6dad1b

Browse files
committed
Why the cache is not updated? #276
1 parent 91d4cd1 commit b6dad1b

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

source/NetCoreServer/FileCache.cs

+23-16
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ private class FileCacheEntry
197197

198198
public FileCacheEntry(FileCache cache, string prefix, string path, string filter, InsertHandler handler, TimeSpan timespan)
199199
{
200-
_prefix = prefix;
201-
_path = path;
200+
_prefix = prefix.Replace('\\', '/').RemoveSuffix('/');
201+
_path = path.Replace('\\', '/').RemoveSuffix('/');
202202
_handler = handler;
203203
_timespan = timespan;
204204
_watcher = new FileSystemWatcher();
@@ -242,8 +242,8 @@ private static bool IsDirectory(string path)
242242

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

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

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

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

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

281281
cache.RemoveFileInternal(entry._path, key);
282282
}
283283

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

291291
// Skip missing files
292292
if (!File.Exists(newFile))
@@ -339,12 +339,10 @@ private bool InsertPathInternal(string root, string path, string prefix, TimeSpa
339339
{
340340
try
341341
{
342-
string keyPrefix = (string.IsNullOrEmpty(prefix) || (prefix == "/")) ? "/" : (prefix + "/");
343-
344342
// Iterate through all directory entries
345343
foreach (var item in Directory.GetDirectories(path))
346344
{
347-
string key = keyPrefix + HttpUtility.UrlDecode(Path.GetFileName(item));
345+
string key = prefix + "/" + HttpUtility.UrlDecode(Path.GetFileName(item));
348346

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

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

358356
// Insert file into the cache
359357
if (!InsertFileInternal(root, item, key, timeout, handler))
@@ -475,4 +473,13 @@ public WriteLock(ReaderWriterLockSlim locker) : base(locker.ExitWriteLock)
475473
locker.EnterWriteLock();
476474
}
477475
}
476+
477+
/// <summary>
478+
/// String extensions utility class.
479+
/// </summary>
480+
public static class StringExtensions
481+
{
482+
public static string RemoveSuffix(this string str, char toRemove) => str.EndsWith(toRemove) ? str.Substring(0, str.Length - 1) : str;
483+
public static string RemoveSuffix(this string str, string toRemove) => str.EndsWith(toRemove) ? str.Substring(0, str.Length - toRemove.Length) : str;
484+
}
478485
}

source/NetCoreServer/NetCoreServer.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
5-
<Version>8.0.3.0</Version>
5+
<Version>8.0.4.0</Version>
66
<Authors>Ivan Shynkarenka</Authors>
77
<Copyright>Copyright (c) 2019-2023 Ivan Shynkarenka</Copyright>
88
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>

0 commit comments

Comments
 (0)