using System.Runtime.Caching; ObjectCache cache = MemoryCache.Default; // Cache data with a key for 10 minutes cache.Add("key", "value", DateTime.Now.AddMinutes(10)); // Retrieve data from the cache using the key string cachedData = cache["key"].ToString();
using StackExchange.Redis; // Create connection to Redis server ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost"); // Get Redis cache database IDatabase cache = redis.GetDatabase(); // Add data to Redis cache with key cache.StringSet("key", "value"); // Retrieve data from Redis cache using the key string cachedData = cache.StringGet("key");In the above examples, we have used System.Runtime.Caching package for in-memory caching and StackExchange.Redis library for distributed caching using Redis cache.