gpt4 book ai didi

c# - ServiceStack.Redis.RedisClient 取消订阅函数挂起

转载 作者:可可西里 更新时间:2023-11-01 11:37:41 24 4
gpt4 key购买 nike

我尝试使用 RedisPubSubServer 但它不适用于关键通知,因为我需要订阅由模式指定的 channel 。所以我创建了自己的解决方案:

public class RedisKeySubscriber
{
public string Host { get; private set; }
public int Port { get; private set; }

private readonly Dictionary<string, IRedisSubscription> _subscriptions;

public RedisKeySubscriber(string host, int port)
{
Host = host;
Port = port;
_subscriptions = new Dictionary<string, IRedisSubscription>();
}

public void Subscribe(string key, Action<string, string> handler)
{
lock (_subscriptions)
{
IRedisSubscription subscription;
if (_subscriptions.TryGetValue(key, out subscription))
return;

// FIXME this might potentially create too many threads
var client = new ServiceStack.Redis.RedisClient(Host, Port);
subscription = client.CreateSubscription();
subscription.OnMessage = handler;
_subscriptions.Add(key, subscription);
new Thread(() => subscription.SubscribeToChannelsMatching(key)).Start();
}
}

public void Unsubscribe(string key)
{
lock (_subscriptions)
{
IRedisSubscription subscription;
if (!_subscriptions.TryGetValue(key, out subscription))
return;
subscription.UnSubscribeFromChannelsMatching(key);
_subscriptions.Remove(key);
}
}

public void UnsubscribeAll()
{
lock (_subscriptions)
{
foreach (var subscription in _subscriptions)
subscription.Value.UnSubscribeFromChannelsMatching(subscription.Key);
_subscriptions.Clear();
}
}

public void Dispose()
{
UnsubscribeAll();
}
}

我意识到这卡在 PSUBSCRIBE 命令上。其他一切都很好。有什么想法吗?

最佳答案

我使用额外的“控制” channel 解决了这个问题。因此,处理程序既订阅了与“key”匹配的 channel ,也订阅了像“CHCTRL:guid”这样的 channel ,其中 guid 是由类实例创建的特定 GUID。取消订阅函数在 CHCTRL:guid 上发布一条消息,处理程序可以理解是时候取消订阅了,因为消息不是来自“ key ”。

替代和建议的解决方案是不使用 ServiceStack.Redis。

关于c# - ServiceStack.Redis.RedisClient 取消订阅函数挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30037721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com