I am using StackExchange.Redis
(Version 1.2.1) with an ASP.NET MVC C# application. I am able to store and view arrays and strings using StackExchange.Redis
. However, I am not able to figure out a way to store a list. Is there any way to do it? If not can you please suggest some good alternatives to StackExchange.Redis
to accomplish this requirement?
I recommend you to use StackExchange.Redis.Extensions, then you can do this:
var list = new List<int> { 1, 2, 3 };
bool added = cacheClient.Add("MyList", list, DateTimeOffset.Now.AddMinutes(10));
And to retrieve your list:
var list = cacheClient.Get<List<int>>("MyList");
Fast forward 3 years+ and I was looking to do similar in .NET core using StackExchange.Redis so leaving this here in case someone is looking to do the same.
There is "StackExchange.Redis.Extensions.Core" and it allows to store & retrieve as below. Use this nuget with "StackExchange.Redis.Extensions.AspNetCore" and "StackExchange.Redis.Extensions.Newtonsoft"
private readonly IRedisCacheClient _redisCacheClient;
public ValuesController(IRedisCacheClient redisCacheClient)
{
_redisCacheClient = redisCacheClient;
}
public async Task<List<int>> Get()
{
var list = new List<int> { 1, 2, 3 };
var added = await _redisCacheClient.Db0.AddAsync("MyList", list, DateTimeOffset.Now.AddMinutes(10));
return await _redisCacheClient.Db0.GetAsync<List<int>>("MyList");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With