Made more async goodness

This commit is contained in:
tidusjar 2016-05-27 15:07:07 +01:00
parent 8550cc4c5e
commit 166e0f81cf
7 changed files with 181 additions and 30 deletions

View file

@ -27,6 +27,7 @@
using System;
using System.Linq;
using System.Runtime.Caching;
using System.Threading.Tasks;
namespace PlexRequests.Helpers
{
@ -65,6 +66,23 @@ namespace PlexRequests.Helpers
return item.CloneJson();
}
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> itemCallback, int cacheTime = 20) where T : class
{
var item = Get<T>(key);
if (item == null)
{
item = await itemCallback();
if (item != null)
{
Set(key, item, cacheTime);
}
}
// Return a copy, not the stored cache reference
// The cached object will not change
return item.CloneJson();
}
/// <summary>
/// Gets the specified item from the cache.
/// </summary>