我正在使用 Service Stack 连接到 Redis 并使用 SubPub 功能。
我应该保持 IRedisSubscription 和 IRedisClient 实例化吗?例如,我应该将其分配给类级别变量吗?
或者我可以简单地在 using 语句中限定它,服务堆栈将处理持久性?
也就是说,以下哪个例子是正确的:
公共类 RedisPubSub1 : IDisposable {
private static PooledRedisClientManager ClientPool = new PooledRedisClientManager("connectionString"); private IRedisSubscription _subscription; private IRedisClient _client; private Action<string, string> _publish; public event Action<string, string> Publish { add { _publish += value; } remove { _publish -= value; } } public RedisPubSub1() { Task.Factory.StartNew(() => { _client = ClientPool.GetClient(); _subscription = _client.CreateSubscription(); { _subscription.OnMessage = EnqueEvent; _subscription.SubscribeToChannels(new string[] { Channel }); } }); } private void EnqueEvent(string channel, string message) { if (_publish!= null) _publish(channel, message); } public void Dispose() { _subscription.Dispose(); _client.Dispose(); } } }
或者
公共类 RedisPubSub2 {
private static PooledRedisClientManager ClientPool = new PooledRedisClientManager("connectionString"); private Action<string, string> _publish; public event Action<string, string> Publish { add { _publish += value; } remove { _publish -= value; } } public RedisPubSub2() { Task.Factory.StartNew(() => { using(var _client = ClientPool.GetClient()) { using(_subscription = _client.CreateSubscription() { _subscription.OnMessage = EnqueEvent; _subscription.SubscribeToChannels(new string[] { Channel }); } } }); } private void EnqueEvent(string channel, string message) { if (_publish!= null) _publish(channel, message); } }