What's the difference between MemoryCache.Add and MemoryCache.Set?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In the context of the .NET framework, specifically when dealing with caching, the `MemoryCache` class plays a vital role in managing data that needs to be stored temporarily for quick access. Two central methods provided by the `MemoryCache` class are `Add` and `Set`. While both methods serve the purpose of storing items in the cache, they differ in terms of behavior and application. Understanding the differences between these two methods is critical for developers who aim to optimize cache usage effectively.
MemoryCache Overview
`MemoryCache` is part of the `System.Runtime.Caching` namespace and provides a convenient way to store data in memory. This improves application performance by reducing the need to load data from slower storage media like databases or files multiple times.
Key Features of MemoryCache
- Thread-Safe: Operations on `MemoryCache` are thread-safe.
- Expiration Policies: Supports various expiration policies such as absolute and sliding expiration.
- Cache Item Priorities: Items can be assigned different priorities which may influence how they are scavenged from the cache.
- Callbacks: Provides the ability to define callbacks triggered upon cache item removal.
`Add` vs. `Set` Methods
Both `Add` and `Set` are used to insert objects into the cache but have different use-cases based on how they handle existing items.
`Add` Method
The `Add` method attempts to insert a new cache entry if an entry with the same key does not already exist.
Characteristics:
- Conditional Insertion: The `Add` method will not overwrite an existing item. If an entry with the specified key already exists, `Add` will return `false` without making any changes.
- Use-Case: Preferable when you need to ensure that no overwriting happens, and you're only interested in adding new data.
Sample Code:
- Unconditional Insertion: Always writes the item to the cache, overwriting any existing item with the same key.
- Use-Case: Ideal when the existing cache entry needs updating with new values without concern for prior key presence.
Related reading
- Whats the difference between Paxos and WRN in Cassandra?
- What's the difference between ZooKeeper and any distributed Key-Value stores?
- What's the key differences in existent approaches to mirror Kafka topics
- What''s the point of using Hinted Handoff in Cassandra, especially for consistencyANY?
- What's the difference between Minimmum Spanning Tree and Travelling Salesman Problems
- What's the difference between SortedList and SortedDictionary?
- What''s the difference between .NET Core, .NET Framework, and Xamarin?
- What's the difference between RouteLink and ActionLink in ASP.NET MVC?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.