Sobes.tech
Junior — Middle

Can you list the types of caches used at the server level?

sobes.tech AI

Answer from AI

The following types of caches are usually used at the server level:

  • Memory Cache (In-Memory Cache): stores data in RAM for quick access (e.g., Redis, Memcached).
  • Disk Cache: saves data on disk for longer-term storage but with slower access speed.
  • Database Cache: an intermediate layer that stores query results from the database to reduce load.
  • HTTP Cache: caches web server responses to speed up delivery of frequently requested resources.
  • Application Cache: a cache specific to the application, such as session cache or configuration cache.

Example of using In-Memory cache in C# with MemoryCache:

using System.Runtime.Caching;

var cache = MemoryCache.Default;
string key = "user_123";
var userData = cache.Get(key);
if (userData == null) {
    userData = GetUserDataFromDb(123); // method to get data
    cache.Set(key, userData, DateTimeOffset.Now.AddMinutes(10));
}