Skip to content

Commit fec9ae0

Browse files
committed
Added GetOrAdd static value overload
1 parent 108e2ca commit fec9ae0

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

FastCache/FastCache.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory, TimeSpan ttl)
156156
return _dict.GetOrAdd(key, (k, v) => new TtlValue(v.valueFactory(k), v.ttl), (ttl, valueFactory)).Value;
157157
}
158158

159+
/// <summary>
160+
/// Adds a key/value pair by using the specified function if the key does not already exist, or returns the existing value if the key exists.
161+
/// </summary>
162+
/// <param name="key">The key to add</param>
163+
/// <param name="value">The value to add</param>
164+
/// <param name="ttl">TTL of the item</param>
165+
public TValue GetOrAdd(TKey key, TValue value, TimeSpan ttl)
166+
{
167+
if (TryGet(key, out var existingValue))
168+
return existingValue;
169+
170+
return _dict.GetOrAdd(key, new TtlValue(value, ttl)).Value;
171+
}
172+
159173
/// <summary>
160174
/// Tries to remove item with the specified key
161175
/// </summary>

0 commit comments

Comments
 (0)