Search code examples
asp.net-coreredisstackexchange.redisdistributed-cache

Distributed Cache (redis) prefix keys


I'm trying to use shared redis instance as a distributed cache for multiple aspnetcore services. My first idea was to simply prefix cache keys for each service to avoid clashes, but it doesn't seem to be possible (beside doing it manually when setting each value). At least I cannot find any such configuration option in AddStackExchangeRedis call.

Am I missing something, is this not supported or is my approach completely wrong?


Solution

  • I believe your approach is completely fine. The key prefix option exists under the InstanceName property and you can use it just as you would expect:

    builder.Services.AddStackExchangeRedisCache(options =>
    {
        options.InstanceName = "your-key-prefix:";
    });