改了一亿个东西,修了一亿个 BUG
This commit is contained in:
62
Cache.cs
62
Cache.cs
@@ -2,11 +2,8 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.Script.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using static System.Text.Json.JsonSerializer;
|
||||
|
||||
namespace AnotherReplayReader
|
||||
{
|
||||
@@ -15,24 +12,32 @@ namespace AnotherReplayReader
|
||||
public static string CacheDirectory => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RA3Bar.Lanyi.AnotherReplayReader");
|
||||
public static string CacheFilePath => Path.Combine(CacheDirectory, "AnotherReplayReader.cache");
|
||||
|
||||
private ConcurrentDictionary<string, string> _storage;
|
||||
private readonly ConcurrentDictionary<string, string> _storage = new();
|
||||
|
||||
public Cache()
|
||||
{
|
||||
try
|
||||
Task.Run(async () =>
|
||||
{
|
||||
if (!Directory.Exists(CacheDirectory))
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(CacheDirectory);
|
||||
}
|
||||
if (!Directory.Exists(CacheDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(CacheDirectory);
|
||||
}
|
||||
|
||||
var serializer = new JavaScriptSerializer();
|
||||
_storage = serializer.Deserialize<ConcurrentDictionary<string, string>>(File.ReadAllText(CacheFilePath));
|
||||
}
|
||||
catch
|
||||
{
|
||||
_storage = new ConcurrentDictionary<string, string>();
|
||||
}
|
||||
using var cacheStream = File.OpenRead(CacheFilePath);
|
||||
var futureCache = DeserializeAsync<Dictionary<string, string>>(cacheStream).ConfigureAwait(false);
|
||||
if (await futureCache is not { } cached)
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (var kv in cached)
|
||||
{
|
||||
_storage.TryAdd(kv.Key, kv.Value);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
});
|
||||
}
|
||||
|
||||
public T GetOrDefault<T>(string key, in T defaultValue)
|
||||
@@ -41,8 +46,7 @@ namespace AnotherReplayReader
|
||||
{
|
||||
if (_storage.TryGetValue(key, out var valueString))
|
||||
{
|
||||
var serializer = new JavaScriptSerializer();
|
||||
return serializer.Deserialize<T>(valueString);
|
||||
return Deserialize<T>(valueString) ?? defaultValue;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
@@ -53,8 +57,20 @@ namespace AnotherReplayReader
|
||||
{
|
||||
try
|
||||
{
|
||||
var serializer = new JavaScriptSerializer();
|
||||
_storage[key] = serializer.Serialize(value);
|
||||
_storage[key] = Serialize(value);
|
||||
Save();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public void SetValues(params (string Key, object Value)[] values)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var (key, value) in values)
|
||||
{
|
||||
_storage[key] = Serialize(value);
|
||||
}
|
||||
Save();
|
||||
}
|
||||
catch { }
|
||||
@@ -66,12 +82,12 @@ namespace AnotherReplayReader
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
public async void Save()
|
||||
{
|
||||
try
|
||||
{
|
||||
var serializer = new JavaScriptSerializer();
|
||||
File.WriteAllText(CacheFilePath, serializer.Serialize(_storage));
|
||||
using var cacheStream = File.Open(CacheFilePath, FileMode.Create, FileAccess.Write);
|
||||
await SerializeAsync(cacheStream, _storage).ConfigureAwait(false);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user