Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 49a82af

Browse files
committed
Fix redis 3.2 error when INCR empty key
Since 3.2 redis raises error when client tries to use INCR on empty key. So `SET test=""; INCR test;` does no longer work, but RedisClient saves integers as empty strings if the value of integer is "0". This commit always sets integer value to the key despite of the integer value.
1 parent aef445a commit 49a82af

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/ServiceStack.Redis/RedisClient.ICacheClient.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,22 @@ public T Get<T>(string key)
5252
);
5353
}
5454

55+
//Looking up Dictionary<Type,bool> for type is faster than HashSet<Type>.
56+
private static Dictionary<Type, bool> integralTypes = new Dictionary<Type, bool> {
57+
{ typeof(byte), true },
58+
{ typeof(sbyte), true},
59+
{ typeof(short), true},
60+
{ typeof(ushort), true},
61+
{ typeof(int), true},
62+
{ typeof(uint), true },
63+
{ typeof(long), true},
64+
{ typeof(ulong), true},
65+
};
66+
5567
private static byte[] ToBytes<T>(T value)
5668
{
5769
var bytesValue = value as byte[];
58-
if (bytesValue == null && !Equals(value, default(T)))
70+
if (bytesValue == null && (integralTypes.ContainsKey(typeof(T)) || !Equals(value, default(T))))
5971
bytesValue = value.ToJson().ToUtf8Bytes();
6072
return bytesValue;
6173
}

tests/ServiceStack.Redis.Tests/RedisCacheClientTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,16 @@ public void Can_GetTimeToLive()
124124
ttl = cacheClient.GetTimeToLive(key);
125125
Assert.That(ttl, Is.Null);
126126
}
127+
128+
[Test]
129+
public void Can_increment_and_reset_values()
130+
{
131+
using (var client = new RedisManagerPool(TestConfig.SingleHost).GetCacheClient())
132+
{
133+
Assert.That(client.Increment("incr:counter", 10), Is.EqualTo(10));
134+
client.Set("incr:counter", 0);
135+
Assert.That(client.Increment("incr:counter", 10), Is.EqualTo(10));
136+
}
137+
}
127138
}
128139
}

tests/ServiceStack.Redis.Tests/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"type": "platform",
2222
"version": "1.0.0"
2323
},
24-
"NUnitLite" : "3.2.1",
24+
"NUnitLite" : "3.5",
2525
"ServiceStack.Redis" : "1.0.*",
2626
"ServiceStack.Interfaces" : "1.0.*",
2727
"ServiceStack.Text" : "1.0.*",

0 commit comments

Comments
 (0)