Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

GetOrCreate* won't add empty entry on throw #216

Merged
merged 1 commit into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,39 @@ public static bool TryGetValue<TItem>(this IMemoryCache cache, object key, out T

public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value)
{
using (var entry = cache.CreateEntry(key))
{
entry.Value = value;
}
var entry = cache.CreateEntry(key);
entry.Value = value;
entry.Dispose();

return value;
}

public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, DateTimeOffset absoluteExpiration)
{
using (var entry = cache.CreateEntry(key))
{
entry.AbsoluteExpiration = absoluteExpiration;
entry.Value = value;
}
var entry = cache.CreateEntry(key);
entry.AbsoluteExpiration = absoluteExpiration;
entry.Value = value;
entry.Dispose();

return value;
}

public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, TimeSpan absoluteExpirationRelativeToNow)
{
using (var entry = cache.CreateEntry(key))
{
entry.AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
entry.Value = value;
}
var entry = cache.CreateEntry(key);
entry.AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow;
entry.Value = value;
entry.Dispose();

return value;
}

public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, IChangeToken expirationToken)
{
using (var entry = cache.CreateEntry(key))
{
entry.AddExpirationToken(expirationToken);
entry.Value = value;
}
var entry = cache.CreateEntry(key);
entry.AddExpirationToken(expirationToken);
entry.Value = value;
entry.Dispose();

return value;
}
Expand All @@ -100,11 +96,13 @@ public static TItem GetOrCreate<TItem>(this IMemoryCache cache, object key, Func
object result;
if (!cache.TryGetValue(key, out result))
{
using (var entry = cache.CreateEntry(key))
{
result = factory(entry);
entry.SetValue(result);
}
var entry = cache.CreateEntry(key);
result = factory(entry);
entry.SetValue(result);
// need to manually call dispose instead of having a using
// in case the factory passed in throws, in which case we
// do not want to add the entry to the cache
entry.Dispose();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all the using's in the file for consistency.

}

return (TItem)result;
Expand All @@ -115,11 +113,13 @@ public static async Task<TItem> GetOrCreateAsync<TItem>(this IMemoryCache cache,
object result;
if (!cache.TryGetValue(key, out result))
{
using (var entry = cache.CreateEntry(key))
{
result = await factory(entry);
entry.SetValue(result);
}
var entry = cache.CreateEntry(key);
result = await factory(entry);
entry.SetValue(result);
// need to manually call dispose instead of having a using
// in case the factory passed in throws, in which case we
// do not want to add the entry to the cache
entry.Dispose();
}

return (TItem)result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,46 @@ public async Task GetOrCreateAsync_ReturnExistingValue()
Assert.Same(obj, result);
}

[Fact]
public void GetOrCreate_WillNotCreateEmptyValue_WhenFactoryThrows()
{
var cache = CreateCache();
string key = "myKey";
try
{
cache.GetOrCreate<int>(key, entry =>
{
throw new Exception();
});
}
catch (Exception)
{
}

int obj;
Assert.False(cache.TryGetValue(key, out obj));
}

[Fact]
public async Task GetOrCreateAsync_WillNotCreateEmptyValue_WhenFactoryThrows()
{
var cache = CreateCache();
string key = "myKey";
try
{
await cache.GetOrCreateAsync<int>(key, entry =>
{
throw new Exception();
});
}
catch (Exception)
{
}

int obj;
Assert.False(cache.TryGetValue(key, out obj));
}

[Fact]
public void SetOverwritesAndInvokesCallbacks()
{
Expand Down