Skip to content

Commit

Permalink
Update barrel and introduce HttpCache
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Dec 7, 2017
1 parent 5c762d3 commit ccf4c9d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
33 changes: 26 additions & 7 deletions README.md
Expand Up @@ -10,18 +10,18 @@ For instance you are making a web request and you get some `json` back from the
That may look something like this:

```csharp
async Task<Monkey> GetMonkeysAsync()
async Task<IEnumerable<Monkey>> GetMonkeysAsync()
{
var url = "http://montemagno.com/monkeys.json";

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

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


Expand All @@ -30,7 +30,7 @@ async Task<Monkey> GetMonkeysAsync()
var monkeys = JsonConvert.DeserializeObject<IEnumerable<Monkey>>(json);

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

}
```
Expand All @@ -39,10 +39,29 @@ MonkeyCache will never delete data unless you want to, which is pretty nice inca

```csharp
//removes all data
await Barrel.EmptyAsync();
Barrel.Empty();

//param list of keys to flush
await Barrel.EmptyAsync(key: url);
Barrel.Empty(key: url);
```

Another goal of MonkeyCache is to offer a fast and native experience when storing and retrieving data from the Barrel.
The above shows how you can integrate MonkeyCache into your existing source code without any modifications to your network code. However, MonkeyCache can help you there too! MonkeyCache also offers helpers when dealing with network calls via HttpCache.

HttpCache balances on top of the Barrel and offers helper methods to pass in a simple url that will handle adding and updating data into the Barrel.

```csharp
Task<IEnumerable<Monkey>> GetMonkeysAsync()
{
var url = "http://montemagno.com/monkeys.json";

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

return HttpCache.GetAsync<IEnumerable<Monkey>>(key: url, expiration: TimeSpan.FromDays(1), headers: headers);

}
```


Another goal of MonkeyCache is to offer a fast and native experience when storing and retrieving data from the Barrel.
8 changes: 4 additions & 4 deletions src/MonkeyCache/Barrel.cs
Expand Up @@ -8,12 +8,12 @@ namespace MonkeyCache
/// </summary>
public class Barrel
{
public async Task<T> GetAsync<T>(string key)
public T Get<T>(string key)
{
throw new NotImplementedException();
}

public async Task<bool> AddAsync<T>(string key, T data)
public T Add<T>(string key, T data)
{
throw new NotImplementedException();
}
Expand All @@ -23,12 +23,12 @@ public bool IsExpired(string key)
throw new NotImplementedException();
}

public async Task<bool> EmptyAsync()
public bool Empty()
{
throw new NotImplementedException();
}

public async Task<bool> EmptyAsync(params string[] key)
public bool Empty(params string[] key)
{
throw new NotImplementedException();
}
Expand Down
10 changes: 10 additions & 0 deletions src/MonkeyCache/HttpCache.cs
@@ -0,0 +1,10 @@
using System;
namespace MonkeyCache
{
public class HttpCache
{
public HttpCache()
{
}
}
}

0 comments on commit ccf4c9d

Please sign in to comment.