当开始用    var result=new RedisClient("127.0.0.1",6379,1"1111");  这个helper ,后面有并发之后就一直提示超时问题。

后面改为连接池(集群的方式)

/// <summary>
/// 连接客户端管理
/// </summary>
private static PooledRedisClientManager prcm;

/// <summary>
/// 创建链接池管理对象
/// </summary>
public static PooledRedisClientManager CreateManager()
{
string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");

return new PooledRedisClientManager(writeServerList, readServerList,
new RedisClientManagerConfig
{
MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
DefaultDb = redisConfigInfo.InitalDb,
AutoStart = redisConfigInfo.AutoStart,
}); //, redisConfigInfo.InitalDb
}

/// <summary>
/// 客户端缓存操作对象
/// </summary>
public static IRedisClient GetClient()
{
if (OpenRedis)
{
if (prcm == null)
{
 prcm = CreateManager();
}

return prcm.GetClient();
}
return null;
}

但发现用同一个还是连接池并发大还是有问题,从日志上面看

Redis Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use.
异常信息:System.TimeoutException: Redis Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use.

could not connect to redis Instance at 127.0.0.1:6379
异常信息:ServiceStack.Redis.RedisException: could not connect to redis Instance at 127.0.0.1:6379 ---> System.Net.Sockets.SocketException: 在一个非套接字上尝试了一个操作。 127.0.0.1:6379

后面在网上查找的时候看到使用using 的方法。 跟连接数据库一样的道理,还有开多线程会报错,要加local锁

RedisConfigInfo类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace Cache.Redis
{
/// <summary>
/// 获取redis配置的类
/// </summary>
public sealed class RedisConfigInfo:ConfigurationSection
{
public static RedisConfigInfo GetConfig()
{
var section = (RedisConfigInfo)System.Configuration.ConfigurationManager.GetSection("RedisConfig");
return section;
}
public static RedisConfigInfo GetConfig(string sectionName)
{
var section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
if (section == null)
{
throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
}
return section;
}

/// <summary>
/// 可写的Redis链接地址
/// </summary>
[ConfigurationProperty("WriteServerList", IsRequired = false)]
public string WriteServerList
{
get
{
return (string)base["WriteServerList"];
}

set {
base["WriteServerList"] = value;
}
}

/// <summary>
/// 可读的Redis链接地址,它一般由多个服务器组件,一般称为从服务器(slave),各个服务器之间用逗号分开
/// </summary>
[ConfigurationProperty("ReadServerList", IsRequired = false)]
public string ReadServerList
{
get
{
return (string)base["ReadServerList"];
}

set
{
base["ReadServerList"] = value;
}
}

/// <summary>
/// 最大写链接数
/// </summary>
[ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)]
public int MaxWritePoolSize
{
get
{
return (int)base["MaxWritePoolSize"];
}

set
{
base["MaxWritePoolSize"] = value;
}
}

/// <summary>
/// 初始化哪个库
/// </summary>
[ConfigurationProperty("InitalDb", IsRequired = false)]
public long InitalDb
{
get
{
return (long)base["InitalDb"];
}

set
{
base["InitalDb"] = value;
}
}

/// <summary>
/// 最大写链接数
/// </summary>
[ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)]
public int MaxReadPoolSize
{
get
{
return (int)base["MaxReadPoolSize"];
}

set
{
base["MaxReadPoolSize"] = value;
}
}

/// <summary>
/// 自动重启
/// </summary>
[ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)]
public bool AutoStart
{
get
{
return (bool)base["AutoStart"];
}

set
{
base["AutoStart"] = value;
}
}

/// <summary>
/// 本地缓存到期时间,单位:秒
/// </summary>
[ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = 36000)]
public int LocalCacheTime
{
get
{
return (int)base["LocalCacheTime"];
}

set
{
base["LocalCacheTime"] = value;
}
}

/// <summary>
/// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
/// </summary>
[ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)]
public bool RecordeLog
{
get
{
return (bool)base["RecordeLog"];
}

set
{
base["RecordeLog"] = value;
}
}

