Families.Web
1.0.0.0
사진, 동영상, 글, 댓글과 반응을 가족끼리만 공유하는 비공개 앨범·타임라인 서비스입니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
JsonGlobalSettingsStore.cs
이 파일의 문서화 페이지로 가기
1
using
System.IO;
2
using
System.Text.Json;
3
using
FamiliesApp.Models
;
4
5
namespace
FamiliesApp.Services
;
6
15
public
sealed
class
JsonGlobalSettingsStore
:
IGlobalSettingsStore
16
{
25
private
readonly
string
_path
;
34
private
static
readonly SemaphoreSlim
_gate
=
new
(1, 1);
43
private
GlobalSettings
?
_cache
;
44
53
private
static
readonly JsonSerializerOptions
_jsonOpts
=
new
() { WriteIndented =
true
};
54
71
public
JsonGlobalSettingsStore
(
FamilyOptions
opts)
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
}
FamiliesApp.Models
Definition
AlbumInfo.cs:1
FamiliesApp.Services
Definition
FamilyOptions.cs:4
FamiliesApp.Models.GlobalSettings
Definition
GlobalSettings.cs:12
FamiliesApp.Services.FamilyOptions
Definition
FamilyOptions.cs:15
FamiliesApp.Services.FamilyOptions.ResolvedDataPath
string ResolvedDataPath
Definition
FamilyOptions.cs:44
FamiliesApp.Services.IGlobalSettingsStore
Definition
IGlobalSettingsStore.cs:14
FamiliesApp.Services.JsonGlobalSettingsStore.JsonGlobalSettingsStore
JsonGlobalSettingsStore(FamilyOptions opts)
Definition
JsonGlobalSettingsStore.cs:71
FamiliesApp.Services.JsonGlobalSettingsStore._jsonOpts
static readonly JsonSerializerOptions _jsonOpts
Definition
JsonGlobalSettingsStore.cs:53
FamiliesApp.Services.JsonGlobalSettingsStore._path
readonly string _path
Definition
JsonGlobalSettingsStore.cs:25
FamiliesApp.Services.JsonGlobalSettingsStore.SaveAsync
async Task SaveAsync(GlobalSettings settings, CancellationToken ct=default)
Definition
JsonGlobalSettingsStore.cs:158
FamiliesApp.Services.JsonGlobalSettingsStore.GetAsync
async Task< GlobalSettings > GetAsync(CancellationToken ct=default)
Definition
JsonGlobalSettingsStore.cs:103
FamiliesApp.Services.JsonGlobalSettingsStore._cache
GlobalSettings? _cache
Definition
JsonGlobalSettingsStore.cs:43
FamiliesApp.Services.JsonGlobalSettingsStore._gate
static readonly SemaphoreSlim _gate
Definition
JsonGlobalSettingsStore.cs:34
Services
JsonGlobalSettingsStore.cs
다음에 의해 생성됨 :
1.17.0