How to use Redis cache in a ServiceStack .Net Core API

Good Morning all,

Would someone be able to point me in the right direction as to how to set up redis caching for sessions in a .Net Core ServiceStack API?

Thank you so much,

Leeny

You just need to register Redis as your caching provider. The Session uses the registered ICacheClient to store sessions.

Thank you Demis. Is the package compatible with .Net Core s i am getting warnings?

Yes it’s compatible. Here is an example of a .NET Core App that registers Redis:

Demis,

Another question related to ServiceStack and Redis, we have different application names specified for our redis session provider in MVC. How do I specify the application name when setting up servicestack redis?

Thanks,
Leeny

You can’t, you’d either need to use different databases or different redis instances, although different Redis Instances is preferred.

Hi Demis,

I have just had a chance to come back to this and confused at the moment and hope you can help:
In our ServiceStack + MVC apis, we currently have a “Current Context” object that we is stored in ASP.Net Http session. We have a couple of applications that all store this “Current Context” object and is used internally in shared code. Currently, we have defined this in our web.config to specify the appliance name:

I am trying to achieve the same using Service Stack + .Net Core. I would like to save the “Current Context” object to Service Stack session. But the issue is that we could have several applications that share this code. Without the concept of specifying an application Name, how would I achieve this?

Thanks,
Leeny

This is up to the Redis Session Provider you’re using which presumably is prefixing the application name with every key.

There is no concept of Application Name in Redis, data is partitioned by either database index or Redis process, where you’d run a Redis instance on a different port. If you want to use the same Redis instance you can use a different db index.

If you’re saving the sessions yourself you can prefix the app name with your key which is all the Redis providers would be doing since there is no concept of App Name or partitioning by name in Redis.

Thanks Demis.

Would I be able to use the following code to prefix “application name” ?
container.Register(c =>
c.Resolve().GetCacheClient().WithPrefix(“site1”));

At the moment, we are only planning to cache service stack sessions - Is there anything in particular I need to add when SaveSession code is called to cache CustomUserSession to redis cache?

Once again, really appreciate your assistance.

Yeah that will prefix all cache keys with the specified prefix and by extension all ServiceStack Sessions which uses the registered ICacheClient to persist Sessions.