Skip to content

Commit 68eeec8

Browse files
committed
Move logging in GetOrCreate to when item is actually being added
1 parent e5afc60 commit 68eeec8

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

AsyncMemoryCache/AsyncMemoryCache.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,10 @@ public CacheEntityReference<TKey, TValue> this[TKey key]
112112
/// <inheritdoc/>
113113
public CacheEntityReference<TKey, TValue> GetOrCreate(TKey key, Func<Task<TValue>> objectFactory, AsyncLazyFlags lazyFlags = AsyncLazyFlags.None)
114114
{
115-
_logger.LogTrace("Adding item with key: {Key}", key);
116-
117115
if (TryGetValue(key, out var entity))
118116
return entity;
119117

118+
_logger.LogTrace("Adding item with key: {Key}", key);
120119
var cacheEntity = new CacheEntity<TKey, TValue>(key, objectFactory, lazyFlags);
121120
cacheEntity.ObjectFactory.Start();
122121
_cache[key] = cacheEntity;
@@ -139,13 +138,11 @@ public bool TryGetValue(TKey key, [NotNullWhen(true)] out CacheEntityReference<T
139138
if (Interlocked.Increment(ref entity.References) > 0)
140139
{
141140
entity.ExpirationStrategy?.CacheEntityAccessed();
142-
143-
var cacheEntityReference = new CacheEntityReference<TKey, TValue>(entity);
141+
value = new(entity);
144142

145143
// Need to decrement here to revert the Increment in the if-statement
146144
_ = Interlocked.Decrement(ref entity.References);
147145

148-
value = cacheEntityReference;
149146
return true;
150147
}
151148

0 commit comments

Comments
 (0)