尝试通过 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
IRedisClientsManagerproperty and above boilerplate.
关于redis - ServiceStack.Redis 无法读取传输 - BasicRedisClientManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738427/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub