之前已经写过一篇redis公共方法的使用(https://www.cnblogs.com/jhy55/p/7681626.html),可是发现在高并发的时候出现 Unknown reply on integer response: 43OK 这样子的错误

ServiceStack.Redis.RedisResponseException: Unknown reply on integer response: 43PONG, sPort: , LastCommand: EXISTS EX:AnKey:Cmp6
at ServiceStack.Redis.RedisNativeClient.CreateResponseError(String error)
at ServiceStack.Redis.RedisNativeClient.ReadLong()
at ServiceStack.Redis.RedisNativeClient.SendExpectLong(Byte[][] cmdWithBinaryArgs)
at ServiceStack.Redis.RedisNativeClient.Exists(String key)
at Redis.Documentos.RedisBaseType.Exists(String key)

这个错误消息表示相同的Redis客户端实例正在多个线程之间共享

错误详细解释 https://stackoverflow.com/questions/36436212/unexpected-reply-on-high-volume-scenario-using-servicestack-redis

如果有不足的地方,请指正

用到第三方dll NServiceKit.Redis ,可以在Nuget中应用

    public class RedisUtil
{
#region 字段
/// <summary>
/// Redis服务器地址
/// </summary>
private static string _host; /// <summary>
/// 端口
/// </summary>
private static int _port; /// <summary>
/// 密码
/// </summary>
private static string _password; /// <summary>
/// 过期时间
/// </summary>
private static DateTime _timeout; #endregion #region 属性
/// <summary>
/// Redis服务器地址
/// </summary>
public static string Host
{
get { return _host = ConfigurationManager.AppSettings["RedisIp"]; }
} /// <summary>
/// 端口
/// </summary>
public static int Port
{
get { return _port = Int32.Parse(ConfigurationManager.AppSettings["RedisPort"]); }
}
/// <summary>
/// 密码
/// </summary>
public static string Password
{
get { return _password = ConfigurationManager.AppSettings["RedisPassWord"]; }
} public static bool IsLock
{
get { return Soholife.Common.CommonUtils.GetIntValue(ConfigurationManager.AppSettings["RedisIsLock"], ) == ? false : true; }
} public static int TimeOutDay = Int32.Parse(ConfigurationManager.AppSettings["RedisTimeDayOut"]); /// <summary>
/// 过期时间
/// </summary>
public static DateTime Timeout
{
get { return _timeout = DateTime.Now.AddDays(TimeOutDay); }
}
#endregion //private static object _locker = new object(); static PooledRedisClientManager prcm = null; /// <summary>
/// 无参构造
/// </summary>
static RedisUtil()
{
//ip:port前面加上@用来表示密码,比如password@ip:port
string sFromstr = "{0}@{1}:{2}";
prcm = CreateManager(new string[] { string.Format(sFromstr, Password, Host, Port) }, new string[] { string.Format(sFromstr, Password, Host, Port) }, ChangeDb); } /// <summary>
/// 创建链接池管理对象
/// </summary>
public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts, long initialDB)
{
prcm = new PooledRedisClientManager(readWriteHosts, readOnlyHosts,
new RedisClientManagerConfig
{
MaxWritePoolSize = readWriteHosts.Length * ,
MaxReadPoolSize = readOnlyHosts.Length * ,
AutoStart = true,
}, initialDB, , ); prcm.IdleTimeOutSecs = ; return prcm;
} /// <summary>
/// 变动的DB
/// </summary>
private static long ChangeDb { get { return Soholife.Common.CommonUtils.GetIntValue(ConfigurationManager.AppSettings["ChangeDb"]); } } /// <summary>
/// 从Redis中读取日志记录列表
/// </summary>
/// <typeparam name="T">数据结构类型</typeparam>
/// <param name="sKey">关键字</param>
/// <returns></returns>
public static List<T> ReaderEnqueueList<T>(string sKey)
{
using (var redis = prcm.GetClient() as RedisClient)
{
var OrderStepLogList = redis.As<T>();
var fromList = OrderStepLogList.Lists[sKey];
List<T> oList = OrderStepLogList.GetAllItemsFromList(fromList);
if (oList != null)
{
OrderStepLogList.RemoveAllFromList(fromList);
}
return oList;
}
} /// <summary>
/// 加入具有数据结构的消息队列中
/// </summary>
/// <typeparam name="T">数据结构类型</typeparam>
/// <param name="t">数据对象</param>
/// <param name="sKey">关键字</param>
public static void AddEnqueue<T>(T t, string sKey)
{
using (var redis = prcm.GetClient() as RedisClient)
{
var OrderStepLogList = redis.As<T>();
//加入具有数据结构的消息队列中
OrderStepLogList.EnqueueItemOnList(OrderStepLogList.Lists[sKey], t);
}
} #region 外部调用方法 /// <summary>
/// 设置缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <param name="timeOut">过期时间</param>
/// <returns></returns>
public static bool Set<T>(string key, T t)
{
if (IsLock) return false; using (var redis = prcm.GetClient() as RedisClient)
{
return redis.Set(key, t, Timeout);
}
} /// <summary>
/// 设置缓存
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <param name="timeOut">过期时间</param>
/// <returns></returns>
public static bool Set<T>(string key, T t, DateTime time)
{
if (IsLock) return false;
using (var redis = prcm.GetClient() as RedisClient)
{
return redis.Set(key, t, time);
}
}
/// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key)
{
if (IsLock) return default(T);
using (var redis = prcm.GetClient() as RedisClient)
{
if (redis.Exists(key) > )
return redis.Get<T>(key);
else
return default(T);
}
} /// <summary>
/// 判断是否存在某个key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool IsExist(string key)
{
if (IsLock) return false;
using (var redis = prcm.GetClient() as RedisClient)
{
byte[] buffer = redis.Get(key);
if (buffer.Length > )
return true;
else
return false;
}
} /// <summary>
/// 获取Value的byte的长度
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static int GetValueLength(string key)
{
if (IsLock) return ;
using (var redis = prcm.GetClient() as RedisClient)
{
return redis.Get(key).Length;
}
} /// <summary>
/// 移除指定的Key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Remove(string key)
{
if (IsLock) return false; using (var redis = prcm.GetClient() as RedisClient)
{
return redis.Remove(key);
}
} /// <summary>
/// 累加(专用的哦)
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string Append(string sKey, string s)
{
byte[] t = Encoding.Default.GetBytes(s);
t = Encoding.Default.GetBytes(Convert.ToBase64String(t) + "^");
using (var redis = prcm.GetClient() as RedisClient)
{
return redis.Append(sKey, t).ToString();
}
} ///// <summary>
///// 获取所有的Keys
///// </summary>
///// <returns></returns>
//public static List<string> GetAllKeys()
//{
// return redis.GetAllKeys();
//} #endregion }

redis的常用公共方法(2)的更多相关文章

  1. redis的常用公共方法

    实用redis已经有一段时间了,今天刚好有空记录一下所用到的方法,欢迎指正 首先我封装了一些字段信息 #region 字段 /// <summary> /// Redis服务器地址 /// ...

  2. iOS常用公共方法

      iOS常用公共方法 字数2917 阅读3070 评论45 喜欢236 1. 获取磁盘总空间大小 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat si ...

  3. iOS 常用公共方法

    iOS常用公共方法 1. 获取磁盘总空间大小 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat size = 0.0; NSError *error; N ...

  4. JS常用公共方法封装

    _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||/ ...

  5. js 常用公共方法

    1.判断是否为空 function isNull(arg1) { return !arg1 && arg1!==0 && typeof arg1!=="boo ...

  6. J2EE项目开发中常用到的公共方法

    在项目IDCM中涉及到多种工单,包括有:服务器|网络设备上下架工单.服务器|网络设备重启工单.服务器光纤网线更换工单.网络设备撤线布线工单.服务器|网络设备替换工单.服务器|网络设备RMA工单.通用原 ...

  7. web开发过程中经常用到的一些公共方法及操作

    进化成为程序猿也有段岁月了,所谓的经验,广度还是依旧,只不过是对于某种功能有了多种实现方式的想法.每天依旧不厌其烦的敲打着代码,每一行代码的回车似乎都有一种似曾相识的感觉.于是乎:粘贴复制,再粘贴再复 ...

  8. 4-4 Redis 的常用配置

    2016-12-22 15:30:43 本篇文章属于Redis 系列第四篇文章:Redis 配置文件介绍 该系列文章链接 NoSQL 数据库简介 Redis的安装及及一些杂项基础知识 Redis 的常 ...

  9. Python中高级变量类型(列表,元组,字典,字符串,公共方法...)

    高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...

随机推荐

  1. Frog and Portal(思维好题)

    Frog and Portal https://hihocoder.com/problemset/problem/1873 时间限制:1000ms 单点时限:1000ms 内存限制:512MB 描述 ...

  2. Appium原理初步--Android自动化测试学习历程

    章节:自动化基础篇——Appium原理初步(第七讲) 本期关键词: Appium.跨语言跨平台.Bootstrap 主要讲解内容及笔记: 一.what is appium 一种封装了uiautomat ...

  3. 解读超轻量级DI容器-Guice与Spring框架的区别【转载】

    依赖注入,DI(Dependency Injection),它的作用自然不必多说,提及DI容器,例如spring,picoContainer,EJB容器等等,近日,google诞生了更轻巧的DI容器… ...

  4. Qt中使用python--Hello Python!

    step1:install Python (version 2.7 or higher): step2:The configuration is as follows: 1.create qt con ...

  5. 三、oracle 用户管理一

    三.oracle 用户管理一 一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 ide ...

  6. Canvas游戏计算机图形教程

    TechbrooD   主站 WOW 登录   注册 0首页 1简介 1.1WWW 技术变迁和生态 1.2WWW 学习建议 1.3WWW 互联网基础知识 1.4WWW Web 1.5 WWW Web ...

  7. maven使用感受

    第一次接触的时候,什么都不懂,感觉好复杂. 后来系统地看了一个使用教程: 简单评价一下: 自动帮我们下载jar架包,还有就是可以执行命令自己部署到远程服务器上面去. 缺点: 学习成本.一般人不了解.第 ...

  8. jQuery使用大全

    我的程序人生 提供基于Lesktop的IM二次开发,联系QQ:76159179 CnBlogs Home New Post Contact Admin Rss Posts - 476  Article ...

  9. Devexpress VCL Build v2013 vol 13.2.4 发布

    不说了,自己看吧. What's New in 13.2.4 (VCL Product Line)   New Major Features in 13.2 What's New in VCL Pro ...

  10. 2018.10.18 poj2187Beauty Contest(旋转卡壳)

    传送门 旋转卡壳板子题. 就是求凸包上最远点对. 直接上双指针维护旋转卡壳就行了. 注意要时刻更新最大值. 代码: #include<iostream> #include<cstdi ...