Junior — Middle
Have you dealt with in-memory caching systems?
sobes.tech AI
Answer from AI
Yes, in C#, in-memory caching systems are often used to improve application performance. For example, you can use the MemoryCache class from the System.Runtime.Caching namespace.
Here's a simple caching example:
using System;
using System.Runtime.Caching;
class Program
{
static void Main()
{
ObjectCache cache = MemoryCache.Default;
string cacheKey = "user_123";
// Try to get data from cache
var userData = cache.Get(cacheKey) as string;
if (userData == null)
{
// If not in cache, retrieve from source (e.g., database)
userData = "User data";
// Add to cache with a 5-minute expiration
cache.Set(cacheKey, userData, DateTimeOffset.Now.AddMinutes(5));
}
Console.WriteLine(userData);
}
}
This approach helps reduce the number of calls to slow data sources by keeping frequently used data in memory.