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

Commit c637a36

Browse files
committed
Refactor CacheClient/Transaction to use atomic set command where possible, throw ex when not
1 parent 747cef8 commit c637a36

File tree

1 file changed

+25
-54
lines changed

1 file changed

+25
-54
lines changed

src/ServiceStack.Redis/RedisClient.ICacheClient.cs

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public T Get<T>(string key)
5252
);
5353
}
5454

55+
private static byte[] ToBytes<T>(T value)
56+
{
57+
var bytesValue = value as byte[];
58+
if (bytesValue == null && !Equals(value, default(T)))
59+
bytesValue = value.ToJson().ToUtf8Bytes();
60+
return bytesValue;
61+
}
62+
5563
public long Increment(string key, uint amount)
5664
{
5765
return Exec(r => r.IncrementValueBy(key, (int)amount));
@@ -64,47 +72,24 @@ public long Decrement(string key, uint amount)
6472

6573
public bool Add<T>(string key, T value)
6674
{
67-
var bytesValue = value as byte[];
68-
if (bytesValue != null)
69-
{
70-
return Exec(r => r.SetNX(key, bytesValue) == Success);
71-
}
72-
73-
var valueString = JsonSerializer.SerializeToString(value);
74-
return Exec(r => r.SetEntryIfNotExists(key, valueString));
75+
return Exec(r => r.Set(key, ToBytes(value), exists:false));
7576
}
7677

7778
public bool Set<T>(string key, T value)
7879
{
79-
var bytesValue = value as byte[];
80-
if (bytesValue != null)
81-
{
82-
Exec(r => ((RedisNativeClient)r).Set(key, bytesValue));
83-
return true;
84-
}
85-
86-
Exec(r => r.SetEntry(key, JsonSerializer.SerializeToString(value)));
80+
Exec(r => ((RedisNativeClient)r).Set(key, ToBytes(value)));
8781
return true;
8882
}
8983

9084
public bool Replace<T>(string key, T value)
9185
{
92-
var exists = ContainsKey(key);
93-
if (!exists) return false;
94-
95-
var bytesValue = value as byte[];
96-
if (bytesValue != null)
97-
{
98-
Exec(r => ((RedisNativeClient)r).Set(key, bytesValue));
99-
return true;
100-
}
101-
102-
Exec(r => r.SetEntry(key, JsonSerializer.SerializeToString(value)));
103-
return true;
86+
return Exec(r => r.Set(key, ToBytes(value), exists: true));
10487
}
10588

10689
public bool Add<T>(string key, T value, DateTime expiresAt)
10790
{
91+
AssertNotInTransaction();
92+
10893
return Exec(r =>
10994
{
11095
if (r.Add(key, value))
@@ -118,34 +103,26 @@ public bool Add<T>(string key, T value, DateTime expiresAt)
118103

119104
public bool Add<T>(string key, T value, TimeSpan expiresIn)
120105
{
121-
return Exec(r => {
122-
r.SetEntryIfNotExists(key, value.ToJson(), expiresIn);
123-
return true;
124-
});
106+
return Exec(r => r.Set(key, ToBytes(value), exists:false, expiryMs:(long)expiresIn.TotalMilliseconds));
125107
}
126108

127109
public bool Set<T>(string key, T value, TimeSpan expiresIn)
128110
{
129-
var bytesValue = value as byte[];
130-
if (bytesValue != null)
111+
if (AssertServerVersionNumber() >= 2600)
131112
{
132-
if (AssertServerVersionNumber() >= 2600)
133-
{
134-
Exec(r => r.PSetEx(key, (long)expiresIn.TotalMilliseconds, bytesValue));
135-
}
136-
else
137-
{
138-
Exec(r => r.SetEx(key, (int)expiresIn.TotalSeconds, bytesValue));
139-
}
140-
return true;
113+
Exec(r => r.Set(key, ToBytes(value), 0, expiryMs: (long)expiresIn.TotalMilliseconds));
114+
}
115+
else
116+
{
117+
Exec(r => r.Set(key, ToBytes(value), (int)expiresIn.TotalSeconds));
141118
}
142-
143-
Exec(r => r.SetEntry(key, JsonSerializer.SerializeToString(value), expiresIn));
144119
return true;
145120
}
146121

147122
public bool Set<T>(string key, T value, DateTime expiresAt)
148123
{
124+
AssertNotInTransaction();
125+
149126
Exec(r =>
150127
{
151128
Set(key, value);
@@ -156,6 +133,8 @@ public bool Set<T>(string key, T value, DateTime expiresAt)
156133

157134
public bool Replace<T>(string key, T value, DateTime expiresAt)
158135
{
136+
AssertNotInTransaction();
137+
159138
return Exec(r =>
160139
{
161140
if (r.Replace(key, value))
@@ -169,15 +148,7 @@ public bool Replace<T>(string key, T value, DateTime expiresAt)
169148

170149
public bool Replace<T>(string key, T value, TimeSpan expiresIn)
171150
{
172-
return Exec(r =>
173-
{
174-
if (r.Replace(key, value))
175-
{
176-
r.ExpireEntryIn(key, expiresIn);
177-
return true;
178-
}
179-
return false;
180-
});
151+
return Exec(r => r.Set(key, ToBytes(value), exists:true, expiryMs: (long)expiresIn.TotalMilliseconds));
181152
}
182153

183154
public IDictionary<string, T> GetAll<T>(IEnumerable<string> keys)

0 commit comments

Comments
 (0)