
Let's examine how to use C# to introduce the Singleton Design Pattern Real-Time Example Caching. In other words, the class will provide the fundamental Caching functionality and will be created as a Singleton. Therefore, we may add elements to the cache, update the cache, retrieve elements from the cache, remove a specific element from the cache, and remove all elements from the cache by utilizing the Singleton instance.
We talked about how the Singleton class can inherit from another class, but the Static class can never inherit from another class in our post on Singleton vs. Static Class. Now let's see how this Caching Example's Singleton class is inherited from another class.
Create the Cache Interface
First, copy and paste the following code into a class file called ICacheService.cs. The operations necessary for caching are declared in this interface. As you can see, in order to save any data in the Cache, we are utilizing the object as the argument for both keys and values.
public interface ICacheService
{
bool Add(object key, object value);
bool AddOrUpdate(object key, object value);
object Get(object key);
bool Remove(object key);
void Clear();
}
Creating the Singleton Class
The Singleton Class must now be created by deriving from the ICacheService interface mentioned above and providing an implementation for each of the five interface methods. In order to use the following code, create a class file called CacheService.cs and copy and paste it.
public sealed class CacheService : ICacheService
{
// Concurrent Dictionary Collection is Thread-Safe;
// yet, it is a shared resource that requires protection in a multithreaded environment.
private ConcurrentDictionary<object, object> _concurrentDictionary = new();
// Initializing the variable during class startup and preparing it for use later on,
// this variable will hold the Singleton Instance.
private static readonly CacheService _cacheService = new();
// The Singleton Instance will be returned by the subsequent Static Method.
// A thread-safe method that makes use of eager loading
public static CacheService GetInstance()
{
return _cacheService;
}
// To prevent class instantiation from outside of this class, the constructor must be private.
private CacheService()
{
Console.WriteLine("Singleton cache instance created.");
Console.WriteLine();
}
// The Singleton Instance can be used to access the following methods from outside of the class.
// A Key-Value Pair can be added to the Cache using this function.
public bool Add(object key, object value)
{
return _concurrentDictionary.TryAdd(key, value);
}
// A Key-Value Pair can be added or updated into the Cache using this function.
// Add the key-value pair if the key is unavailable.
// Update the key's value if it has already been added.
public bool AddOrUpdate(object key, object value)
{
if (_concurrentDictionary.ContainsKey(key))
{
_concurrentDictionary.TryRemove(key, out _);
}
return _concurrentDictionary.TryAdd(key, value);
}
// If the specified key is in the cache, this function is used to return its value.
// If not, return null.
public object Get(object key)
{
if (_concurrentDictionary.ContainsKey(key))
{
return _concurrentDictionary[key];
}
return null!;
}
// Using this technique, the specified key is deleted from the cache.
// Return true if removed; return false otherwise.
public bool Remove(object key)
{
return _concurrentDictionary.TryRemove(key, out _);
}
// Using this technique, the specified key is deleted from the cache.
// Return true if removed; return false otherwise.
public void Clear()
{
// Eliminates every key and value from the Cache, or the ConcurrentDictionary.
_concurrentDictionary.Clear();
}
}

Join the conversation! Your thoughts help the community grow.