@@ -8,20 +8,14 @@ namespace SRAFrontend.Services;
88public class DataPersistenceService
99{
1010 private readonly string _baseStorageDirectory ;
11-
12- private readonly string _settingsFilePath ;
1311 private readonly string _cacheFilePath ;
14- private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
12+
13+ private readonly JsonSerializerOptions _jsonSerializerOptions = new ( )
1514 {
1615 WriteIndented = true
1716 } ;
1817
19- private void EnsurePath ( )
20- {
21- if ( ! Directory . Exists ( _baseStorageDirectory ) ) Directory . CreateDirectory ( _baseStorageDirectory ) ;
22- if ( ! File . Exists ( _settingsFilePath ) ) File . Create ( _settingsFilePath ) . Dispose ( ) ;
23- if ( ! File . Exists ( _cacheFilePath ) ) File . Create ( _cacheFilePath ) . Dispose ( ) ;
24- }
18+ private readonly string _settingsFilePath ;
2519
2620 public DataPersistenceService ( )
2721 {
@@ -32,21 +26,41 @@ public DataPersistenceService()
3226 _cacheFilePath = Path . Combine ( _baseStorageDirectory , "cache.json" ) ;
3327 EnsurePath ( ) ;
3428 }
35-
29+
30+ private void EnsurePath ( )
31+ {
32+ if ( ! Directory . Exists ( _baseStorageDirectory ) ) Directory . CreateDirectory ( _baseStorageDirectory ) ;
33+ if ( ! File . Exists ( _settingsFilePath ) ) File . Create ( _settingsFilePath ) . Dispose ( ) ;
34+ if ( ! File . Exists ( _cacheFilePath ) ) File . Create ( _cacheFilePath ) . Dispose ( ) ;
35+ }
36+
3637 public Settings LoadSettings ( )
3738 {
3839 EnsurePath ( ) ;
3940 var json = File . ReadAllText ( _settingsFilePath ) ;
40- if ( string . IsNullOrWhiteSpace ( json ) )
41- {
42- return new Settings ( ) ;
43- }
41+ if ( string . IsNullOrWhiteSpace ( json ) ) return new Settings ( ) ;
4442 return JsonSerializer . Deserialize < Settings > ( json ) ?? new Settings ( ) ;
4543 }
44+
4645 public void SaveSettings ( Settings settings )
4746 {
4847 EnsurePath ( ) ;
4948 var json = JsonSerializer . Serialize ( settings , _jsonSerializerOptions ) ;
5049 File . WriteAllText ( _settingsFilePath , json ) ;
5150 }
51+
52+ public Cache LoadCache ( )
53+ {
54+ EnsurePath ( ) ;
55+ var json = File . ReadAllText ( _cacheFilePath ) ;
56+ if ( string . IsNullOrWhiteSpace ( json ) ) return new Cache ( ) ;
57+ return JsonSerializer . Deserialize < Cache > ( json ) ?? new Cache ( ) ;
58+ }
59+
60+ public void SaveCache ( Cache cache )
61+ {
62+ EnsurePath ( ) ;
63+ var json = JsonSerializer . Serialize ( cache , _jsonSerializerOptions ) ;
64+ File . WriteAllText ( _cacheFilePath , json ) ;
65+ }
5266}
0 commit comments