Upload FastSeekWpf/Core/CacheManager.cs
Browse files
FastSeekWpf/Core/CacheManager.cs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Collections.Generic;
|
| 3 |
+
using System.IO;
|
| 4 |
+
using System.IO.Compression;
|
| 5 |
+
using System.Text;
|
| 6 |
+
using System.Text.Json;
|
| 7 |
+
using System.Text.Json.Serialization;
|
| 8 |
+
using K4os.Compression.LZ4.Streams;
|
| 9 |
+
|
| 10 |
+
namespace FastSeekWpf.Core;
|
| 11 |
+
|
| 12 |
+
public static class CacheManager
|
| 13 |
+
{
|
| 14 |
+
private static readonly string CachePath = Path.Combine(
|
| 15 |
+
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
| 16 |
+
"FastSeek", "cache.dat");
|
| 17 |
+
|
| 18 |
+
private static readonly JsonSerializerOptions JsonOptions = new()
|
| 19 |
+
{
|
| 20 |
+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
| 21 |
+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
| 22 |
+
IncludeFields = true
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
private static string ConfigPath => Path.Combine(
|
| 26 |
+
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
| 27 |
+
"FastSeek", "config.txt");
|
| 28 |
+
|
| 29 |
+
static CacheManager()
|
| 30 |
+
{
|
| 31 |
+
Directory.CreateDirectory(Path.GetDirectoryName(CachePath)!);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
public static void SaveCache(IndexStore store)
|
| 35 |
+
{
|
| 36 |
+
var cache = store.ToCache();
|
| 37 |
+
string json = JsonSerializer.Serialize(cache, JsonOptions);
|
| 38 |
+
byte[] bytes = Encoding.UTF8.GetBytes(json);
|
| 39 |
+
|
| 40 |
+
using var ms = new MemoryStream();
|
| 41 |
+
using (var lz4 = new LZ4Stream(ms, LZ4Mode.HighCompression, LZ4StreamFlags.IsolateInnerStream))
|
| 42 |
+
{
|
| 43 |
+
lz4.Write(bytes, 0, bytes.Length);
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
File.WriteAllBytes(CachePath, ms.ToArray());
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
public static CacheData? LoadCache()
|
| 50 |
+
{
|
| 51 |
+
if (!File.Exists(CachePath)) return null;
|
| 52 |
+
|
| 53 |
+
try
|
| 54 |
+
{
|
| 55 |
+
byte[] compressed = File.ReadAllBytes(CachePath);
|
| 56 |
+
using var ms = new MemoryStream(compressed);
|
| 57 |
+
using var lz4 = new LZ4Stream(ms, LZ4Mode.Decode, LZ4StreamFlags.IsolateInnerStream);
|
| 58 |
+
using var reader = new StreamReader(lz4, Encoding.UTF8);
|
| 59 |
+
string json = reader.ReadToEnd();
|
| 60 |
+
return JsonSerializer.Deserialize<CacheData>(json, JsonOptions);
|
| 61 |
+
}
|
| 62 |
+
catch
|
| 63 |
+
{
|
| 64 |
+
return null;
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
public static void ClearCache()
|
| 69 |
+
{
|
| 70 |
+
if (File.Exists(CachePath))
|
| 71 |
+
File.Delete(CachePath);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
public static List<string> LoadExclusions()
|
| 75 |
+
{
|
| 76 |
+
if (!File.Exists(ConfigPath)) return new List<string>();
|
| 77 |
+
|
| 78 |
+
var result = new List<string>();
|
| 79 |
+
foreach (var line in File.ReadAllLines(ConfigPath))
|
| 80 |
+
{
|
| 81 |
+
string trimmed = line.Trim().ToLowerInvariant();
|
| 82 |
+
if (!string.IsNullOrEmpty(trimmed))
|
| 83 |
+
result.Add(trimmed);
|
| 84 |
+
}
|
| 85 |
+
return result;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
public static void SaveExclusions(List<string> exclusions)
|
| 89 |
+
{
|
| 90 |
+
Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath)!);
|
| 91 |
+
File.WriteAllText(ConfigPath, string.Join("\n", exclusions));
|
| 92 |
+
}
|
| 93 |
+
}
|