StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.86k stars 1.5k forks source link

Storing decimal values in redis #2579

Open ahmetkoprulu opened 9 months ago

ahmetkoprulu commented 9 months ago

When I have to store decimals I convert them to a string in order to be able to use stack exchange API.

In the parsing method, there is no case for decimals. Is there a specific reason for it or it is a bug.

internal static RedisValue TryParse(object? obj, out bool valid)
{
    valid = true;
    switch (obj)
    {
        case null: return Null;
        case string v: return v;
        case int v: return v;
        case uint v: return v;
        case double v: return v;
        case byte[] v: return v;
        case bool v: return v;
        case long v: return v;
        case ulong v: return v;
        case float v: return v;
        case ReadOnlyMemory<byte> v: return v;
        case Memory<byte> v: return v;
        case RedisValue v: return v;
        default:
            valid = false;
            return Null;
    }
}
mgravell commented 9 months ago

Well, whether it is a bug depends on what scenarios we expect it to support. It gets a bit circular! I'd need to look at all the possible code paths to there to make a hard call on that, but that might be a moot distinction. If it is an omission, and there are valid use-cases for it: we can certainly consider adding it. What scenario are you targeting at the moment? If this worked today: what would your code look like, in a perfect world?

ahmetkoprulu commented 9 months ago

Sorry for the late response. I am feeding the cache with real-time asset information from various exchanges (Binance, Mexc, etc) as hashes that contain the last prices as well. It will probably work with double in most scenarios and I do not know if it fails on numbers with high precision. Also, I do not know if implementing it has any side effects. Nevertheless, I think it is a bad developer experience not to have direct decimal support. because I had to either store it as a string or a double and convert it to decimal in run time while dealing with mapping hashes genericly.