Skip to content

Commit

Permalink
Add some documentation on the Json serialization options.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerhardt committed Apr 5, 2022
1 parent bc7f815 commit a3ff856
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,22 @@ async Task<IEnumerable<Monkey>> GetMonkeysAsync()
var url = "http://montemagno.com/monkeys.json";

//Dev handle online/offline scenario
if(!CrossConnectivity.Current.IsConnected)
if (!CrossConnectivity.Current.IsConnected)
{
return Barrel.Current.Get<IEnumerable<Monkey>>(key: url);
}

//Dev handles checking if cache is expired
if(!Barrel.Current.IsExpired(key: url))
if (!Barrel.Current.IsExpired(key: url))
{
return Barrel.Current.Get<IEnumerable<Monkey>>(key: url);
}


var client = new HttpClient();
var json = await client.GetStringAsync(url);
var monkeys = JsonConvert.DeserializeObject<IEnumerable<Monkey>>(json);
var monkeys = await client.GetFromJsonAsync<IEnumerable<Monkey>>(url);

//Saves the cache and pass it a timespan for expiration
Barrel.Current.Add(key: url, data: monkeys, expireIn: TimeSpan.FromDays(1));

}
```

Expand All @@ -107,30 +104,25 @@ Ideally, you can make these calls extremely generic and just pass in a string:
```csharp
public async Task<T> GetAsync<T>(string url, int days = 7, bool forceRefresh = false)
{
var json = string.Empty;

if (!CrossConnectivity.Current.IsConnected)
json = Barrel.Current.Get<string>(url);
return Barrel.Current.Get<T>(url);

if (!forceRefresh && !Barrel.Current.IsExpired(url))
json = Barrel.Current.Get<string>(url);
return Barrel.Current.Get<T>(url);

try
{
if (string.IsNullOrWhiteSpace(json))
{
json = await client.GetStringAsync(url);
Barrel.Current.Add(url, json, TimeSpan.FromDays(days));
}
return JsonConvert.DeserializeObject<T>(json);
T result = await httpClient.GetFromJsonAsync<T>(url);
Barrel.Current.Add(url, result, TimeSpan.FromDays(days));
return result;
}
catch (Exception ex)
{
Console.WriteLine($"Unable to get information from server {ex}");
//probably re-throw here :)
}

return default(T);
return default;
}
```

Expand Down Expand Up @@ -185,6 +177,14 @@ BarrelUtils.SetBaseCachePath("Path");

You MUST call this before initializing or accessing anything in the Barrel, and it can only ever be called once else it will throw an `InvalidOperationException`.

#### Json Serialization

MonkeyCache v2.0 and higher uses System.Text.Json to serialize objects to/from the backing store. By default, the default System.Text.Json serialization behavior is used. There are two options for controlling this serialization:

1. Pass an optional [JsonSerializationOptions](https://docs.microsoft.com/dotnet/api/system.text.json.jsonserializeroptions) instance to `Barrel.Current.Add` and `Barrel.Current.Get`.
2. Pass a `JsonTypeInfo<T>` instance to `Barrel.Current.Add` and `Barrel.Current.Get`. You can get a `JsonTypeInfo<T>` instance by using the System.Text.Json source generator. See [How to use source generation in System.Text.Json](https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-source-generation) for more information.

No matter which option you choose, it is recommended to use the same option between `Barrel.Current.Add` and `Barrel.Current.Get`. If the options are inconsistent, the information going into the backing store may not be read properly when retrieving it back from the Barrel.

### FAQ

Expand Down

0 comments on commit a3ff856

Please sign in to comment.