【问题标题】:Error when using Redis with C# : value is not an integer or out of range, sPort: 51410, LastCommand:将 Redis 与 C# 一起使用时出错:值不是整数或超出范围,sPort:51410,LastCommand:
【发布时间】:2015-01-03 01:05:51
【问题描述】:

下面的代码在 redis 中设置一个键,如果它不存在,则它具有一个到期期限,如果键已经存在,则每次递增它的值,当我尝试递增键的现有值时,代码会给出一个异常,即是当它进入 'If'

异常消息:值不是整数或超出范围,sPort:51814,LastCommand:

public bool SetKeyInRedis(string Id, double Amount)
    {
        bool b = false;

        try
        {
            string Key = "Id:" + Id;
            using (var redisClient = new RedisClient(RedisIPAddress,RedisPortNo))
            {
                if (redisClient.Exists(Key) == 1)
                {
                    redisClient.IncrByFloat(Key, Amount);
                    b = true;
                }
                else if (redisClient.Exists(Key) == 0)
                {
                    DateTime Today = DateTime.Now;
                    DateTime EndOfMonth = new DateTime(Today.Year, Today.Month, DateTime.DaysInMonth(Today.Year, Today.Month));

                    b = redisClient.Set<double>(Key, Amount, EndOfMonth);
                }
                else
                {
                    //to-do
                }
            }
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex.Message);
        }

        return b;
    }

【问题讨论】:

    标签: c# c#-4.0 redis servicestack.redis


    【解决方案1】:

    想出了解决办法:

    我使用的是旧版本的所有必需的ServiceStack dll's for Redis,下载了所有必需的 dll 的新版本,现在它可以正常工作了。

    【讨论】:

      【解决方案2】:

      INCRBYFLOAT 期望增量是浮点数,而不是双精度数。

      传递 float amount ,而不是 double amount,因为此命令可能不支持 double。

      【讨论】:

      • 我尝试将数据类型更改为 float ,但我仍然面临同样的问题。
      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 2019-03-29
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多