- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
尝试通过 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();"如果我有一堆类方法,那么我必须在每个方法中重复代码吗?
最佳答案
您不应该拥有 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/
我有一个关于 Redis Pubsub 的练习,如下所示: 如果发布者发布消息但订阅者没有收到服务器崩溃。订阅者如何在重启服务器时收到该消息? 请帮帮我,谢谢! 最佳答案 在这种情况下,消息将永远消失
我们正在使用 Service Stack 的 RedisClient 的 BlockingDequeue 来保存一些数据,直到它可以被处理。调用代码看起来像 using (var client =
我有一个 Redis 服务器和多个 Redis 客户端。每个 Redis 客户端都是一个 WebSocket+HTTP 服务器,其中包括管理 WebSocket 连接。这些 WebSocket+HTT
我有多个 Redis 实例。我使用不同的端口创建了一个集群。现在我想将数据从预先存在的 redis 实例传输到集群。我知道如何将数据从一个实例传输到集群,但是当实例多于一个时,我无法做到这一点。 最佳
配置:三个redis集群分区,跨三组一主一从。当 Master 宕机时,Lettuce 会立即检测到中断并开始重试。但是,Lettuce 没有检测到关联的 slave 已经将自己提升为 master
我想根据从指定集合中检索这些键来删除 Redis 键(及其数据集),例如: HMSET id:1 password 123 category milk HMSET id:2 password 456
我正在编写一个机器人(其中包含要禁用的命令列表),用于监视 Redis。它通过执行禁用命令,例如 (rename-command ZADD "")当我重新启动我的机器人时,如果要禁用的命令列表发生变化
我的任务是为大量听众使用发布/订阅。这是来自 docs 的订阅的简化示例: r = redis.StrictRedis(...) p = r.pubsub() p.subscribe('my-firs
我一直在阅读有关使用 Redis 哨兵进行故障转移的内容。我打算有1个master+1个slave,如果master宕机超过1分钟,就把slave变成master。我知道这在 Sentinel 中是
与仅使用常规 Redis 和创建分片相比,使用 Redis 集群有哪些优势? 在我看来,Redis Cluster 更注重数据安全(让主从架构解决故障)。 最佳答案 我认为当您需要在不丢失任何数据的情
由于 Redis 以被动和主动方式使 key 过期, 有没有办法得到一个 key ,即使它的过期时间已过 (但 在 Redis 中仍然存在 )? 最佳答案 DEBUG OBJECT myKey 将返回
我想用redis lua来实现monitor命令,而不是redis-cli monitor。但我不知道怎么办。 redis.call('monitor') 不起作用。 最佳答案 您不能从 Redis
我读过 https://github.com/redisson/redisson 我发现有几个 Redis 复制设置(包括对 AWS ElastiCache 和 Azure Redis 缓存的支持)
Microsoft.AspNet.SignalR.Redis 和 StackExchange.Redis.Extensions.Core 在同一个项目中使用。前者需要StackExchange.Red
1. 认识 Redis Redis(Remote Dictionary Server)远程词典服务器,是一个基于内存的键值对型 NoSQL 数据库。 特征: 键值(key-value)型,value
1. Redis 数据结构介绍 Redis 是一个 key-value 的数据库,key 一般是 String 类型,但 value 类型多种多样,下面就举了几个例子: value 类型 示例 Str
1. 什么是缓存 缓存(Cache) 就是数据交换的缓冲区,是存贮数据的临时地方,一般读写性能较高。 缓存的作用: 降低后端负载 提高读写效率,降低响应时间 缓存的成本: 数据一致性成本 代码维护成本
我有一份记录 list 。对于我的每条记录,我都需要进行一些繁重的计算,因为我要在Redis中创建反向索引。为了达到到达记录,需要在管道中执行多个redis命令(sadd为100 s + set为1
我有一个三节点Redis和3节点哨兵,一切正常,所有主服务器和从属服务器都经过验证,并且哨兵配置文件已与所有Redis和哨兵节点一起更新,但是问题是当Redis主服务器关闭并且哨兵希望选举失败者时再次
我正在尝试计算Redis中存储的消息之间的响应时间。但是我不知道该怎么做。 首先,我必须像这样存储chat_messages的时间流 ZADD conversation:CONVERSATION_ID
我是一名优秀的程序员,十分优秀!