/// <summary>
/// RedisManager类主要是创建链接池管理对象的
/// </summary>
public class RedisManager
{
/// <summary>
/// redis配置文件信息
/// </summary>
private static string RedisPath = System.Configuration.ConfigurationSettings.AppSettings["RedisPath"];
private static PooledRedisClientManager _prcm;

/// <summary>
/// 静态构造方法,初始化链接池管理对象
/// </summary>
static RedisManager()
{
CreateManager();
}

/// <summary>
/// 创建链接池管理对象
/// </summary>
private static void CreateManager()
{
_prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath });
}

private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
{
//WriteServerList:可写的Redis链接地址。
//ReadServerList:可读的Redis链接地址。
//MaxWritePoolSize:最大写链接数。
//MaxReadPoolSize:最大读链接数。
//AutoStart:自动重启。
//LocalCacheTime:本地缓存到期时间,单位:秒。
//RecordeLog:是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项。
//RedisConfigInfo类是记录redis连接信息,此信息和配置文件中的RedisConfig相呼应

// 支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 5, // “写”链接池链接数
MaxReadPoolSize = 5, // “读”链接池链接数
AutoStart = true,

});

// ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("Redis1_IP地址:端口,password=密码");
}

private static IEnumerable<string> SplitString(string strSource, string split)
{
return strSource.Split(split.ToArray());
}

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

}

/// <summary>
/// RedisOperatorBase类,是redis操作的基类,继承自IDisposable接口,主要用于释放内存
/// </summary>
public abstract class RedisOperatorBase : IDisposable
{
protected IRedisClient Redis { get; private set; }
private bool _disposed = false;
protected RedisOperatorBase()
{
Redis = RedisManager.GetClient();
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
Redis.Dispose();
Redis = null;
}
}
this._disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// 保存数据DB文件到硬盘
/// </summary>
public void Save()
{
Redis.Save();
}
/// <summary>
/// 异步保存数据DB文件到硬盘
/// </summary>
public void SaveAsync()
{
Redis.SaveAsync();
}

}

/// <summary>
/// HashOperator类,是操作哈希表类。继承自RedisOperatorBase类
/// </summary>
public class HashOperator : RedisOperatorBase
{
public HashOperator() : base() { }
/// <summary>
/// 判断某个数据是否已经被缓存
/// </summary>
public bool Exist<T>(string hashId, string key)
{
return Redis.HashContainsEntry(hashId, key);
}
/// <summary>
/// 存储数据到hash表
/// </summary>
public bool Set<T>(string hashId, string key, T t)
{
var value = JsonSerializer.SerializeToString<T>(t);
return Redis.SetEntryInHash(hashId, key, value);
}
/// <summary>
/// 移除hash中的某值
/// </summary>
public bool Remove(string hashId, string key)
{
return Redis.RemoveEntryFromHash(hashId, key);
}
/// <summary>
/// 移除整个hash
/// </summary>
public bool Remove(string key)
{
return Redis.Remove(key);
}
/// <summary>
/// 从hash表获取数据
/// </summary>
public T Get<T>(string hashId, string key)
{
string value = Redis.GetValueFromHash(hashId, key);
return JsonSerializer.DeserializeFromString<T>(value);
}
/// <summary>
/// 获取整个hash的数据
/// </summary>
public List<T> GetAll<T>(string hashId)
{
var result = new List<T>();
var list = Redis.GetHashValues(hashId);
if (list != null && list.Count > 0)
{
list.ForEach(x =>
{
var value = JsonSerializer.DeserializeFromString<T>(x);
result.Add(value);
});
}
return result;
}
/// <summary>
/// 设置缓存过期
/// </summary>
public void SetExpire(string key, DateTime datetime)
{
Redis.ExpireEntryAt(key, datetime);
}
}

调用实例

using (HashOperator ht = new HashOperator())
{
ht.Set<string>(i.ToString(), "a"+i.ToString(), "value"+i.ToString());
//string str = ht.Get<string>(i.ToString(), "a" + i.ToString());
//Console.WriteLine(str);
//MessageBox.Show(str);
}

配置服务访问连接字符串

<add key="RedisPath" value="123456@127.0.0.1:6379" />

其中 123456 为密码

127.0.0.1:6379 为服务地址及端口

使用  ServiceStack.Redis V3.9.71 版本 可以避免高版本 每小时6000次的访问并发限制

最新文章

  1. C#读取Excel,或者多个excel表,返回dataset
  2. BZOJ 1009 【HNOI2008】 GT考试
  3. Android OpenGL 基础入门
  4. php学习笔记:利用gd库生成图片,并实现随机验证码
  5. JY游戏之毁经典《扫雷》
  6. SVN与TortoiseSVN实战:从入门到精通
  7. location.hash来保持页面状态
  8. Ubuntu 12.04 (10) Personal Environment - @staticor
  9. BGP服务器您了解多少?
  10. 一步一步学Vue(九)
  11. 201521123026 《Java程序设计》第一周学习总结
  12. Python爬虫【实战篇】获取网易云歌词
  13. 第二个项目:WC
  14. 【游记】THUWC2018踹线记
  15. Java 之 Web前端(一)
  16. phpstorm激活大全--持续更新(支持2018最新版)
  17. XGboost学习总结
  18. android学习-Activity启动过程详解
  19. CS231n课程笔记翻译6:神经网络笔记 part1
  20. 探讨Java I/O类和接口

热门文章

  1. GET /static/plugins/bootstrap/css/bootstrap.css HTTP/1.1&quot; 404 1718
  2. 以组件的方式,添加redis_cache
  3. Java8-Stream-No.08
  4. retrying failed action with response code: 403 错误解决
  5. iosselect插件
  6. Combine String HDU - 5707 dp or 广搜
  7. Codevs 1298 凸包周长
  8. 【线性代数】2-1:解方程组(Ax=b)
  9. Win10 + CLion + 树莓派 + QT 远程开发调用Python
  10. php &amp;#编码/php unicode转码/php &amp;#数字编码