Last active
December 16, 2015 21:50
-
-
Save spewu/5503018 to your computer and use it in GitHub Desktop.
A way to send notifications through Redis, that I build. Requires these NuGet packages:
*ServiceStack.Redis
*Protobuf-net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IMessageSerializer<T> where T : class | |
{ | |
string Serialize(T message); | |
byte[] SerializeBytes(T message); | |
T Deserialize(string message); | |
T Deserialize(byte[] message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public interface IPublish<in T> : IDisposable where T : class | |
{ | |
int Publish(string channel, T message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IRedisConfiguration | |
{ | |
string Host { get; } | |
int Port { get; } | |
string Password { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public interface ISubscribe<out T> : IDisposable where T : class | |
{ | |
void Subscribe(string channel, Action<T> work); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using ProtoBuf; | |
public class ProtoBufSerializer<T> : IMessageSerializer<T> where T : class | |
{ | |
public string Serialize(T message) | |
{ | |
return Convert.ToBase64String(this.SerializeBytes(message)); | |
} | |
public byte[] SerializeBytes(T message) | |
{ | |
var stream = new MemoryStream(); | |
Serializer.Serialize(stream, message); | |
return stream.ToArray(); | |
} | |
public T Deserialize(string message) | |
{ | |
var stream = new MemoryStream(Convert.FromBase64String(message)); | |
return Serializer.Deserialize<T>(stream); | |
} | |
public T Deserialize(byte[] message) | |
{ | |
var stream = new MemoryStream(message); | |
return Serializer.Deserialize<T>(stream); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using ServiceStack.Redis; | |
public class RedisMessenger<T> : IPublish<T>, ISubscribe<T> where T : class | |
{ | |
private readonly IMessageSerializer<T> serializer; | |
private readonly RedisClient client; | |
private IRedisSubscription subscription; | |
public RedisMessenger(IMessageSerializer<T> serializer, IRedisConfiguration configuration) | |
{ | |
this.serializer = serializer; | |
client = new RedisClient(configuration.Host, configuration.Port, configuration.Password); | |
} | |
public int Publish(string channel, T message) | |
{ | |
return client.PublishMessage(channel, serializer.Serialize(message)); | |
} | |
public void Subscribe(string channel, Action<T> work) | |
{ | |
subscription = client.CreateSubscription(); | |
subscription.OnMessage = (ch, msg) => work(serializer.Deserialize(msg)); | |
subscription.SubscribeToChannels(channel); | |
} | |
public void Dispose() | |
{ | |
if (subscription != null) | |
{ | |
try | |
{ | |
subscription.UnSubscribeFromAllChannels(); | |
subscription.Dispose(); | |
} | |
catch (IOException) | |
{ | |
} | |
catch (RedisException) | |
{ | |
} | |
} | |
client.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment