本文参考:https://www.cnblogs.com/knowledgesea/p/5032101.html
Net实现Redis操作
控制台App.Config或Web.Config配置节点信息
更多的配置信息查看https://blog.csdn.net/NoFind_Coder/article/details/103023332
<configSections>
<section name="RedisConfig" type="TestRedis.RedisConfig,TestRedis" />
</configSections>
<RedisConfig WriteServerConStr="127.0.0.1:6379" ReadServerConStr="127.0.0.1:6377" MaxWritePoolSize="10" AutoStart="true" />
1、新建RedisConfig类,读取config中的节点配置信息
namespace TestRedis
{
public class RedisConfig:ConfigurationSection
{
public static RedisConfig GetConfig()
{
return GetConfig("RedisConfig");
}
/// <summary>
/// 配置文件Redis链接字符
/// </summary>
/// <param name="sectionstring"></param>
/// <returns></returns>
public static RedisConfig GetConfig(string sectionname)
{
RedisConfig section=(RedisConfig)ConfigurationManager.GetSection(sectionname);
if (section==null)
{
throw new ConfigurationErrorsException("Section["+ sectionname + "] is not found");
}
return section;
}
/// <summary>
/// 可写的Redis链接地址
/// </summary>
///
[ConfigurationProperty("WriteServerConStr", DefaultValue ="",IsRequired =true,IsKey =false)]
public string WriteServerConStr
{
get
{
return (string)base["WriteServerConStr"];
}
set
{
base["WriteServerConStr"] = value;
}
}
/// <summary>
/// 可读的Redis链接地址
/// </summary>
///
[ConfigurationProperty("ReadServerConStr", DefaultValue = "", IsRequired = true, IsKey = false)]
public string ReadServerConStr
{
get
{
return (string)base["ReadServerConStr"];
}
set
{
base["ReadServerConStr"] = value;
}
}
[ConfigurationProperty("MaxWritePoolSize", DefaultValue = "", IsRequired = true, IsKey = false)]
public string MaxWritePoolSize
{
get
{
return (string)base["MaxWritePoolSize"];
}
set
{
base["MaxWritePoolSize"] = value;
}
}
/// <summary>
/// 自动重启
/// </summary>
[ConfigurationProperty("AutoStart", DefaultValue = "", IsRequired = true, IsKey = false)]
public string AutoStart
{
get
{
return (string)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;
}
}
}
}
2.新建RedisManager类,配置Redis链接,创建对象实例
public class RedisManager
{
/// <summary>
/// redis配置文件信息
/// </summary>
private static RedisConfig RedisConfig = RedisConfig.GetConfig();
private static PooledRedisClientManager prcm;
/// <summary>
/// 静态构造方法,初始化链接池管理对象
/// </summary>
static RedisManager()
{
CreateManager();
}
/// <summary>
/// 创建链接池管理对象
/// </summary>
private static void CreateManager()
{
string[] WriteServerConStr = SplitString(RedisConfig.WriteServerConStr, ",");
string[] ReadServerConStr = SplitString(RedisConfig.ReadServerConStr, ",");
prcm = new PooledRedisClientManager(ReadServerConStr, WriteServerConStr,
new RedisClientManagerConfig
{
MaxWritePoolSize = RedisConfig.MaxWritePoolSize,
MaxReadPoolSize = RedisConfig.MaxReadPoolSize,
AutoStart = RedisConfig.AutoStart,
});
}
private static string[] SplitString(string strSource, string split)
{
return strSource.Split(split.ToArray());
}
/// <summary>
/// 客户端缓存操作对象
/// </summary>
public static IRedisClient GetClient()
{
if (prcm == null)
CreateManager();
return prcm.GetClient();
}
}
3、IRedisClient为操作Redis的接口,是.Net操作Redis的主要类库,这里我们把它接入
/// <summary>
/// DoRedisBase类,是redis操作的基类,继承自IDisposable接口,主要用于释放内存
/// </summary>
public abstract class DoRedisBase: IDisposable
{
public static IRedisClient Core {
get; private set; }
private bool _disposed = false;
static RedisBase()
{
Core = RedisManager.GetClient();
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
Core.Dispose();
Core = null;
}
}
this._disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 保存数据DB文件到硬盘
/// </summary>
public void Save()