6

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?

3
  • 2
    Could you please show us what you have tried so far with code? Commented Apr 28, 2017 at 12:32
  • Shouldn't this be exactly the same as with arrays? Commented Apr 28, 2017 at 12:38
  • What actual problem are you experiencing? There's no limitation here. Redis can store anything that can be serialized, and a list definitely can be serialized. Perhaps the problem is not the list, but the contained type that's stored in the list. Commented Apr 28, 2017 at 13:02

4 Answers 4

10

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");
Sign up to request clarification or add additional context in comments.

3 Comments

Andrei Add and Get<T> are not supported by IDatabase (github.com/StackExchange/StackExchange.Redis/blob/master/…). You're probably working with a wrapper around the API... check the basics: stackexchange.github.io/StackExchange.Redis/Basics
@Leonardo thanks for the notice. You right, I have update the answer.
I found this tutorial here for anyone looking. It is more indepth with setup instructions. Watchout how you place the legacy config XML nodes in your app.config or web.config. tostring.it/2015/04/23/…
3

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");
        }

Comments

0

Strictly speaking, Redis only stores strings, and a List<MyObject>, is obviously not a string. But there's a way to reliably achieve your goal: transform your list into a string.

To store a List<MyObject>, first serialize it as a string, using tools such as JsonConvert, then store it on redis. When retrieving, just de-serialize it...

6 Comments

I think it's not a good idea. The serialization part should be done using redis client under the hood.
@Andrei pls check the stackexchange.github.io/StackExchange.Redis/Basics so you can see that although ideal, this is not done OOB
What is OOB, I cannot seem to find a definition for that?
Redis supports other value types as well, including byte[]: redis.io/topics/data-types-intro
@petrosmm OOB means Out-Of-the-Box (something that is done automatically, without need of external help)
|
0

Can't you use ListRightPush?

using StackExchange.Redis;

// Connect to Redis
var redis = ConnectionMultiplexer.Connect("localhost");

// Get a reference to the Redis database
var db = redis.GetDatabase();

// Append an item to the end of the list
db.ListRightPush("mylist", "newitem");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.