我在C#/ ASP.NET 4应用程序中使用Booksleeve库.目前,RedisConnection对象是我的MonoLink类中的静态对象.我应该保持这个连接打开,还是应该在每次查询/事务后打开/关闭它(正如我现在所做的那样)?只是略显困惑.这就是我现在使用它的方式:
public static MonoLink CreateMonolink(string URL)
{
redis.Open();
var transaction = redis.CreateTransaction();
string Key = null;
try
{
var IncrementTask = transaction.Strings.Increment(0, "nextmonolink");
if (!IncrementTask.Wait(5000))
{
transaction.Discard();
throw new System.TimeoutException("Monolink index increment timed out.");
}
// Increment complete
Key = string.Format("monolink:{0}", IncrementTask.Result);
var AddLinkTask = transaction.Strings.Set(0, Key, URL);
if (!AddLinkTask.Wait(5000))
{
transaction.Discard();
throw new System.TimeoutException("Add monolink creation timed out.");
}
// Run the transaction
var ExecTransaction = transaction.Execute();
if (!ExecTransaction.Wait(5000))
{
throw new System.TimeoutException("Add monolink transaction timed out."); …Run Code Online (Sandbox Code Playgroud) 我正在使用Redis pubsub通道将工作进程池中的消息发送到我的ASP.NET应用程序.收到消息后,我的应用程序将消息转发到带有SignalR的客户端浏览器.
我找到了这个解决方案来维护与Redis的开放连接,但它在重新创建连接时没有考虑订阅.
我正在处理Global.asax文件中的Redis pubsub消息:
public class Application : HttpApplication
{
protected void Application_Start()
{
var gateway = Resolve<RedisConnectionGateway>();
var connection = gateway.GetConnection();
var channel = connection.GetOpenSubscriberChannel();
channel.PatternSubscribe("workers:job-done:*", OnExecutionCompleted);
}
/// <summary>
/// Handle messages received from workers through Redis.</summary>
private static void OnExecutionCompleted(string key, byte[] message)
{
/* forwarded the response to the client that requested it */
}
}
Run Code Online (Sandbox Code Playgroud)
当前RedisConnection因任何原因关闭时会发生此问题.最简单的解决方案是RedisConnectionGateway在重置连接时从类中触发事件,并使用新的重新订阅RedisSubscriberChannel.但是,在重置连接时发布到通道的任何消息都将丢失.
是否有任何推荐的方法来处理这种情况?
我最近开始在新的Azure VM上托管我的一个侧面项目.该应用程序使用Redis作为内存缓存.在我的本地环境中,一切都运行良好,但现在我已经将代码移动到Azure了,我看到一些奇怪的例外来自Booksleeve.
当应用程序首次启动时,一切正常.但是,在大约5-10分钟的不活动之后,对应用程序的下一个请求会遇到网络异常(我现在正在工作,并且没有确切的错误消息,所以当我回到家时我会发布它们人们认为它们与讨论密切相关)这导致内部MessageQueue关闭,这导致每个后续的Enqueue()抛出异常("队列关闭").
所以经过一些谷歌搜索我找到了这样的帖子:使用BookSleeve维护一个关于DIY连接管理器的开放Redis连接.如果这是最好的行动方案,我当然可以实施类似的东西.
所以,问题:
conn.SetKeepAlive()方法,但我尝试了许多不同的值,似乎没有任何区别.还有更多的东西,还是我在错误的树上吠叫?就像我说的那样,如果Redis连接在不活动后死亡是不寻常的,那么当我回到家时,我会从我的日志中发布堆栈跟踪和异常.
谢谢!
更新 Didier在评论中指出,这可能与Azure使用的负载balanacer有关:http://blogs.msdn.com/b/avkashchauhan/archive/2011/11/12/windows-azure-load-balancer-超时details.aspx
假设是这种情况,那么实现可以解决这个愚蠢问题的连接管理器的最佳方法是什么.我假设我不应该为每个工作单元创建一个连接吗?
我使用RedisConnection Set方法设置字节数组但是如何获取数据?get返回一个包装的字节数组?
链接:
这有效,但感觉不对:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookSleeve;
using ProtoBuf;
using System.IO;
namespace RedisTest001
{
[ProtoContract, Serializable]
public class Price
{
private string _ticker;
private double _value;
public Price()
{
}
public Price(string ticker, double value)
{
_ticker = ticker;
_value = value;
}
[ProtoMember(1)]
public string Ticker
{
get { return _ticker; }
set { _ticker = value; }
}
[ProtoMember(2)]
public double Value
{
get { return _value; }
set …Run Code Online (Sandbox Code Playgroud)