/// <summary>
/// 超时时间
/// </summary>
[ConfigurationProperty("PoolTimeOutSeconds", IsRequired = false, DefaultValue = 5000)]
public int PoolTimeOutSeconds
{
get
{
return (int)base["PoolTimeOutSeconds"];
}

set
{
base["PoolTimeOutSeconds"] = value;
}
}

}
}

RedisOperatorBase类

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Cache.Redis
{
/// <summary>
/// Redis回收类
/// </summary>
public abstract class RedisOperatorBase : IDisposable
{
protected static object lockobj = new object();

private bool _disposed = false;
protected RedisOperatorBase()
{
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
}
}
this._disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 保存数据DB文件到硬盘
/// </summary>
public void Save()
{
}
/// <summary>
/// 异步保存数据DB文件到硬盘
/// </summary>
public void SaveAsync()
{
}

}
}

RedisManager  redis管理的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;

namespace Cache.Redis
{
/// <summary>
/// redis管理的类
/// </summary>
public class RedisManager
{
public static bool OpenRedis = System.Configuration.ConfigurationManager.AppSettings["OpenRedis"] == null ? false : Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["OpenRedis"]);
/// <summary>
/// redis配置文件信息
/// </summary>
private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();

/// <summary>
/// 创建链接池管理对象
/// </summary>
public static PooledRedisClientManager CreateManager()
{
string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");

return new PooledRedisClientManager(writeServerList, readServerList,
new RedisClientManagerConfig
{
MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
DefaultDb = redisConfigInfo.InitalDb,
AutoStart = redisConfigInfo.AutoStart,
}); //, redisConfigInfo.InitalDb
}

/// <summary>
/// 得到连接数组
/// </summary>
/// <param name="strSource"></param>
/// <param name="split"></param>
/// <returns></returns>

private static string[] SplitString(string strSource, string split)
{
return strSource.Split(split.ToArray());
}

}
}

Redis string get set的类

using ServiceStack.Redis;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Cache.Redis
{
/// <summary>
/// redis的string
/// </summary>
public class RedisStringOperator : RedisOperatorBase
{

public RedisStringOperator() : base() { }
/// <summary>
///redis的string 存值
/// </summary>
public bool Set<T>(string key, T t)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.Set(key, t);
}
}

}

/// <summary>
/// redis的string存值
/// </summary>
public bool Set<T>(string key, T t, DateTime expiresAt)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.Set(key, t, expiresAt);
}
}

//return Redis.Set(key, t, expiresAt);
}

/// <summary>
/// 存值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public bool Set(string key, string value)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.Set<string>(key, value);
}
}

}
/// <summary>
/// 移除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Remove(string key)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.Remove(key);
}
}
}
/// <summary>
/// 取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T Get<T>(string key)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return default(T);
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.Get<T>(key);
}
}

}

/// <summary>
/// 设置缓存过期
/// </summary>
public bool SetExpire(string key, DateTime datetime)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.ExpireEntryAt(key, datetime);
}
}
}

}
}

RedisHash的类

using ServiceStack.Text;
using System;
using System.Collections.Generic;

namespace Cache.Redis
{
public class RedisHashOperator : RedisOperatorBase
{
public RedisHashOperator() : base() { }
/// <summary>
/// 判断某个数据是否已经被缓存
/// </summary>
public bool ExistHashContainsEntry(string hashId, string key)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.HashContainsEntry(hashId, key);
}
}
}
/// <summary>
/// 存储数据到hash表
/// </summary>
public bool SetInHash<T>(string hashId, string key, T t)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
var value = JsonSerializer.SerializeToString<T>(t);
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.SetEntryInHash(hashId, key, value);
}
}
}

/// <summary>
/// 存储数据到hash表
/// </summary>
/// <param name="hashId"></param>
/// <param name="key"></param>
/// <param name="t"></param>
/// <returns></returns>
public bool SetInHash(string hashId, string key, string value)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.SetEntryInHash(hashId, key, value);
}
}
}

