| using System; |
| using System.Collections.Generic; |
| using System.IO; |
| using System.Text; |
| using System.Text.Json; |
| using System.Text.Json.Serialization; |
| using K4os.Compression.LZ4; |
|
|
| namespace FastSeekWpf.Core; |
|
|
| public static class CacheManager |
| { |
| private static readonly string CachePath = Path.Combine( |
| Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
| "FastSeek", "cache.dat"); |
|
|
| private static readonly JsonSerializerOptions JsonOptions = new() |
| { |
| PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| IncludeFields = true |
| }; |
|
|
| private static string ConfigPath => Path.Combine( |
| Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), |
| "FastSeek", "config.txt"); |
|
|
| static CacheManager() |
| { |
| Directory.CreateDirectory(Path.GetDirectoryName(CachePath)!); |
| } |
|
|
| public static void SaveCache(IndexStore store) |
| { |
| var cache = store.ToCache(); |
| string json = JsonSerializer.Serialize(cache, JsonOptions); |
| byte[] bytes = Encoding.UTF8.GetBytes(json); |
| byte[] compressed = LZ4Pickler.Pickle(bytes, LZ4Level.L12_MAX); |
| File.WriteAllBytes(CachePath, compressed); |
| } |
|
|
| public static CacheData? LoadCache() |
| { |
| if (!File.Exists(CachePath)) return null; |
|
|
| try |
| { |
| byte[] compressed = File.ReadAllBytes(CachePath); |
| byte[] bytes = LZ4Pickler.Unpickle(compressed); |
| string json = Encoding.UTF8.GetString(bytes); |
| var cache = JsonSerializer.Deserialize<CacheData>(json, JsonOptions); |
| |
| if (cache != null && cache.DriveRoots == null || cache!.DriveRoots.Count == 0) |
| { |
| |
| return null; |
| } |
| return cache; |
| } |
| catch |
| { |
| return null; |
| } |
| } |
|
|
| public static void ClearCache() |
| { |
| if (File.Exists(CachePath)) |
| File.Delete(CachePath); |
| } |
|
|
| public static List<string> LoadExclusions() |
| { |
| if (!File.Exists(ConfigPath)) return new List<string>(); |
|
|
| var result = new List<string>(); |
| foreach (var line in File.ReadAllLines(ConfigPath)) |
| { |
| string trimmed = line.Trim().ToLowerInvariant(); |
| if (!string.IsNullOrEmpty(trimmed)) |
| result.Add(trimmed); |
| } |
| return result; |
| } |
|
|
| public static void SaveExclusions(List<string> exclusions) |
| { |
| Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath)!); |
| File.WriteAllText(ConfigPath, string.Join("\n", exclusions)); |
| } |
| } |
|
|