zoukankan      html  css  js  c++  java
  • asp.net redis 帮助类封装

    问题:in C# Redis : could not connect to redis Instance at 127.0.0.1:6379  (有时候访问不了,刷新又可访问)

    使用的是ServiceStack.redis 版本 2.2 ,据说版本4+ 要收费。

    代码:开始用    var result=new RedisClient("127.0.0.1",6379,1"1111"); 

    后面改为连接池(集群的方式)
    
    /// <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");
    }
    

      转自:https://www.cnblogs.com/zhian/p/5669964.html

  • 相关阅读:
    Sum Root to Leaf Numbers [LeetCode]
    Symmetric Tree [LeetCode]
    Combination Sum II [LeetCode]
    Maximal Rectangle [LeetCode]
    Trapping Rain Water [LeetCode]
    Combination Sum [LeetCode]
    05 如何“响铃”
    04 八进制
    03 关键字?保留字?预留字?
    020 函数之变量的作用域
  • 原文地址:https://www.cnblogs.com/xiaobudong/p/9552612.html
Copyright © 2011-2022 走看看