/// <summary>
/// 移除hash中的某值
/// </summary>
public bool RemoveFromHash(string hashId, string key)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return true;
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.RemoveEntryFromHash(hashId, key);
}
}
}
///// <summary>
///// 移除整个hash
///// </summary>
//public static bool Remove(string key)
//{
// return Redis.Remove(key);
//}

/// <summary>
/// 从hash表获取数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="hashId"></param>
/// <param name="key"></param>
/// <returns></returns>
public T GetFromHash<T>(string hashId, string key)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return default(T);
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
string value = redieclient.GetValueFromHash(hashId, key);
return JsonSerializer.DeserializeFromString<T>(value);
}
}

//string value = Redis.GetValueFromHash(hashId, key);
//return JsonSerializer.DeserializeFromString<T>(value);
}

/// <summary>
/// 从hash表获取数据
/// </summary>
/// <param name="hashId"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetFromHash(string hashId, string key)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return "";
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.GetValueFromHash(hashId, key);
}
}
}

/// <summary>
/// 获取整个hash的数据
/// </summary>
public static List<T> GetAllFromHash<T>(string hashId)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return new List<T>();
}
var result = new List<T>();
lock (lockobj)
{

using (var redieclient = RedisManager.CreateManager().GetClient())
{
var list = redieclient.GetHashValues(hashId);
if (list != null && list.Count > 0)
{
list.ForEach(x =>
{
var value = JsonSerializer.DeserializeFromString<T>(x);
result.Add(value);
});
}

}
}
return result;
}

/// <summary>
/// 根据hash获取keys
/// </summary>
/// <param name="hashId"></param>
/// <returns></returns>
public static List<string> GetHashKeys(string hashId)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return new List<string>();
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.GetHashKeys(hashId);
}
}
}

/// <summary>
/// 获取整个hash的数据
/// </summary>
public static List<string> GetAllFromHash(string hashId)
{
if (!Cache.Redis.RedisManager.OpenRedis)
{
return new List<string>();
}
lock (lockobj)
{
using (var redieclient = RedisManager.CreateManager().GetClient())
{
return redieclient.GetHashValues(hashId);
}
}

}

}
}

比如string的set方法

<configSections>
<!--redis缓存-->
<section name="RedisConfig" type="Cache.Redis.RedisConfigInfo,Cache" />
</configSections>
<!--ServiceStack.Redis 4.0以后的版本的格式 password@IP:port?&db=1&ssl=true&client=aaa&password=123&namespaceprefix=vvv&connecttimeout=10&sendtimeout=10&receivetimeout=10&retrytimeout=10&idletimeout=10-->
<!--redis缓存 写入地址(如果没有密码则不用@以及@之前的值,如127.0.0.1:6379,有密码则51sole@127.0.0.1:6379) 读的地址 默认数据库是第几个 最大超时时间(秒) 最大写链接数 最大读的链接数 是否自动重启 缓存时间 是否写入日志 -->
<RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" InitalDb="3" PoolTimeOutSeconds="5000" MaxWritePoolSize="5" MaxReadPoolSize="5" AutoStart="true" LocalCacheTime="180" RecordeLog="false">
</RedisConfig>

string的 get 和set方法

//
// GET: /RedisTest/
public ActionResult Index()
{
Cache.Redis.RedisStringOperator redistring = new Cache.Redis.RedisStringOperator();
redistring.Set("test", 1);
return View();
}

public ActionResult GetRedisValue()
{
Cache.Redis.RedisStringOperator redistring = new Cache.Redis.RedisStringOperator();
redistring.Get<int>("test");
}

