static void Main() { const string channelInvalidate = "cache/invalidate"; using (var pub = new RedisConnection("127.0.0.1")) using (var sub = new RedisSubscriberConnection("127.0.0.1")) { pub.Open(); sub.Open(); sub.Subscribe(channelInvalidate, (channel, data) => { string key = Encoding.UTF8.GetString(data); Console.WriteLine("Invalidated {0}", key); }); Console.WriteLine( "Enter a key to invalidate, or an empty line to exit"); string line; do { line = Console.ReadLine(); if (!string.IsNullOrEmpty(line)) { pub.Publish(channelInvalidate, line); } } while (!string.IsNullOrEmpty(line)); } }
static void Main() { // IMPORTANT: the "pub" and "sub" can be on entirely separate machines, // as long as they are talking to the same server. They are only shown // together here for convenience using (var sub = new RedisSubscriberConnection("localhost")) using (var pub = new RedisConnection("localhost")) { sub.Open(); pub.Open(); sub.Subscribe("busytime", (queue, payload) => { // you don't actually need the payload, probably var received = Encoding.UTF8.GetString(payload); Console.WriteLine("Work to do! Look busy!: " + received); }); string line; Console.WriteLine("Enter messages to send, or q to quit"); while ((line = Console.ReadLine()) != null && line != "q") { pub.Publish("busytime", line); } } }
internal static RedisSubscriberConnection GetSubscriberConnection() { var conn = new RedisSubscriberConnection(LocalHost, unsecuredPort); conn.Error += (s, args) => { Trace.WriteLine(args.Exception.Message, args.Cause); }; conn.Open(); return(conn); }
public virtual async Task Subscribe() { if (connection.State == RedisConnectionBase.ConnectionState.New) { await connection.Open(); } await connection.Subscribe( channel, (key, data) => action(Info.Deserialize <TInfo>( data))); }
public IAOPResult RegisterDispatcher(IEventDispatcher dispatcher) { if (!this.dispatchers.ContainsKey(dispatcher)) { lock (this.dispatchers) { if (!this.dispatchers.ContainsKey(dispatcher)) { var redis = new RedisSubscriberConnection(this.Redis.Host, this.Redis.Port); redis.Open(); redis.Subscribe(this.NotifyQueueName, (k, r) => dispatcher.OnMessageReceived(this, r)); dispatchers.Add(dispatcher, redis); } } } return(AOPResult.Success()); }
private void TestSubscriberNameOnRemote(bool setName) { string id = Config.CreateUniqueName(); using (var pub = new RedisConnection(Config.RemoteHost, allowAdmin: true)) using (var sub = new RedisSubscriberConnection(Config.RemoteHost)) { List <string> errors = new List <string>(); EventHandler <BookSleeve.ErrorEventArgs> errorHandler = (sender, args) => { lock (errors) errors.Add(args.Exception.Message); }; pub.Error += errorHandler; sub.Error += errorHandler; if (setName) { pub.Name = "pub_" + id; sub.Name = "sub_" + id; } int count = 0; var subscribe = sub.Subscribe("foo" + id, (key, payload) => Interlocked.Increment(ref count)); Task pOpen = pub.Open(), sOpen = sub.Open(); pub.WaitAll(pOpen, sOpen, subscribe); Assert.AreEqual(0, Interlocked.CompareExchange(ref count, 0, 0), "init message count"); pub.Wait(pub.Publish("foo" + id, "hello")); PubSub.AllowReasonableTimeToPublishAndProcess(); var clients = setName ? pub.Wait(pub.Server.ListClients()) : null; Assert.AreEqual(1, Interlocked.CompareExchange(ref count, 0, 0), "got message"); lock (errors) { foreach (var error in errors) { Console.WriteLine(error); } Assert.AreEqual(0, errors.Count, "zero errors"); } if (setName) { Assert.AreEqual(1, clients.Count(x => x.Name == pub.Name), "pub has name"); Assert.AreEqual(1, clients.Count(x => x.Name == sub.Name), "sub has name"); } } }
/// <summary> /// This is a small application to test event subscriptions in redis - which are required for automatic expiry of cache items /// if subscriptions dont work try setting the redis config : /// config set notify-keyspace-events Ex /// </summary> /// <param name="args"></param> private static void Main(string[] args) { Console.WriteLine("Start"); _conn = new RedisConnection("localhost"); var c = _conn.Open(); c.Wait(); Console.WriteLine("Conn : " + _conn.State); _conn.Keys.Remove(_db, "_expireys"); _subConn = new RedisSubscriberConnection("localhost"); var s = _subConn.Open(); s.Wait(); Console.WriteLine("SubConn : " + _subConn.State); channel = _conn.GetOpenSubscriberChannel(); Thread.Sleep(100); Console.WriteLine("Channel : " + channel.State); channel.PatternSubscribe("*:expired", OnExecutionCompleted).Wait(); Console.WriteLine("Subscriptions : " + channel.SubscriptionCount); Set(1, 4); Thread.Sleep((6 * 1000)); if (received > 0) { Console.WriteLine("Subscriptions have worked"); } else { Console.WriteLine("Subscriptions have not worked"); } Console.ReadKey(); }
public virtual async Task Subscribe( string eventName, Action <TInfo> action) { if (action == null) { throw new ArgumentNullException("action"); } if (connection.State == RedisConnectionBase.ConnectionState.New) { await connection.Open(); } var channel = Channel(eventName); await connection.Subscribe( channel, (key, data) => action(Info.Deserialize <TInfo>(data))); }
public void TestForcedSubscriberName() { using (var conn = Config.GetUnsecuredConnection(allowAdmin: true, open: true, waitForOpen: true)) using (var sub = new RedisSubscriberConnection(conn.Host, conn.Port)) { var task = sub.Subscribe("foo", delegate { }); string name = Config.CreateUniqueName(); sub.Name = name; sub.SetServerVersion(new Version("2.6.9"), ServerType.Master); sub.Wait(sub.Open()); sub.Wait(task); Assert.AreEqual(1, sub.SubscriptionCount); if (!conn.Features.ClientName) { Assert.Inconclusive(); } var clients = conn.Wait(conn.Server.ListClients()).Where(c => c.Name == name).ToList(); Assert.AreEqual(1, clients.Count, "number of clients"); } }