Exemplo n.º 1
0
        /// <summary>
        /// 从某个角色列表中轮循获取一个用户,在线的用户优先。
        /// </summary>
        /// <param name="roleCode">角色编号</param>
        /// <param name="userId">当前用户主键</param>
        /// <param name="cacheHours">缓存小时数</param>
        /// <returns>用户主键</returns>
        public string GetRandomUserId(IDbHelper dbHelper, string roleCode, string userId, int cacheHours = 4)
        {
            string result = null;

            // 检查缓存里,是否有这个用户的对应客服的主键?若没有进行分配
            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            if (!string.IsNullOrEmpty(userId))
            {
                if (cache != null && cache[userId] == null)
                {
                    var manager = new BaseUserManager(dbHelper);
                    result = manager.GetRandomUserId("Base", roleCode);
                    cache.Add(userId, result, null, DateTime.Now.AddHours(cacheHours), TimeSpan.Zero, CacheItemPriority.Normal, null);
                }
                result = cache[userId] as string;
            }
            return(result);
        }