asp.net redis 实战

当开始用    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以后的版本的格式 [email protected]: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,有密码则[email protected]: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");
}

时间: 2024-10-24 18:29:56

asp.net redis 实战的相关文章

C# Redis实战(三)

三.程序配置 在C# Redis实战(二)中我们安装好了Redis的系统服务,此时Redis服务已经运行. 现在我们需要让我们的程序能正确读取到Redis服务地址等一系列的配置信息,首先,需要在Web.config文件中添加如下信息: [html] view plain copy <?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.mi

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

原文:ASP.NET Core 实战:将 .NET Core 2.0 项目升级到 .NET Core 2.1  一.前言  最近一两个星期,加班,然后回去后弄自己的博客,把自己的电脑从 Windows 10 改到 Ubuntu 18.10 又弄回 Windows 10,原本计划的学习 Vue 中生命周期的相关知识目前也没有任何的进展,嗯,罪过罪过.看了眼时间,11月也快要结束了,准备补上一篇如何将我们的 .NET Core 2.0 版本的程序升级到 .NET Core 2.1 版本,好歹也算多学

C# Redis实战(五)

五.删除数据 在C# Redis实战(四)中讲述了如何在Redis中写入key-value型数据,本篇将讲述如何删除Redis中数据. 1.void Delete(T entity);删除函数的运用 [csharp] view plain copy using (var redisClient = RedisManager.GetClient()) { var user = redisClient.GetTypedClient<User>(); var newUser = new User {

C# Redis实战(四)

四.写入数据 在C# Redis实战(三)中我们已经配置好了web.config程序,并且能通过C#代码来读取和管理以上配置信息. 接下来,就可以进行Redis的数据写入了.Redis中可以用Store和StoreAll分别保存单条和多条数据,C#中具体代码如下: 1.保存多条数据 [csharp] view plain copy protected void btnOpenDB_Click(object sender, EventArgs e) { //System.Diagnostics.P

C# Redis实战(二)

二.Redis服务 在C# Redis实战(一)中我将所有文件拷贝到了D盘redis文件夹下,其中redis-server.exe即为其服务端程序,双击即开始运行,如图 可以将此服务设置为windows系统服务,下载Redis服务安装软件,安装即可. 安装完成在服务中找到此服务,将其设置为自动延迟启动即可. 再回到redis文件夹下,找到redis-cli.exe文件,它就是Redis客户端程序.打开,输入:set qiujialong 123 即在Redis中插入了一条key为qiujialo

C# Redis实战(六)

六.查询数据 在C# Redis实战(五)中介绍了如何删除Redis中数据,本篇将继续介绍Redis中查询的写法. 1.使用Linq匹配关键字查询 [csharp] view plain copy using (var redisClient = RedisManager.GetClient()) { var user = redisClient.GetTypedClient<User>(); var userList = user.GetAll().Where(x => x.Job.P

Redis实战阅读笔记——开始

Redis实战这本书,看完以后最大的不是redis本身的东西,而是作者面对实际问题的分析而给出的设计方案,可以看成NoSql设计的应用.个人从这方面收获很多,至于Redis本身的东西,这个就花一两个小时就可以知道大致是怎么一回事了.具体API就直接查查就OK.关键是怎么用,这才是灵感创造所在,看看别人解决问题的思路,学习学习哈.

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

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

redis实战_01_yucong_redis基础

redis实战概要: redis简介 redis的五种数据类型: 2.1 String类型 String类型是包含很多种类型的特殊类型,并且是二进制安全的. 比如序列化的对象进行存储,比如一张图片进行二进制存储, 比如一个简单的字符串,数值等等. set和get方法: 设置值: set name yucong 取值 get name (说明 设置name多次会覆盖) 删除值:del name 使用setnx (not exist) name 如果不存在进行设置,存在就不需要进行设置了,返回0,