mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
fix(game): 清理 FileStorage 异步存储路径的 MA0004
- 修复 FileStorage 在锁获取与异步释放路径上的 ConfigureAwait(false) 缺失 - 保持文件锁、临时文件写入和原子替换流程不变
This commit is contained in:
parent
4bb8f4f429
commit
bad6c1b108
@ -143,12 +143,12 @@ public sealed class FileStorage : IFileStorage, IDisposable
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
var path = ToPath(key);
|
||||
|
||||
await using (await _lockManager.AcquireLockAsync(path).ConfigureAwait(false))
|
||||
{
|
||||
var pathLock = await _lockManager.AcquireLockAsync(path).ConfigureAwait(false);
|
||||
await using var configuredPathLock = pathLock.ConfigureAwait(false);
|
||||
|
||||
if (File.Exists(path))
|
||||
File.Delete(path);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -178,11 +178,11 @@ public sealed class FileStorage : IFileStorage, IDisposable
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
var path = ToPath(key);
|
||||
|
||||
await using (await _lockManager.AcquireLockAsync(path).ConfigureAwait(false))
|
||||
{
|
||||
var pathLock = await _lockManager.AcquireLockAsync(path).ConfigureAwait(false);
|
||||
await using var configuredPathLock = pathLock.ConfigureAwait(false);
|
||||
|
||||
return File.Exists(path);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -236,24 +236,25 @@ public sealed class FileStorage : IFileStorage, IDisposable
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
var path = ToPath(key);
|
||||
|
||||
await using (await _lockManager.AcquireLockAsync(path).ConfigureAwait(false))
|
||||
{
|
||||
var pathLock = await _lockManager.AcquireLockAsync(path).ConfigureAwait(false);
|
||||
await using var configuredPathLock = pathLock.ConfigureAwait(false);
|
||||
|
||||
if (!File.Exists(path))
|
||||
throw new FileNotFoundException($"Storage key not found: {key}", path);
|
||||
|
||||
await using var fs = new FileStream(
|
||||
var fs = new FileStream(
|
||||
path,
|
||||
FileMode.Open,
|
||||
FileAccess.Read,
|
||||
FileShare.Read,
|
||||
_bufferSize,
|
||||
useAsync: true);
|
||||
await using var configuredFileStream = fs.ConfigureAwait(false);
|
||||
|
||||
using var sr = new StreamReader(fs, Encoding.UTF8);
|
||||
var content = await sr.ReadToEndAsync().ConfigureAwait(false);
|
||||
return _serializer.Deserialize<T>(content);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -354,22 +355,27 @@ public sealed class FileStorage : IFileStorage, IDisposable
|
||||
var path = ToPath(key);
|
||||
var tempPath = path + ".tmp";
|
||||
|
||||
await using (await _lockManager.AcquireLockAsync(path).ConfigureAwait(false))
|
||||
{
|
||||
var pathLock = await _lockManager.AcquireLockAsync(path).ConfigureAwait(false);
|
||||
await using var configuredPathLock = pathLock.ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
var content = _serializer.Serialize(value);
|
||||
|
||||
// 先写入临时文件
|
||||
await using (var fs = new FileStream(
|
||||
{
|
||||
var fs = new FileStream(
|
||||
tempPath,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.None,
|
||||
_bufferSize,
|
||||
useAsync: true))
|
||||
{
|
||||
await using var sw = new StreamWriter(fs, Encoding.UTF8);
|
||||
useAsync: true);
|
||||
await using var configuredFileStream = fs.ConfigureAwait(false);
|
||||
|
||||
var sw = new StreamWriter(fs, Encoding.UTF8);
|
||||
await using var configuredStreamWriter = sw.ConfigureAwait(false);
|
||||
|
||||
await sw.WriteAsync(content).ConfigureAwait(false);
|
||||
await sw.FlushAsync().ConfigureAwait(false);
|
||||
}
|
||||
@ -385,7 +391,6 @@ public sealed class FileStorage : IFileStorage, IDisposable
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user