File tree Expand file tree Collapse file tree 6 files changed +81
-3
lines changed
BitFaster.Caching.UnitTests/Lru Expand file tree Collapse file tree 6 files changed +81
-3
lines changed Original file line number Diff line number Diff line change @@ -257,5 +257,24 @@ public void WhenKeyDoesNotExistTryRemoveReturnsFalse()
257
257
258
258
lru . TryRemove ( 2 ) . Should ( ) . BeFalse ( ) ;
259
259
}
260
+
261
+ [ Fact ]
262
+ public void WhenKeyExistsTryUpdateUpdatesValueAndReturnsTrue ( )
263
+ {
264
+ lru . GetOrAdd ( 1 , valueFactory . Create ) ;
265
+
266
+ lru . TryUpdate ( 1 , "2" ) . Should ( ) . BeTrue ( ) ;
267
+
268
+ lru . TryGet ( 1 , out var value ) ;
269
+ value . Should ( ) . Be ( "2" ) ;
270
+ }
271
+
272
+ [ Fact ]
273
+ public void WhenKeyDoesNotExistTryUpdateReturnsFalse ( )
274
+ {
275
+ lru . GetOrAdd ( 1 , valueFactory . Create ) ;
276
+
277
+ lru . TryUpdate ( 2 , "3" ) . Should ( ) . BeFalse ( ) ;
278
+ }
260
279
}
261
280
}
Original file line number Diff line number Diff line change @@ -373,5 +373,24 @@ public void WhenRepeatedlyAddingAndRemovingSameValueLruRemainsInConsistentState(
373
373
lru . TryRemove ( 1 ) ;
374
374
}
375
375
}
376
+
377
+ [ Fact ]
378
+ public void WhenKeyExistsTryUpdateUpdatesValueAndReturnsTrue ( )
379
+ {
380
+ lru . GetOrAdd ( 1 , valueFactory . Create ) ;
381
+
382
+ lru . TryUpdate ( 1 , "2" ) . Should ( ) . BeTrue ( ) ;
383
+
384
+ lru . TryGet ( 1 , out var value ) ;
385
+ value . Should ( ) . Be ( "2" ) ;
386
+ }
387
+
388
+ [ Fact ]
389
+ public void WhenKeyDoesNotExistTryUpdateReturnsFalse ( )
390
+ {
391
+ lru . GetOrAdd ( 1 , valueFactory . Create ) ;
392
+
393
+ lru . TryUpdate ( 2 , "3" ) . Should ( ) . BeFalse ( ) ;
394
+ }
376
395
}
377
396
}
Original file line number Diff line number Diff line change @@ -41,10 +41,18 @@ public interface ICache<K, V>
41
41
Task < V > GetOrAddAsync ( K key , Func < K , Task < V > > valueFactory ) ;
42
42
43
43
/// <summary>
44
- /// Attempts to remove and return the value that has the specified key from the cache .
44
+ /// Attempts to remove the value that has the specified key.
45
45
/// </summary>
46
46
/// <param name="key">The key of the element to remove.</param>
47
47
/// <returns>true if the object was removed successfully; otherwise, false.</returns>
48
48
bool TryRemove ( K key ) ;
49
+
50
+ /// <summary>
51
+ /// Attempts to update the value that has the specified key.
52
+ /// </summary>
53
+ /// <param name="key">The key of the element to update.</param>
54
+ /// <param name="value">The new value.</param>
55
+ /// <returns>true if the object was updated successfully; otherwise, false.</returns>
56
+ bool TryUpdate ( K key , V value ) ;
49
57
}
50
58
}
Original file line number Diff line number Diff line change @@ -193,6 +193,19 @@ public bool TryRemove(K key)
193
193
return false ;
194
194
}
195
195
196
+ ///<inheritdoc/>
197
+ ///<remarks>Note: Calling this method does not affect LRU order.</remarks>
198
+ public bool TryUpdate ( K key , V value )
199
+ {
200
+ if ( this . dictionary . TryGetValue ( key , out var node ) )
201
+ {
202
+ node . Value . Value = value ;
203
+ return true ;
204
+ }
205
+
206
+ return false ;
207
+ }
208
+
196
209
// Thead A reads x from the dictionary. Thread B adds a new item. Thread A moves x to the end. Thread B now removes the new first Node (removal is atomic on both data structures).
197
210
private void LockAndMoveToEnd ( LinkedListNode < LruItem > node )
198
211
{
@@ -226,7 +239,7 @@ public LruItem(K k, V v)
226
239
227
240
public K Key { get ; }
228
241
229
- public V Value { get ; }
242
+ public V Value { get ; set ; }
230
243
}
231
244
}
232
245
}
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ public LruItem(K k, V v)
19
19
20
20
public readonly K Key ;
21
21
22
- public readonly V Value ;
22
+ public V Value { get ; set ; }
23
23
24
24
public bool WasAccessed
25
25
{
Original file line number Diff line number Diff line change @@ -223,6 +223,25 @@ public bool TryRemove(K key)
223
223
return false ;
224
224
}
225
225
226
+ ///<inheritdoc/>
227
+ ///<remarks>Note: Calling this method does not affect LRU order.</remarks>
228
+ public bool TryUpdate ( K key , V value )
229
+ {
230
+ if ( this . dictionary . TryGetValue ( key , out var existing ) )
231
+ {
232
+ lock ( existing )
233
+ {
234
+ if ( ! existing . WasRemoved )
235
+ {
236
+ existing . Value = value ;
237
+ return true ;
238
+ }
239
+ }
240
+ }
241
+
242
+ return false ;
243
+ }
244
+
226
245
private void Cycle ( )
227
246
{
228
247
// There will be races when queue count == queue capacity. Two threads may each dequeue items.
You can’t perform that action at this time.
0 commit comments