31

I was able to do this in ServiceStack.redis by using,

IRedisTypedClient<ObjectName> myObj = redisClient.As<ObjectName>();

But I couldn't find any examples to do this in StackExchange.Redis.

Do I have to Serialize to JSON and then store them?

Thanx in advance.

4 Answers 4

32

At the current time, SE.Redis does not attempt to offer serialisation - there are simply too many different ways of doing that. I'm rather of the opinion that the library should do one thing, not 7. It should be possible to add any hybrid serialisation etc concerns simply by extension methods or other plumbing/wrapping code, choosing any serialisation strategy you choose, and any library you choose.

Sign up to request clarification or add additional context in comments.

1 Comment

So the solution is another layer of abstraction? I appreciate that StackExchange.Redis is a fairly bare-bones client and other concerns should be implemented as extensions. Any thoughts on github.com/imperugo/StackExchange.Redis.Extensions? Also, thank you so much for this!
12

Simplest solution will be use json/binary/other serialization. More complex but more native - using redis "hashes" data type.

In first case i prefer protobuf library (it included by default to StackExchange.Redis.Extensions.Protobuf nuget package). But you can use json/binary/xml serialization if you need.

There is a good performance report about them all : https://medium.com/@maximn/serialization-performance-comparison-xml-binary-json-p-ad737545d227

Comments

6

Example: Create helper function sample below, used JSON SerializeObject/DeSerializeObject before get/set.But this solution has the limitation object size can't not over Int32.MaxNumber ( 2,147,483,647 is 2Gb ).

Sample Code

    public static void SetData<T>(string key, T data)
    {
        using (var redis = ConnectionMultiplexer.Connect("localhost:6379"))
        {
            IDatabase db = redis.GetDatabase();
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            json_serializer.MaxJsonLength = int.MaxValue;

            db.StringSet(key, json_serializer.Serialize(data));
            redis.Close();
        }
    }

    public static T GetData<T>(string key)
    {
        using (var redis = ConnectionMultiplexer.Connect("localhost:6379"))
        {
            try
            {
                IDatabase db = redis.GetDatabase();
                var res = db.StringGet(key);

                redis.Close();
                if (res.IsNull)
                    return default(T);
                else
                    return JsonConvert.DeserializeObject<T>(res);
            }
            catch
            {
                return default(T);
            }

        }
    }

3 Comments

As mentioned in the documentation you shouldn't use ConnectionMultiplexer.Connect each time you want to access the data. You should store the connection and use the light weight GetDatabase() each time.
No need to call .close() on the main var of a using block.
This works fine when you only need to connect occasionally to save/get some values, so storing an active connections is useless.
0

Asynchronous version of extension methods for the IConnectionMultiplexer instance:

public static class RedisObjectSaver
{
    public static async Task SetData<T>(this IConnectionMultiplexer connection, string key, T data)
    {
        var db = connection.GetDatabase();

        _ = await db.StringSetAsync(key, JsonSerializer.Serialize(data, JsonSerializerOptions.Default));
    }

    public static async Task<T?> GetData<T>(this IConnectionMultiplexer connection, string key, CancellationToken token)
    {
        var db = connection.GetDatabase();
        var res = await db.StringGetAsync(key);
        
        return res.IsNull
            ? default
            : JsonSerializer.Deserialize<T>(res!, JsonSerializerOptions.Default);
    }
}

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.