當開始用 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");
}