Skip to content

Commit

Permalink
feat: more polish of the async/await and .ConfigureAwait(false)
Browse files Browse the repository at this point in the history
  • Loading branch information
alastairtree committed Mar 4, 2018
1 parent d0d49f9 commit ce43ad1
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions LazyCache/CachingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public virtual T Get<T>(string key)
return UnwrapLazy<T>(item);
}

public virtual async Task<T> GetAsync<T>(string key)
public virtual Task<T> GetAsync<T>(string key)
{
ValidateKey(key);

var item = CacheProvider.Get(key);

return await UnwrapAsyncLazys<T>(item);
return UnwrapAsyncLazys<T>(item);
}

public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory)
Expand All @@ -106,8 +106,9 @@ public virtual async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemF
{
ValidateKey(key);

EnsureRemovedCallbackDoesNotReturnTheAsyncLazy<T>(policy);
EnsureEvictionCallbackDoesNotReturnTheAsyncLazy<T>(policy);

//any other way of doing this?
//var cacheItem = CacheProvider.GetOrCreateAsync(key, async entry =>
// await new AsyncLazy<T>(async () => {
// entry.SetOptions(policy);
Expand All @@ -122,7 +123,7 @@ public virtual async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemF
//});

object cacheItem;
await locker.WaitAsync(); //TODO: do we really need this?
await locker.WaitAsync().ConfigureAwait(false); //TODO: do we really need to lock - or can we lock just the key?
try
{
cacheItem = CacheProvider.GetOrCreate(key, entry =>
Expand All @@ -145,7 +146,7 @@ public virtual async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemF
if (result.IsCanceled || result.IsFaulted)
CacheProvider.Remove(key);

return await result;
return await result.ConfigureAwait(false);
}
catch //addItemFactory errored so do not cache the exception
{
Expand All @@ -168,7 +169,7 @@ public virtual T GetOrAdd<T>(string key, Func<T> addItemFactory, MemoryCacheEntr
{
ValidateKey(key);

EnsureRemovedCallbackDoesNotReturnTheLazy<T>(policy);
EnsureEvictionCallbackDoesNotReturnTheLazy<T>(policy);

object cacheItem;
locker.Wait(); //TODO: do we really need this?
Expand Down Expand Up @@ -206,20 +207,20 @@ public virtual void Remove(string key)

public virtual ICacheProvider CacheProvider => cacheProvider.Value;

public virtual async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory)
public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory)
{
return await GetOrAddAsync(key, addItemFactory, DefaultExpiryDateTime);
return GetOrAddAsync(key, addItemFactory, DefaultExpiryDateTime);
}

public virtual async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, DateTimeOffset expires)
public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, DateTimeOffset expires)
{
return await GetOrAddAsync(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
return GetOrAddAsync(key, addItemFactory, new MemoryCacheEntryOptions {AbsoluteExpiration = expires});
}

public virtual async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory,
public virtual Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory,
TimeSpan slidingExpiration)
{
return await GetOrAddAsync(key, addItemFactory,
return GetOrAddAsync(key, addItemFactory,
new MemoryCacheEntryOptions {SlidingExpiration = slidingExpiration});
}

Expand All @@ -240,24 +241,24 @@ protected virtual T UnwrapLazy<T>(object item)
return default(T);
}

protected virtual async Task<T> UnwrapAsyncLazys<T>(object item)
protected virtual Task<T> UnwrapAsyncLazys<T>(object item)
{
if (item is AsyncLazy<T> asyncLazy)
return await asyncLazy.Value;
return asyncLazy.Value;

if (item is Task<T> task)
return await task;
return task;

if (item is Lazy<T> lazy)
return lazy.Value;
return Task.FromResult(lazy.Value);

if (item is T variable)
return variable;
return Task.FromResult(variable);

return default(T);
return Task.FromResult(default(T));
}

protected virtual void EnsureRemovedCallbackDoesNotReturnTheLazy<T>(MemoryCacheEntryOptions policy)
protected virtual void EnsureEvictionCallbackDoesNotReturnTheLazy<T>(MemoryCacheEntryOptions policy)
{
if (policy?.PostEvictionCallbacks != null)
foreach (var item in policy.PostEvictionCallbacks)
Expand All @@ -274,7 +275,7 @@ protected virtual void EnsureRemovedCallbackDoesNotReturnTheLazy<T>(MemoryCacheE
}
}

protected virtual void EnsureRemovedCallbackDoesNotReturnTheAsyncLazy<T>(MemoryCacheEntryOptions policy)
protected virtual void EnsureEvictionCallbackDoesNotReturnTheAsyncLazy<T>(MemoryCacheEntryOptions policy)
{
if (policy?.PostEvictionCallbacks != null)
foreach (var item in policy.PostEvictionCallbacks)
Expand Down

0 comments on commit ce43ad1

Please sign in to comment.