@@ -173,33 +173,36 @@ public bool TryAdd(TKey key, TValue value, TimeSpan ttl)
173
173
return _dict . TryAdd ( key , new TtlValue ( value , ttl ) ) ;
174
174
}
175
175
176
- /// <summary>
177
- /// 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.
178
- /// </summary>
179
- /// <param name="key">The key to add</param>
180
- /// <param name="valueFactory">The factory function used to generate the item for the key</param>
181
- /// <param name="ttl">TTL of the item</param>
182
- public TValue GetOrAdd ( TKey key , Func < TKey , TValue > valueFactory , TimeSpan ttl )
176
+ private TValue GetOrAddCore ( TKey key , Func < TValue > valueFactory , TimeSpan ttl )
183
177
{
184
178
bool wasAdded = false ; //flag to indicate "add vs get". TODO: wrap in ref type some day to avoid captures/closures
185
179
var ttlValue = _dict . GetOrAdd (
186
180
key ,
187
181
( k ) =>
188
182
{
189
183
wasAdded = true ;
190
- return new TtlValue ( valueFactory ( k ) , ttl ) ;
184
+ return new TtlValue ( valueFactory ( ) , ttl ) ;
191
185
} ) ;
192
186
193
187
//if the item is expired, update value and TTL
194
188
//since TtlValue is a reference type we can update its properties in-place, instead of removing and re-adding to the dictionary (extra lookups)
195
189
if ( ! wasAdded ) //performance hack: skip expiration check if a brand item was just added
196
190
{
197
- ttlValue . ModifyIfExpired ( ( ) => valueFactory ( key ) , ttl ) ;
191
+ ttlValue . ModifyIfExpired ( valueFactory , ttl ) ;
198
192
}
199
193
200
194
return ttlValue . Value ;
201
195
}
202
196
197
+ /// <summary>
198
+ /// 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.
199
+ /// </summary>
200
+ /// <param name="key">The key to add</param>
201
+ /// <param name="valueFactory">The factory function used to generate the item for the key</param>
202
+ /// <param name="ttl">TTL of the item</param>
203
+ public TValue GetOrAdd ( TKey key , Func < TKey , TValue > valueFactory , TimeSpan ttl )
204
+ => GetOrAddCore ( key , ( ) => valueFactory ( key ) , ttl ) ;
205
+
203
206
/// <summary>
204
207
/// 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.
205
208
/// </summary>
@@ -208,25 +211,7 @@ public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory, TimeSpan ttl)
208
211
/// <param name="ttl">TTL of the item</param>
209
212
/// <param name="factoryArgument">Argument value to pass into valueFactory</param>
210
213
public TValue GetOrAdd < TArg > ( TKey key , Func < TKey , TArg , TValue > valueFactory , TimeSpan ttl , TArg factoryArgument )
211
- {
212
- bool wasAdded = false ; //flag to indicate "add vs get"
213
- var ttlValue = _dict . GetOrAdd (
214
- key ,
215
- ( k ) =>
216
- {
217
- wasAdded = true ;
218
- return new TtlValue ( valueFactory ( k , factoryArgument ) , ttl ) ;
219
- } ) ;
220
-
221
- //if the item is expired, update value and TTL
222
- //since TtlValue is a reference type we can update its properties in-place, instead of removing and re-adding to the dictionary (extra lookups)
223
- if ( ! wasAdded ) //performance hack: skip expiration check if a brand item was just added
224
- {
225
- ttlValue . ModifyIfExpired ( ( ) => valueFactory ( key , factoryArgument ) , ttl ) ;
226
- }
227
-
228
- return ttlValue . Value ;
229
- }
214
+ => GetOrAddCore ( key , ( ) => valueFactory ( key , factoryArgument ) , ttl ) ;
230
215
231
216
/// <summary>
232
217
/// 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.
@@ -235,24 +220,7 @@ public TValue GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, Ti
235
220
/// <param name="value">The value to add</param>
236
221
/// <param name="ttl">TTL of the item</param>
237
222
public TValue GetOrAdd ( TKey key , TValue value , TimeSpan ttl )
238
- {
239
- bool wasAdded = false ; //flag to indicate "add vs get"
240
- var ttlValue = _dict . GetOrAdd ( key ,
241
- ( k ) =>
242
- {
243
- wasAdded = true ;
244
- return new TtlValue ( value , ttl ) ;
245
- } ) ;
246
-
247
- //if the item is expired, update value and TTL
248
- //since TtlValue is a reference type we can update its properties in-place, instead of removing and re-adding to the dictionary (extra lookups)
249
- if ( ! wasAdded ) //performance hack: skip expiration check if a brand item was just added
250
- {
251
- ttlValue . ModifyIfExpired ( ( ) => value , ttl ) ;
252
- }
253
-
254
- return ttlValue . Value ;
255
- }
223
+ => GetOrAddCore ( key , ( ) => value , ttl ) ;
256
224
257
225
/// <summary>
258
226
/// Tries to remove item with the specified key
0 commit comments