Cache Setup for .NET Projects

Before we dive into fabulous pre-defined Cache Service implementations, we need to set up a cache setup connection in our project. REN supports 2 types of cache for now;

  • In Memory

  • Redis

First, you need to add some configuration to the appsetttings.json file:

"CacheConfiguration": {
  "RedisConfiguration": {
    "Url": "localhost:6379",
    "TimeConfiguration": {
      "AbsoluteExpirationInHours": 12
    },
    "DatabaseId": 0,
    "Username": "default",
    "Password": "mypwd",
    "AbortOnConnectFail": false
  },
  "InMemoryConfiguration": {
      "TimeConfiguration": {
          "AbsoluteExpirationInHours": 12,
          "SlidingExpirationInMinutes": 30
      }
  }
}

Let us set up the both of them in order:

Setting up the In Memory Cache

To set up In Memory cache you need to register required services into your Program.cs:

builder.Services.AddMemoryCache();

Setting up the Redis

To set up Redis cache you need to register required services into your Program.cs:

builder.Services.AddDistributedMemoryCache();

Last updated