VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之StackExchange.Redis 二次封装(5)

/// <returns></returns> public static async Task<List<T>> GetListAsync<T>(int dbIndex, string key, Func<List<T>> fun, int timeout = EXPIRY) where T : class { dbIndex = CheckDbIndex(dbIndex); List<T> datas = RedisUtils.StringGet<List<T>>(dbIndex, key); if (datas != null && datas.Count > 0) { return datas; } datas = await Task.Run(() => { return fun(); }); if (datas != null && datas.Count > 0) { RedisUtils.StringSet<List<T>>(dbIndex, key, datas, TimeSpan.FromMinutes(timeout)); } return datas; } /// <summary> /// ZSet /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dbIndex"></param> /// <param name="key"></param> /// <param name="func">如找不到则从func获取</param> /// <returns></returns> public static List<T> GetObject_ZSet<T>(int dbIndex, string key, Func<List<T>> func) where T : class { List<T> data = RedisUtils.SortedSetRangeByRank<T>(dbIndex, key); if (data != null && data.Count > 0) { return data; } if (func != null) { data = func(); } if (data != null) { RedisUtils.SortedSetAdd<T>(dbIndex, key, data); } return data; } /// <summary> /// Hash /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dbIndex"></param> /// <param name="hashID">hashID</param> /// <param name="key"></param> /// <param name="func">如找不到则从func获取</param> /// <returns></returns> public static T GetObject_Hash<T>(int dbIndex, string hashID, string key, Func<T> func) where T : class { T data = RedisUtils.HashGet<T>(dbIndex, hashID, key); if (data != null) { return data; } if (func != null) { data = func(); } if (data != null) { RedisUtils.HashSet<T>(dbIndex, hashID, key, data); } return data; } }
复制代码

 修复

经过反复阅读源码和测试,ConnectionMultiplexer是线程安全的,在多线程的情况下,避免使用lock来作为单例来使用,出现的连接超时的情况。

It was not possible to connect to the redis server(s).ConnectTimeout

正确姿势在帮助类折叠代码里面。

测试源码:

复制代码
static void TestRedis()
        {
            int dbIdx = 4;
            string key = "testKey";
            Task.Run(()=> {
                while (true)
                {
                    Task.Run(() =>
                    {
                        RedisUtils.StringSet(dbIdx, key, DateTime.Now.ToString(), TimeSpan.FromSeconds(30));
                    });
                    Task.Run(() =>
                    {
                        string v = RedisUtils.StringGet(dbIdx, key);
                        Console.WriteLine("1:" + v);
                    });
                    System.Threading.Thread.Sleep(100);
                }
            });
            Task.Run(() => {
                while (true)
                {
                    Task.Run(() =>
                    {
                        RedisUtils.StringSet(dbIdx, key, DateTime.Now.ToString(), TimeSpan.FromSeconds(30));
                    });
                    Task.Run(() =>
                    {
                        string v = RedisUtils.StringGet(dbIdx, key);
                        Console.WriteLine("2:" + v);
                    });
                    System.Threading.Thread.Sleep(100);
                }
            });
            Task.Run(() => {
                while (true)
                {
                    Task.Run(() =>
                    {
                        RedisUtils.StringSet(dbIdx, key, DateTime.Now.ToString(), TimeSpan.FromSeconds(30));
                    });
                    Task.Run(() =>
                    {
                        string v = RedisUtils.StringGet(dbIdx, key);
                        Console.WriteLine("3:" + v);
                    });
                    System.Threading.Thread.Sleep(100);
                }
            });

        }
复制代码

 已封装在这里


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号