下面的代码在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;
}