Unable to Connect: sPort: 0 C# ServiceStack.Redis 访问 redis

需求:  对数据库中的不断抓取的文章进行缓存,因此需要定时访问数据,写入缓存中

在捕获到的异常日志发现错误:Unable to Connect: sPort: 0

使用的访问方式是线程池的方式:PooledRedisClientManager

经过测试发现在并发访问redis服务的情况下出现该异常的概率比较高,

解决方法

第一点:要使用using(据说访问效率在高并发的时候会有影响,简单的测试过了确实是这样,不过现在的业务达不到高并发量,速度还是很快滴)

using (IRedisClient redisClient = Instance.GetClient())
            {
                T t = redisClient.Get<T>(key);
                if (t == null && func != null)
                {
                    t = func();
                    redisClient.Set<T>(key, t, DateTime.Now.AddSeconds(seconds));
                }
                return t;
            }  

第二点:设置ConnectTimeout属性(猜测单位应该是毫秒),要设置一个比较大的值,我设置为:1000 * 60 * 20  ,没错是20分钟,我发现设置小了还是不管用

做好这两点,经测试发现每秒钟200个并发量 毫无压力

附上封装的一个帮助类:

/// <summary>
    /// 数据缓存
    /// </summary>
    public class RedisHelper
    {
        public static PooledRedisClientManager instance;

        static RedisHelper()
        {
        }
        public static PooledRedisClientManager Instance
        {
            get
            {
                return instance;
            }
        }public static void InitClient(string redis_ip, int redis_port, string redis_pass)
        {
            instance = new PooledRedisClientManager(10000,
                                           10,
                                           new string[] { string.Format("{0}@{1}:{2}", redis_pass, redis_ip, redis_port) })
            {
                ConnectTimeout = 1000 * 60 * 20
            };
        }

        public static T Get<T>(string key, int seconds, Func<T> func)
        {
            using (IRedisClient redisClient = Instance.GetClient())
            {
                T t = redisClient.Get<T>(key);
                if (t == null && func != null)
                {
                    t = func();
                    redisClient.Set<T>(key, t, DateTime.Now.AddSeconds(seconds));
                }
                return t;
            }

        }
        public static T Get<T>(string key)
        {
            using (IRedisClient redisClient = Instance.GetReadOnlyClient())
            {
                T t = redisClient.Get<T>(key);
                return t;
            }

        }
        public static Boolean Set<T>(string key, int seconds, T t)
        {
            using (IRedisClient redisClient = Instance.GetClient())
            {
                return redisClient.Set<T>(key, t, DateTime.Now.AddSeconds(seconds));
            }
        }

        public static void ClearKey(string key)
        {
            using (IRedisClient redisClient = Instance.GetClient())
            {
                IEnumerable<string> keys = redisClient.GetAllKeys(); ;
                foreach (var item in keys)
                {
                    if (item.Contains(key))
                    {
                        redisClient.Remove(item);
                    }
                }
            }

        }
    }
时间: 2024-11-04 14:46:15

Unable to Connect: sPort: 0 C# ServiceStack.Redis 访问 redis的相关文章

[RabbitMQ]Error: unable to connect to node [email&#160;protected]: nodedown(CentOS7.0)

今天在搭建OpenStack的时候需要安装RabbitMQ,可是使用yum install rabbitmq-server安装之后,按照OpenStack官方提供的文档修改guest用户密码的时候却出现了如下错误: [[email protected] ~]# rabbitmqctl change_password guest Rabbit123 Changing password for user "guest" ... Error: unable to connect to nod

selenium,在Eclipse中打开fireFox浏览器报错:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms

1. 相信很多同学刚接触selenium时,在Eclipse中打开fireFox浏览器是报报错:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:...(异常代码太多此处省略) 在网上找了好多方法都是不能解决问题,结果是焦头烂额! 2.其实这是selenium的jar包和

配置Mysql Cluster时出错Unable to connect with connect string: nodeid=0,localhost:1186问题的解决

在执行 /etc/init.d/ndbd --initial出现下列错误 [[email protected] ~]# /etc/init.d/ndbd --initialUnable to connect with connect string: nodeid=0,localhost:1186Retrying every 5 seconds. Attempts left: 12 11 10 9 8 7 6 5 4 3 2 1, failed. 解决办法 1.查看/etc/my.cnf文件配置

Selenium2学习-038-firefox、webdriver版本不对称问题解决:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055

今天有个朋友在群里问,为何脚本运行不通过,其脚本操作步骤简单描述如下: 1.启动火狐浏览器 2.打开百度 3.查询框输入关键字 4.点击按钮[百度一下] 脚本挺简单的,其给出的应用报错信息如下所示: [TestNG] Running: C:\Users\jiwei.xu\AppData\Local\Temp\testng-eclipse--429788523\testng-customsuite.xml org.openqa.selenium.firefox.NotConnectedExcept

WebDriver:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms

今天尝试最新的webDriver与fireFox搭配: 运行代码时出现如下的问题,但是浏览器却可以打开: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewPro

使用vmware虚拟机安装virt-manager unable to connect to libvirt的处理办法

RHEL7.0在安装完成virt-manager后,启动libvirtd服务进程 [[email protected] Desktop]# systemctl enable libvirtd [[email protected] Desktop]# systemctl start libvirtd 在使用virt-manager命令启动虚拟机管理软件时,提示unable to connect to libvirt Unable to connect to libvirt. no connecti

The search request was unable to connect to the Search Service

使用Sharepoint进行搜索时,出现以下提示:The search request was unable to connect to the Search Service: 进入前端服务器,发现以下错误: Source: Office Server Search Event ID : 10040 Description: The last query machine has been taken out of rotation. Check previous event logs to de

全量恢复gprecoverseg -F出现Unable to connect to database时的相关分析及解决方法

之前有两位朋友碰到过在对greenplum的系统构架更改后,出现全量恢复gprecoverseg -F也无法正常运行的情况. 报错信息为Unable to connect to database. Retrying 1 gprecoverseg failed. (Reason='Unable to connect to database and start transaction') exiting... 有幸拷得一份虚拟机上的全部文件,对其进行分析. 发现其实出现这个问题只需要修改pg_cha

greenplum全量恢复gprecoverseg -F出现Unable to connect to database时的相关分析及解决方法

之前有两位朋友碰到过在对greenplum的系统构架更改后,出现全量恢复gprecoverseg -F也无法正常执行的情况. 报错信息为Unable to connect to database. Retrying 1 gprecoverseg failed. (Reason='Unable to connect to database and start transaction') exiting... 有幸拷得一份虚拟机上的所有文件.对其进行分析. 发现事实上出现这个问题仅仅须要改动pg_c