Families.Web 1.0.0.0
사진, 동영상, 글, 댓글과 반응을 가족끼리만 공유하는 비공개 앨범·타임라인 서비스입니다.
로딩중...
검색중...
일치하는것 없음
JsonGlobalSettingsStore.cs
이 파일의 문서화 페이지로 가기
1using System.IO;
2using System.Text.Json;
4
6
16{
25 private readonly string _path;
34 private static readonly SemaphoreSlim _gate = new(1, 1);
44
53 private static readonly JsonSerializerOptions _jsonOpts = new() { WriteIndented = true };
54
72 {
73 var dataRoot = Path.GetDirectoryName(opts.ResolvedDataPath.TrimEnd(Path.DirectorySeparatorChar))
74 ?? Path.Combine(AppContext.BaseDirectory, "App_Data");
75 Directory.CreateDirectory(dataRoot);
76 _path = Path.Combine(dataRoot, "global-settings.json");
77 }
78
103 public async Task<GlobalSettings> GetAsync(CancellationToken ct = default)
104 {
105 if (_cache is not null) return _cache;
106
107 await _gate.WaitAsync(ct).ConfigureAwait(false);
108 try
109 {
110 if (_cache is not null) return _cache;
111
112 if (!File.Exists(_path))
113 {
114 _cache = new GlobalSettings();
115 return _cache;
116 }
117
118 await using var fs = File.OpenRead(_path);
119 _cache = await JsonSerializer.DeserializeAsync<GlobalSettings>(fs, _jsonOpts, ct).ConfigureAwait(false)
120 ?? new GlobalSettings();
121 return _cache;
122 }
123 finally { _gate.Release(); }
124 }
125
158 public async Task SaveAsync(GlobalSettings settings, CancellationToken ct = default)
159 {
160 await _gate.WaitAsync(ct).ConfigureAwait(false);
161 try
162 {
163 var tmp = _path + ".tmp";
164 await using (var fs = File.Create(tmp))
165 await JsonSerializer.SerializeAsync(fs, settings, _jsonOpts, ct).ConfigureAwait(false);
166
167 File.Copy(tmp, _path, overwrite: true);
168 File.Delete(tmp);
169 _cache = settings;
170 }
171 finally { _gate.Release(); }
172 }
173}
static readonly JsonSerializerOptions _jsonOpts
async Task SaveAsync(GlobalSettings settings, CancellationToken ct=default)
async Task< GlobalSettings > GetAsync(CancellationToken ct=default)