Skip to content

Instantly share code, notes, and snippets.

@jlyonsmith
Created August 19, 2014 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlyonsmith/dc9cf527685bdf522659 to your computer and use it in GitHub Desktop.
Save jlyonsmith/dc9cf527685bdf522659 to your computer and use it in GitHub Desktop.
ICacheClient for ServiceStack using StackExchange.Redis
using System;
using System.Collections.Generic;
using ServiceStack.Caching;
using StackExchange.Redis;
using ServiceStack.Text;
using System.Linq;
namespace Shared.ServiceInterface
{
public class RedisCacheClient : ICacheClient, IRemoveByPattern
{
private readonly ConnectionMultiplexer conn;
public RedisCacheClient(ConnectionMultiplexer conn)
{
this.conn = conn;
}
#region IRemoveByPattern
// If we need either of these, see
// https://github.com/StackExchange/StackExchange.Redis/blob/a816651d6d2dce55f07c4c620654e3e38646a14c/Docs/KeysScan.md
public void RemoveByPattern(string pattern)
{
throw new NotImplementedException();
}
public void RemoveByRegex(string pattern)
{
throw new NotImplementedException();
}
#endregion
#region ICacheClient
public bool Add<T>(string key, T value, DateTime expiresAt)
{
TimeSpan expiresIn = (expiresAt.Kind == DateTimeKind.Utc ? expiresAt - DateTime.UtcNow : expiresAt - DateTime.Now);
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), expiresIn, When.NotExists);
}
public bool Add<T>(string key, T value, TimeSpan expiresIn)
{
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), expiresIn, When.NotExists);
}
public bool Add<T>(string key, T value)
{
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), when: When.NotExists);
}
public long Decrement(string key, uint amount)
{
return conn.GetDatabase().StringDecrement(key, (long)amount);
}
public void FlushAll()
{
throw new NotImplementedException();
}
public T Get<T>(string key)
{
return TypeSerializer.DeserializeFromString<T>(conn.GetDatabase().StringGet(key));
}
public IDictionary<string, T> GetAll<T>(IEnumerable<string> keys)
{
return conn.GetDatabase().StringGet(keys.OfType<RedisKey>().ToArray())
.Zip(keys, (k, v) => new Tuple<string, T>(k, TypeSerializer.DeserializeFromString<T>((string)v))).ToDictionary(i => i.Item1, i => i.Item2);
}
public long Increment(string key, uint amount)
{
return conn.GetDatabase().StringIncrement(key, (long)amount);
}
public bool Remove(string key)
{
return conn.GetDatabase().KeyDelete(key);
}
public void RemoveAll(IEnumerable<string> keys)
{
conn.GetDatabase().KeyDelete(keys.OfType<RedisKey>().ToArray());
}
public bool Replace<T>(string key, T value, DateTime expiresAt)
{
TimeSpan expiresIn = (expiresAt.Kind == DateTimeKind.Utc ? expiresAt - DateTime.UtcNow : expiresAt - DateTime.Now);
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), expiresIn, When.Exists);
}
public bool Replace<T>(string key, T value)
{
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), when: When.Exists);
}
public bool Replace<T>(string key, T value, TimeSpan expiresIn)
{
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), expiresIn, When.Exists);
}
public bool Set<T>(string key, T value)
{
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), when: When.Always);
}
public bool Set<T>(string key, T value, TimeSpan expiresIn)
{
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), expiresIn, When.Always);
}
public bool Set<T>(string key, T value, DateTime expiresAt)
{
TimeSpan expiresIn = (expiresAt.Kind == DateTimeKind.Utc ? expiresAt - DateTime.UtcNow : expiresAt - DateTime.Now);
return conn.GetDatabase().StringSet(key, TypeSerializer.SerializeToString<T>(value), expiresIn, When.Always);
}
public void SetAll<T>(IDictionary<string, T> values)
{
conn.GetDatabase().StringSet(
values.Select(p => new KeyValuePair<RedisKey, RedisValue>((RedisKey)p.Key, (RedisValue)TypeSerializer.SerializeToString<T>(p.Value))).ToArray(),
When.Always);
}
#endregion
#region IDisposable
public void Dispose()
{
// Nothing to dispose
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment