Skip to content

Commit 1481f01

Browse files
committed
re-factor Set methods always pass in 0 as default value
1 parent f42c884 commit 1481f01

File tree

3 files changed

+19
-49
lines changed

3 files changed

+19
-49
lines changed

src/ServiceStack.Redis/Commands.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ public static class Commands
175175
public readonly static byte[] Flush = "FLUSH".ToUtf8Bytes();
176176
public readonly static byte[] Slowlog = "SLOWLOG".ToUtf8Bytes();
177177

178-
public readonly static byte[] ExpireInSeconds = "EX".ToUtf8Bytes();
179-
public readonly static byte[] ExpireInMilliseconds = "PX".ToUtf8Bytes();
180-
public readonly static byte[] SetIfKeyDoesNotExist = "NX".ToUtf8Bytes();
181-
public readonly static byte[] SetIfKeyExists = "XX".ToUtf8Bytes();
178+
public readonly static byte[] Ex = "EX".ToUtf8Bytes();
179+
public readonly static byte[] Px = "PX".ToUtf8Bytes();
180+
public readonly static byte[] Nx = "NX".ToUtf8Bytes();
181+
public readonly static byte[] Xx = "XX".ToUtf8Bytes();
182182
}
183183
}

src/ServiceStack.Redis/RedisClient.cs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,14 @@ public void SetEntry(string key, string value, TimeSpan expireIn)
126126
{
127127
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
128128

129-
if (expireIn.Milliseconds == 0)
130-
{
131-
base.Set(key, bytesValue, (int)expireIn.TotalSeconds);
132-
}
133-
else
134-
{
135-
base.Set(key, bytesValue, null, (long)expireIn.TotalMilliseconds);
136-
}
129+
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, expireIn.Milliseconds);
137130
}
138131

139132
public void SetEntryIfExists(string key, string value, TimeSpan expireIn)
140133
{
141134
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
142135

143-
if (expireIn.Milliseconds == 0)
144-
{
145-
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, null, true);
146-
}
147-
else
148-
{
149-
base.Set(key, bytesValue, null, (long)expireIn.TotalMilliseconds, true);
150-
}
136+
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, expireIn.Milliseconds, exists:true);
151137
}
152138

153139
public bool SetEntryIfNotExists(string key, string value)
@@ -162,14 +148,7 @@ public void SetEntryIfNotExists(string key, string value, TimeSpan expireIn)
162148
{
163149
var bytesValue = value != null ? value.ToUtf8Bytes() : null;
164150

165-
if (expireIn.Milliseconds == 0)
166-
{
167-
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, null, false);
168-
}
169-
else
170-
{
171-
base.Set(key, bytesValue, null, (long)expireIn.TotalMilliseconds, false);
172-
}
151+
base.Set(key, bytesValue, (int)expireIn.TotalSeconds, expireIn.Milliseconds, exists: false);
173152
}
174153

175154
public void ChangeDb(long db)

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public void Set(string key, byte[] value)
327327
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value);
328328
}
329329

330-
public void Set(string key, byte[] value, int? expirySeconds = null, long? expiryMs = null, bool? exists = null)
330+
public void Set(string key, byte[] value, int expirySeconds, long expiryMs = 0, bool? exists = null)
331331
{
332332
if (key == null)
333333
throw new ArgumentNullException("key");
@@ -336,29 +336,20 @@ public void Set(string key, byte[] value, int? expirySeconds = null, long? expir
336336
if (value.Length > OneGb)
337337
throw new ArgumentException("value exceeds 1G", "value");
338338

339-
if (!expirySeconds.HasValue && !exists.HasValue)
339+
if (exists == null)
340340
{
341-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value);
341+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value,
342+
Commands.Ex, expirySeconds.ToUtf8Bytes(),
343+
Commands.Px, expiryMs.ToUtf8Bytes());
342344
}
343-
else if (!exists.HasValue && expiryMs.HasValue)
344-
{
345-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.ExpireInMilliseconds, expiryMs.Value.ToUtf8Bytes());
346-
}
347-
else if (!exists.HasValue && expirySeconds.HasValue)
348-
{
349-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, Commands.ExpireInSeconds, expirySeconds.Value.ToUtf8Bytes());
350-
}
351-
else if (exists.HasValue && !expirySeconds.HasValue && !expiryMs.HasValue)
352-
{
353-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, exists.Value ? Commands.SetIfKeyExists : Commands.SetIfKeyDoesNotExist);
354-
}
355-
else if (exists.HasValue && expirySeconds.HasValue)
356-
{
357-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, exists.Value ? Commands.SetIfKeyExists : Commands.SetIfKeyDoesNotExist, Commands.ExpireInSeconds, expirySeconds.Value.ToUtf8Bytes());
358-
}
359-
else if (exists.HasValue && expiryMs.HasValue)
345+
else
360346
{
361-
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value, exists.Value ? Commands.SetIfKeyExists : Commands.SetIfKeyDoesNotExist, Commands.ExpireInMilliseconds, expiryMs.Value.ToUtf8Bytes());
347+
var entryExists = exists.Value ? Commands.Xx : Commands.Nx;
348+
349+
SendExpectSuccess(Commands.Set, key.ToUtf8Bytes(), value,
350+
Commands.Ex, expirySeconds.ToUtf8Bytes(),
351+
Commands.Px, expiryMs.ToUtf8Bytes(),
352+
entryExists);
362353
}
363354
}
364355

0 commit comments

Comments
 (0)