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

Commit 4f9196d

Browse files
committed
Implement Store with expiry, new SetValue API's, UrnKey
1 parent fe76caf commit 4f9196d

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

src/ServiceStack.Redis/Generic/RedisTypedClient.cs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ public List<string> GetAllKeys()
150150
return client.GetAllKeys();
151151
}
152152

153+
public string UrnKey(T entity)
154+
{
155+
return client.UrnKey(entity);
156+
}
157+
153158
public IRedisSet TypeIdsSet
154159
{
155160
get
@@ -176,28 +181,53 @@ public T DeserializeValue(byte[] value)
176181
return serializer.DeserializeFromString(strValue);
177182
}
178183

179-
public void SetEntry(string key, T value)
184+
public void SetValue(string key, T entity)
180185
{
181186
if (key == null)
182187
throw new ArgumentNullException("key");
183188

184-
client.Set(key, SerializeValue(value));
185-
client.RegisterTypeId(value);
189+
client.Set(key, SerializeValue(entity));
190+
client.RegisterTypeId(entity);
186191
}
187192

188-
public void SetEntry(string key, T value, TimeSpan expireIn)
193+
[Obsolete("Use SetValue()")]
194+
public void SetEntry(string key, T value)
195+
{
196+
SetValue(key, value);
197+
}
198+
199+
public void SetValue(string key, T entity, TimeSpan expireIn)
189200
{
190201
if (key == null)
191202
throw new ArgumentNullException("key");
192203

193-
client.Set(key, SerializeValue(value), expireIn);
194-
client.RegisterTypeId(value);
204+
client.Set(key, SerializeValue(entity), expireIn);
205+
client.RegisterTypeId(entity);
206+
}
207+
208+
[Obsolete("Use SetValue()")]
209+
public void SetEntry(string key, T value, TimeSpan expireIn)
210+
{
211+
SetValue(key, value, expireIn);
212+
}
213+
214+
public bool SetValueIfNotExists(string key, T entity)
215+
{
216+
var success = client.SetNX(key, SerializeValue(entity)) == RedisNativeClient.Success;
217+
if (success) client.RegisterTypeId(entity);
218+
return success;
195219
}
196220

221+
[Obsolete("Use SetValueIfNotExists()")]
197222
public bool SetEntryIfNotExists(string key, T value)
198223
{
199-
var success = client.SetNX(key, SerializeValue(value)) == RedisNativeClient.Success;
200-
if (success) client.RegisterTypeId(value);
224+
return SetValueIfNotExists(key, value);
225+
}
226+
227+
public bool SetValueIfExists(string key, T entity)
228+
{
229+
var success = client.Set(key, SerializeValue(entity), exists:true);
230+
if (success) client.RegisterTypeId(entity);
201231
return success;
202232
}
203233

@@ -399,8 +429,14 @@ public IList<T> GetAll()
399429
public T Store(T entity)
400430
{
401431
var urnKey = client.UrnKey(entity);
402-
this.SetEntry(urnKey, entity);
432+
this.SetValue(urnKey, entity);
433+
return entity;
434+
}
403435

436+
public T Store(T entity, TimeSpan expireIn)
437+
{
438+
var urnKey = client.UrnKey(entity);
439+
this.SetValue(urnKey, entity, expireIn);
404440
return entity;
405441
}
406442

0 commit comments

Comments
 (0)