gpt4 book ai didi

redis - ServiceStack.Redis 无法读取传输 - BasicRedisClientManager

转载 作者:IT王子 更新时间:2023-10-29 06:04:00 25 4
gpt4 key购买 nike

尝试通过 ServiceStack.Redis 读取 Redis 列表时,我间歇性地收到以下错误:“无法从传输连接读取数据:已建立的连接已被主机中的软件中止”。我想知道我关于如何使用 ServiceStack 可靠地连接和池连接到 Redis 的整个概念是否错误。这是我使用密封类和单例模式进行连接的代码:

public sealed class RedisClientBase
{
public BasicRedisClientManager Redis;
private static readonly RedisClientBase instance = new RedisClientBase();

private RedisClientBase()
{
Redis = new BasicRedisClientManager("mypassword@localhost:6379");
}

public static RedisClientBase Instance
{
get
{
return instance;
}
}
}

然后我实例化另一个使用单例的类:

public class RedisBarSetData
{
private static RedisClient Redis;
protected IRedisTypedClient<BarSet> redisBarSetClient;
protected string instrument_key;

public RedisBarSetData()
{
Redis = (RedisClient)RedisClientBase.Instance.Redis.GetClient();
redisBarSetClient = Redis.As<BarSet>();
}

~RedisBarSetData()
{
if (Redis != null)
Redis.Dispose();
}

public List<BarSet> getData(BarSets data)
{
setKeys(data); // instrument_key is set in here
var redisBarSetClientList = redisBarSetClient.Lists[instrument_key];
List<BarSet> barSetData;

barSetData = redisBarSetClientList.GetAll(); // <-- exception here (sometimes)
return(barSetData);
}
}

这又被实例化并从“服务”DTO 回调中调用:

public class JmaSetsService : Service
{
public object Get(JmaSets request)
{
RedisBarSetData barSetData = new RedisBarSetData();
BarSets barSets = new BarSets(request);
barSetList = barSetData.getData(barSets);
return barSetList;
}
}

然后我使用“ postman ”向这条路线发帖。大多数点击“发送”都会返回数据。有些以异常(exception)结束。异常是在尝试从代码中指示的 redis 中读取时,注释为“<-- exception here”。现在还有一点是,我最近通过设置配置文件将我的 redis 配置为使用密码。我提到那是因为我不记得以前有过这个问题,但这也可能不相关,不知道。

就释放 redis 连接而言,我的想法是当 RedisBarSetData() 超出范围时我的析构函数调用 Redis.Dispose。这是处理它的可靠方法还是有更好的方法。我见过有人在获取池客户端时使用“使用”语句,但是我有很多“使用”语句而不是只在类中的一个地方调用:“Redis = (RedisClient)RedisClientBase.Instance.Redis .GetClient();"如果我有一堆类方法,那么我必须在每个方法中重复代码吗?

  • 当我说“我不记得以前遇到过这个问题”时,我正在为许多工作 DTO 使用这种模式。不确定为什么现在失败了?

最佳答案

您不应该拥有 RedisClient 的任何单例实例或 IRedisTypedClient<BarSet>两者都封装了一个非线程安全的 Redis TCP 连接。您可以改为持有 IRedisClientsManager 的单例实例- 这是它提供线程安全的 Redis 客户端工厂(如数据库连接池)的目的。

如果您还使用 ServiceStack 服务,则在 ServiceStack 的 IOC 中注册依赖项会更容易,因此 IRedisClientsManager可以像任何其他依赖项一样注入(inject),例如 AppHost.Configure() :

container.Register<IRedisClientsManager>(c =>
new BasicRedisClientManager("mypassword@localhost:6379"));

这将允许您使用 base.Redis您的 ServiceStack 服务中的 RedisClient 属性,例如:

public class JmaSetsService : Service
{
public object Get(JmaSets request)
{
var redisBarSets = base.Redis.As<BarSet>();
return redisBarSets.Lists[instument_key].GetAll();
}
}

如果您使用 base.Redis您不必显式处理 RedisClient,因为它已经是 automatically disposed by the Service ,即:

public class Service
{
...

public virtual void Dispose()
{
if (redis != null)
redis.Dispose();
...
}
}

你也可以注入(inject)IRedisClientsManager像使用公共(public)属性或构造函数参数的任何其他依赖项一样进入您自己的类,例如:

public class RedisBarSetData
{
public virtual IRedisClientsManager RedisManager { get; set; }

private IRedisClient redis;
public virtual IRedisClient Redis
{
get { return redis ?? (redis = RedisManager.GetClient()); }
}

public override void Dispose()
{
if (redis != null)
redis.Dispose();
}

public List<BarSet> getData(BarSets data)
{
setKeys(data); // instrument_key is set in here
return Redis.As<BarSet>().Lists[instrument_key].GetAll();
}
}

然后您可以在 ServiceStack 的 IOC 中注册并 Autowiring :

container.RegisterAutoWired<RedisBarSetData>();

这将允许您将其用作服务中的依赖项:

public class JmaSetsService : Service
{
public RedisBarSetData RedisBarSetData { get; set; }

public object Get(JmaSets request)
{
return RedisBarSetData.getData(new BarSets(request));
}
}

An alternative to creating your own base class is to inherit from the pre-existing LogicBase base class, which already has IRedisClientsManager property and above boilerplate.

关于redis - ServiceStack.Redis 无法读取传输 - BasicRedisClientManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738427/

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