-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vastly improved and simplified offline support services
- Loading branch information
Showing
9 changed files
with
210 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/Shiny.Mediator.AppSupport/Infrastructure/IOfflineService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
namespace Shiny.Mediator.Infrastructure; | ||
|
||
|
||
public interface IOfflineService | ||
{ | ||
Task<string> Set(object request, object result); | ||
Task<OfflineResult<TResult>?> Get<TResult>(object request); | ||
Task ClearByType(Type requestType); | ||
Task ClearByRequest(object request); | ||
Task Clear(); | ||
} | ||
|
||
public record OfflineResult<TResult>( | ||
string RequestKey, | ||
DateTimeOffset Timestamp, | ||
TResult Value | ||
); | ||
|
||
public class OfflineService(IStorageService storage, ISerializerService serializer) : IOfflineService | ||
{ | ||
public async Task<string> Set(object request, object result) | ||
{ | ||
var requestKey = this.GetRequestKey(request); | ||
await this.DoTransaction(dict => | ||
{ | ||
dict[requestKey] = new OfflineStore( | ||
this.GetTypeKey(request.GetType()), | ||
requestKey, | ||
DateTimeOffset.UtcNow, | ||
serializer.Serialize(result) | ||
); | ||
return true; | ||
}); | ||
return requestKey; | ||
} | ||
|
||
|
||
public async Task<OfflineResult<TResult>?> Get<TResult>(object request) | ||
{ | ||
OfflineResult<TResult>? result = null; | ||
await this | ||
.DoTransaction(dict => | ||
{ | ||
var requestKey = this.GetRequestKey(request); | ||
if (dict.TryGetValue(requestKey, out var store)) | ||
{ | ||
var jsonObj = serializer.Deserialize<TResult>(store.Json); | ||
result = new OfflineResult<TResult>(store.RequestKey, store.Timestamp, jsonObj); | ||
} | ||
return false; | ||
}) | ||
.ConfigureAwait(false); | ||
|
||
return result; | ||
} | ||
|
||
|
||
public Task ClearByType(Type requestType) => this.DoTransaction(dict => | ||
{ | ||
var typeKey = this.GetTypeKey(requestType); | ||
var keys = dict | ||
.Where(x => x.Value.TypeName == typeKey) | ||
.Select(x => x.Key) | ||
.ToList(); | ||
|
||
if (keys.Count == 0) | ||
return false; | ||
|
||
foreach (var key in keys) | ||
dict.Remove(key); | ||
|
||
return false; | ||
}); | ||
|
||
|
||
public Task ClearByRequest(object request) => this.DoTransaction(dict => | ||
{ | ||
var requestKey = this.GetRequestKey(request); | ||
if (dict.ContainsKey(requestKey)) | ||
{ | ||
dict.Remove(requestKey); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
|
||
public Task Clear() => this.DoTransaction(dict => | ||
{ | ||
dict.Clear(); | ||
return true; | ||
}); | ||
|
||
|
||
SemaphoreSlim semaphore = new(1, 1); | ||
Dictionary<string, OfflineStore> cache = null!; | ||
|
||
Task DoTransaction(Func<IDictionary<string, OfflineStore>, bool> action) => Task.Run(async () => | ||
{ | ||
await this.semaphore.WaitAsync(); | ||
if (this.cache == null) | ||
{ | ||
var dict = await storage | ||
.Get<Dictionary<string, OfflineStore>>(nameof(IStorageService)) | ||
.ConfigureAwait(false); | ||
|
||
this.cache = dict ?? new(); | ||
} | ||
var result = action(this.cache); | ||
if (result) | ||
await storage.Set(nameof(IStorageService), this.cache).ConfigureAwait(false); | ||
|
||
this.semaphore.Release(); | ||
}); | ||
|
||
|
||
protected virtual string GetRequestKey(object request) | ||
{ | ||
if (request is IRequestKey keyProvider) | ||
return keyProvider.GetKey(); | ||
|
||
var t = request.GetType(); | ||
var key = $"{t.Namespace}_{t.Name}"; | ||
|
||
return key; | ||
} | ||
|
||
|
||
protected string GetTypeKey(Type type) => $"{type.Namespace}.{type.Name}"; | ||
} | ||
|
||
record OfflineStore( | ||
string TypeName, | ||
string RequestKey, | ||
DateTimeOffset Timestamp, | ||
string Json | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.