Search code examples
.netcaching.net-4.0memorycache

Can I add an atomic operation to MemoryCache?


I'd like to know if I can implement a method on MemoryCache that removes an item from it and add a new one before any other thread tries to reach it. I can't seem to find anything that would let me control the lock(maybe for a good reason) so I can perform these two operations at once.


Solution

  • If you mean "with the same key", then use the indexer:

    cache[key] = value;
    

    If you mean with different keys, then: no