Skip to content

Commit ff70321

Browse files
committed
Introduce WriteCache option
1 parent cd02267 commit ff70321

4 files changed

Lines changed: 24 additions & 15 deletions

File tree

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public static async Task Main(string[] args)
5959
|---------------------------|------------------------------------------------------------------------------------------|----------|
6060
| string cacheKey | The key to use for the cache. | Required |
6161
| Func<Task<TRes>> function | The function to call. | Required |
62-
| CachedRequest? request | settings for the cache request (explained below). | Optional |
63-
| TRes? defaultResponse | default value to return if the function failed or was not called and the cache is empty. | Optional |
64-
| CancellationToken clt | a cancellation token. | Optional |
62+
| CachedRequest? request | Settings for the cache request (explained below). | Optional |
63+
| TRes? defaultResponse | Default value to return if the function failed or was not called and the cache is empty. | Optional |
64+
| CancellationToken clt | A cancellation token. | Optional |
6565

6666
### Example
6767

@@ -90,16 +90,16 @@ public static async Task Main(string[] args)
9090

9191
You can give optional settings to the CachedRequest object.
9292

93-
| Parameter | Description | Default |
94-
|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------|
95-
| OneCallPerLocalStorage | If true, the result will be returned from localstorage if it is not expired. | false |
96-
| OneCallPerSession | If true, the result will be returned from sessionstorage if it is not expired. | false |
97-
| ExpireLocalStorage | The DateTime the localstorage value will be expired. | 7 days |
98-
| ExpireSessionStorage | The DateTime the sessionstorage value will be expired. | 15 minutes |
99-
| IgnoreCache | If true, never return a cached result. | false |
100-
| CachedAndReplace | If true, The cached result will be returned and the cache will be refreshed for the next call. If no cache is found, the default or NULL value will be returned. | false |
101-
| CacheWhenOffline | If true, the cached result will be returned when offline, except when IgnoreCache is true. | true |
102-
| RetryOnJsonException | If true, If a JSON exception occurs, the cache will be cleared and the request will be retried once. This will minimize the effect if a breaking change was introduced in the JSON value. | true |
93+
| Parameter | Description | Default |
94+
|------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------|
95+
| OneCallPerLocalStorage | If true, the result will be returned from localstorage if it is not expired. | false |
96+
| OneCallPerSession | If true, the result will be returned from sessionstorage if it is not expired. Only writes to session if this is true. | false |
97+
| ExpireLocalStorage | The DateTime the localstorage value will be expired. | 7 days |
98+
| ExpireSessionStorage | The DateTime the sessionstorage value will be expired. | 15 minutes |
99+
| IgnoreCache | If true, never return a cached result. | false |
100+
| CachedAndReplace | If true, The cached result will be returned and the cache will be refreshed for the next call. If no cache is found, the default or NULL value will be returned. | false |
101+
| CacheWhenOffline | If true, the cached result will be returned when offline, except when IgnoreCache is true. | true |
102+
| WriteCache | If true, Writes response to localstorage cache. | true |
103103

104104
### Global settings
105105

Src/Drogecode.Blazor.OfflineSupport/Drogecode.Blazor.OfflineSupport.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7-
<Version>1.0.3</Version>
7+
<Version>1.0.4</Version>
88
<Title>Drogecode.Blazor.OfflineSupport</Title>
99
<Authors>Taco Droogers</Authors>
1010
<PackageProjectUrl>https://github.com/Drogecode/Drogecode.Blazor.OfflineSupport</PackageProjectUrl>

Src/Drogecode.Blazor.OfflineSupport/Models/CachedRequest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public class CachedRequest
2626
/// Ignore session and local storage cache
2727
/// </summary>
2828
public bool IgnoreCache { get; set; }
29+
30+
/// <summary>
31+
/// Write to the local storage cache
32+
/// </summary>
33+
public bool WriteCache { get; set; } = true;
2934

3035
/// <summary>
3136
/// Return cached but also call for update

Src/Drogecode.Blazor.OfflineSupport/Services/OfflineSupportService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ public OfflineSupportService(
149149
private async Task<TRes?> RunSaveAndReturn<TRes>(string cacheKey, Func<Task<TRes>> function, CachedRequest request, CancellationToken clt)
150150
{
151151
var result = await function();
152-
await _localStorageExpireService.SetItemAsync(cacheKey, result, request.ExpireLocalStorage, clt);
152+
if (request.WriteCache)
153+
{
154+
await _localStorageExpireService.SetItemAsync(cacheKey, result, request.ExpireLocalStorage, clt);
155+
}
156+
153157
if (request.OneCallPerSession)
154158
{
155159
await _sessionStorageExpireService.SetItemAsync(cacheKey, result, request.ExpireSession, clt);

0 commit comments

Comments
 (0)