Efficient Key Management: Implementing Tree Structure in Redis with .Net

Fatma Erkan
SDTR
Published in
3 min readAug 3, 2024

Are you interested in organizing your keys in Redis? Redis allows you to use a tree structure for better key management. This structure helps you group related keys into folders. It makes it easier to navigate and manage your data efficiently.

Redis simplifies and speeds up data management as a high-performance key-value data store. However, when working with large data, key management can become important. At this point, Redis provides flexibility to organize and manage keys.

Implementation

We’ll build a .NET 8 Web API application and integrate Redis to demonstrate its usage.

  • Add StackExchangeRedis package to your project
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.7"/>
  • Define Redis configuration to appsettings.json
"Redis": {
"ConnectionString": "localhost:6379,password=YOUR_PASSWORD" // Adjust based on your Redis server
}
  • Modify Program.cs to implement Redis.
builder.Services.AddSingleton<IConnectionMultiplexer>(
ConnectionMultiplexer.Connect(builder.Configuration.GetSection("Redis:ConnectionString").Value));
  • Create a service class to interact with Redis:
public  interface IRedisService
{
Task SetValueAsync(string key, string value);
Task<string?> GetValueAsync(string key);
}

public class RedisService : IRedisService
{
private readonly IConnectionMultiplexer _redis;

private readonly IDatabase _db;

private readonly ILogger<RedisService> _logger;

public RedisService(IConnectionMultiplexer redis,
ILogger<RedisService> logger)
{
_redis = redis;
_logger = logger;
_db = _redis.GetDatabase();
}

private readonly string FolderStructure = $"RedisApp:Keys:";

public async Task SetValueAsync(string key, string value)
{
await _db.StringSetAsync($"{FolderStructure}{key}", value);
}

public async Task<string?> GetValueAsync(string key)
{
return await _db.StringGetAsync($"{FolderStructure}{key}");
}
}

The above code defines the folder structure using “RedisApp:Keys:”. If you want to add more files, you can use the format as:

"FolderName:FolderName:FolderName:FolderName:FolderName:"
  • Register RedisService to Program.cs
builder.Services.AddSingleton<IRedisService, RedisService>();
  • Use the service in the TestController

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
private readonly IRedisService _redisService;

public TestController(IRedisService redisService)
{
_redisService = redisService;
}

[HttpGet]
public async Task<IActionResult> GetValue(string key)
{
var value = await _redisService.GetValueAsync(key);
return Ok($"Value is: {value}");
}

[HttpPost]
public async Task<IActionResult> SetValue(string key, string value)
{
await _redisService.SetValueAsync(key, value);
return Ok($"Key {key} added.");
}

}

Viewing Keys in Redis Insight

After adding keys from the project, you can see the keys in the folder structure we defined earlier as “RedisApp:Keys:{keys}”.

Filtering Keys

  • To view all the keys in the folder, apply a filter pattern like this:
RedisApp:Keys:*
  • If you are searching for a specific key:
RedisApp:Keys:test3
  • If you know the key name but are unsure about the folder name:
RedisApp:*:test3   //Use * for the folder name you are unsure of
or
*:*:test3 //To search for a specific key without folder names

--

--