WeddingThankYou 1.0.0.0
결혼식 이후 감사 인사와 사진, 계좌 안내, 연락처를 모바일 페이지로 전달하는 감사장 서비스입니다.
로딩중...
검색중...
일치하는것 없음
JsonGlobalSettingsStore.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.IO;
3using System.Text.Json;
4using System.Threading;
5using System.Threading.Tasks;
7
9{
19 {
28 private readonly string _path;
37 private static readonly SemaphoreSlim _gate = new(1, 1);
47
56 private static readonly JsonSerializerOptions _jsonOpts = new() { WriteIndented = true };
57
75 {
76 var dataRoot = Path.GetDirectoryName(opts.ResolvedDataPath.TrimEnd(Path.DirectorySeparatorChar))
77 ?? Path.Combine(AppContext.BaseDirectory, "App_Data");
78 Directory.CreateDirectory(dataRoot);
79 _path = Path.Combine(dataRoot, "global-settings.json");
80 }
81
106 public async Task<GlobalSettings> GetAsync(CancellationToken ct = default)
107 {
108 if (_cache is not null) return _cache;
109
110 await _gate.WaitAsync(ct).ConfigureAwait(false);
111 try
112 {
113 if (_cache is not null) return _cache;
114
115 if (!File.Exists(_path))
116 {
117 _cache = new GlobalSettings();
118 return _cache;
119 }
120
121 await using var fs = File.OpenRead(_path);
122 _cache = await JsonSerializer.DeserializeAsync<GlobalSettings>(fs, _jsonOpts, ct).ConfigureAwait(false)
123 ?? new GlobalSettings();
124 _cache.Normalize();
125 return _cache;
126 }
127 finally { _gate.Release(); }
128 }
129
162 public async Task SaveAsync(GlobalSettings settings, CancellationToken ct = default)
163 {
164 settings.Normalize();
165 await _gate.WaitAsync(ct).ConfigureAwait(false);
166 try
167 {
168 var tmp = _path + ".tmp";
169 await using (var fs = File.Create(tmp))
170 await JsonSerializer.SerializeAsync(fs, settings, _jsonOpts, ct).ConfigureAwait(false);
171
172 File.Copy(tmp, _path, overwrite: true);
173 File.Delete(tmp);
174 _cache = settings;
175 }
176 finally { _gate.Release(); }
177 }
178 }
179}
async Task SaveAsync(GlobalSettings settings, CancellationToken ct=default)
async Task< GlobalSettings > GetAsync(CancellationToken ct=default)
static readonly JsonSerializerOptions _jsonOpts