asp.net redis 实战的更多相关文章

  1. C# Redis实战

    转自  :http://blog.csdn.net/qiujialongjjj/article/details/16945569 一.初步准备 Redis 是一个开源的使用ANSI C 语言编写.支持 ...

  2. C# Redis实战(三)

    三.程序配置 在C# Redis实战(二)中我们安装好了Redis的系统服务,此时Redis服务已经运行. 现在我们需要让我们的程序能正确读取到Redis服务地址等一系列的配置信息,首先,需要在Web ...

  3. ASP.NET Core 实战:基于 Dapper 扩展你的数据访问方法

    一.前言 在非静态页面的项目开发中,必定会涉及到对于数据库的访问,最开始呢,我们使用 Ado.Net,通过编写 SQL 帮助类帮我们实现对于数据库的快速访问,后来,ORM(Object Relatio ...

  4. ASP.NET Core 实战:基于 Jwt Token 的权限控制全揭露

    一.前言 在涉及到后端项目的开发中,如何实现对于用户权限的管控是需要我们首先考虑的,在实际开发过程中,我们可能会运用一些已经成熟的解决方案帮助我们实现这一功能,而在 Grapefruit.VuCore ...

  5. ASP.NET Core 实战:将 .NET Core 2.0 项目升级到 .NET Core 2.1

    一.前言  最近一两个星期,加班,然后回去后弄自己的博客,把自己的电脑从 Windows 10 改到 Ubuntu 18.10 又弄回 Windows 10,原本计划的学习 Vue 中生命周期的相关知 ...

  6. Redis实战阅读笔记——开始

    Redis实战这本书,看完以后最大的不是redis本身的东西,而是作者面对实际问题的分析而给出的设计方案,可以看成NoSql设计的应用.个人从这方面收获很多,至于Redis本身的东西,这个就花一两个小 ...

  7. Redis实战阅读笔记——第一章

    Redis 实战 中文版 的20-21页看的人郁闷死了,最后看英文版才明白意思,哎,我理解能力差成这样了 其中,图 1-12 有错误,草,这个是英文版的错--应该是group:programming

  8. redis实战(01)_redis安装

    早就想对redis进行实战操作了,最近看了一些视频和参考书籍,总结总结一下,redis实战内容: 实战前先对redis做一个大概的认识: 现在开始安装redis了... redis的安装下载地址 ht ...

  9. ASP开发入门+实战电子书共50本 —下载目录

    小弟为大家整理50个ASP电子书籍,有入门,也有实战电子书,做成了一个下载目录,欢迎大家下载. 资源名称 资源地址 ASP.NET开发实战1200例_第I卷 http://down.51cto.com ...

随机推荐

  1. RabbitMQ PHP操作类,守护进程及相关测试数据

    封装类如下: <?php /* * amqp协议操作类,可以访问rabbitMQ * 需先安装php_amqp扩展 */ class RabbitMQCommand{ public $confi ...

  2. MVC + JQUERY + AJAX

  3. 和阿木聊Node.js

    npm:node.js官方库 cnpm:taobao维护的库: WebStorm:Node.js的开发工具,但是收费: seajs:还有一款交requirjs,前者是遵循amd规范(一次性参数中加载要 ...

  4. android SurfaceView绘制 重新学习--切图clipRect详解

    解释都在代码注释中: public class SampleView extends View { private Paint mPaint; private Path mPath; public S ...

  5. Codeforces Round #198 (Div. 2) —— C

    C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...

  6. 【HDU 1133】 Buy the Ticket (卡特兰数)

    Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...

  7. 【Xamarin 跨平台机制原理剖析】

    原文:[Xamarin 跨平台机制原理剖析] [看了请推荐,推荐满100后,将发补丁地址] Xamarin项目从喊口号到现在,好几个年头了,在内地没有火起来,原因无非有三,1.授权费贵 2.贵 3.原 ...

  8. Android的十六进制颜色值

    [转自]: http://blog.csdn.net/fxtxz2/article/details/7598256 颜色和不透明度 (alpha) 值以十六进制表示法表示.任何一种颜色的值范围都是 0 ...

  9. 使用android.view.TouchDelegate扩大View的触摸点击区域

    Android4.0设计规定的有效可触摸的UI元素标准是48dp,转化为一个物理尺寸约为9毫米.7~10毫米,这是一个用户手指能准确并且舒适触摸的区域. 如下图所示,你的UI元素可能小于48dp,图标 ...

  10. Properties vs. Attributes

    http://blogs.msdn.com/b/ericlippert/archive/2009/02/02/properties-vs-attributes.aspx Here is yet ano ...