コード例 #1
0
        public static bool IsAuthorizedByCache(string systemCode, string userId, string permissionCode)
        {
            bool result = false;

            using (var redisReadOnlyClient = PooledRedisHelper.GetPermissionReadOnlyClient())
            {
                // 2016-02-18 吉日嘎拉 这样可以刷新用户权限时,可以把一个用户的权限全去掉。
                string hashId = "User:IsAuthorized:" + userId;
                string key    = systemCode + ":" + permissionCode;
                // 若是缓存里过期了?
                if (redisReadOnlyClient.HashContainsEntry(hashId, key))
                {
                    string isAuthorized = redisReadOnlyClient.GetValueFromHash(hashId, key);
                    result = isAuthorized.Equals(true.ToString());
                }
                else
                {
                    BasePermissionManager permissionManager = new BasePermissionManager();
                    result = permissionManager.IsAuthorized(systemCode, userId, permissionCode);
#if ReadOnlyRedis
                    using (var redisClient = PooledRedisHelper.GetPermissionClient())
                    {
                        redisClient.SetEntryInHash(hashId, key, result.ToString());
                        redisClient.ExpireEntryAt(hashId, DateTime.Now.AddMinutes(20));
                    }
#else
                    redisReadOnlyClient.SetEntryInHash(hashId, key, result.ToString());
                    redisReadOnlyClient.ExpireEntryAt(hashId, DateTime.Now.AddMinutes(20));
#endif
                }
            }

            return(result);
        }
コード例 #2
0
        private static List <BaseModuleEntity> GetListCache(string key)
        {
            List <BaseModuleEntity> result = null;

            if (!string.IsNullOrWhiteSpace(key))
            {
                using (var redisClient = PooledRedisHelper.GetPermissionReadOnlyClient())
                {
                    result = redisClient.Get <List <BaseModuleEntity> >(key);
                }
            }

            return(result);
        }
コード例 #3
0
        public static BaseModuleEntity GetCacheByKey(string key)
        {
            BaseModuleEntity result = null;

            if (!string.IsNullOrWhiteSpace(key))
            {
                using (var redisClient = PooledRedisHelper.GetPermissionReadOnlyClient())
                {
                    result = redisClient.Get <BaseModuleEntity>(key);
                }
            }

            return(result);
        }
コード例 #4
0
        public static string[] GetPermissionIdsByCache(string systemCode, string organizeId)
        {
            string[] result = null;

            string key = string.Empty;

            key = "Permission:" + systemCode + ":Organize:" + organizeId;

            List <string> items = null;

            using (var readOnlyRedisClient = PooledRedisHelper.GetPermissionReadOnlyClient())
            {
                HashSet <string> setItems = readOnlyRedisClient.GetAllItemsFromSet(key);
                if (setItems.Count == 0)
                {
                    BaseOrganizePermissionManager organizePermissionManager = new Business.BaseOrganizePermissionManager();
                    result = organizePermissionManager.GetPermissionIds(organizeId);

                    if (result != null)
                    {
                        if (result.Length > 0)
                        {
                            items = new List <string>(result);
                        }
                        else
                        {
                            // 2016-02-26 吉日嘎拉 这里是为了防止重复读取,数据库被拖、效率低
                            items = new List <string>(new string[] { string.Empty });
                        }
#if ReadOnlyRedis
                        using (var redisClient = PooledRedisHelper.GetPermissionClient())
                        {
                            redisClient.AddRangeToSet(key, items);
                            redisClient.ExpireEntryAt(key, DateTime.Now.AddMinutes(20));
                        }
#else
                        readOnlyRedisClient.AddRangeToSet(key, items);
                        readOnlyRedisClient.ExpireEntryAt(key, DateTime.Now.AddMinutes(20));
#endif
                    }
                }
                else
                {
                    result = setItems.ToArray();
                }
            }

            return(result);
        }
コード例 #5
0
        public static string[] GetRoleIdsByCache(string systemCode, string userId, string companyId = null)
        {
            // 返回值
            string[] result = null;

            if (!string.IsNullOrEmpty(userId))
            {
                string key = "User:"******":" + systemCode + ":Role";

                using (var readOnlyRedisClient = PooledRedisHelper.GetPermissionReadOnlyClient())
                {
                    HashSet <string> setItems = readOnlyRedisClient.GetAllItemsFromSet(key);
                    if (setItems.Count == 0)
                    {
                        // 找不到数据进行数据库查询
                        BaseUserManager userManager = new BaseUserManager();
                        string[]        roleIds     = userManager.GetRoleIds(systemCode, userId, companyId);

                        List <string> items = null;
                        if (roleIds.Length > 0)
                        {
                            items = new List <string>(roleIds);
                        }
                        else
                        {
                            // 2016-02-26 吉日嘎拉 这里是为了防止重复读取,数据库被拖、效率低
                            items = new List <string>(new string[] { string.Empty });
                        }
#if ReadOnlyRedis
                        using (var redisClient = PooledRedisHelper.GetPermissionClient())
                        {
                            redisClient.AddRangeToSet(key, items);
                            redisClient.ExpireEntryAt(key, DateTime.Now.AddMinutes(20));
                        }
#else
                        readOnlyRedisClient.AddRangeToSet(key, items);
                        readOnlyRedisClient.ExpireEntryAt(key, DateTime.Now.AddMinutes(20));
#endif
                    }
                    else
                    {
                        result = setItems.ToArray();
                    }
                }
            }

            return(result);
        }
コード例 #6
0
        /// <summary>
        /// 多个角色,都有啥权限?单个角色都有啥权限的循环获取?
        /// </summary>
        /// <param name="systemCode">系统编号</param>
        /// <param name="roleIds">角色主键数组</param>
        /// <returns>权限数组</returns>
        public static string[] GetPermissionIdsByCache(string systemCode, string[] roleIds)
        {
            string[] result = null;

            string key    = string.Empty;
            string roleId = string.Empty;
            BaseRolePermissionManager rolePermissionManager = null;

            string[] permissionIds = null;
            using (var readOnlyRedisClient = PooledRedisHelper.GetPermissionReadOnlyClient())
            {
                for (int i = 0; i < roleIds.Length; i++)
                {
                    // 2016-02-26 吉日嘎拉 若是角色是空的,跳出循环,没必要查了,提高效率
                    roleId = roleIds[i];
                    if (string.IsNullOrEmpty(roleId))
                    {
                        continue;
                    }
                    key = "Permission:" + systemCode + ":Role:" + roleId;
                    List <string> items = null;

                    HashSet <string> setItems = readOnlyRedisClient.GetAllItemsFromSet(key);
                    if (setItems.Count == 0)
                    {
                        if (rolePermissionManager == null)
                        {
                            rolePermissionManager = new Business.BaseRolePermissionManager();
                        }
                        permissionIds = rolePermissionManager.GetPermissionIds(systemCode, roleId);

                        if (permissionIds != null)
                        {
                            if (permissionIds.Length > 0)
                            {
                                items = new List <string>(permissionIds);
                            }
                            else
                            {
                                // 2016-02-26 吉日嘎拉 这里是为了防止重复读取,数据库被拖、效率低
                                items = new List <string>(new string[] { string.Empty });
                            }

#if ReadOnlyRedis
                            using (var redisClient = PooledRedisHelper.GetClient())
                            {
                                // 2016-02-26 吉日嘎拉 角色权限一般发生变更时,需要有一定的即时性、所以不能缓存太长时间,宁可再次读取一下数据库
                                redisClient.AddRangeToSet(key, items);
                                redisClient.ExpireEntryAt(key, DateTime.Now.AddMinutes(20));
                            }
#else
                            readOnlyRedisClient.AddRangeToSet(key, items);
                            readOnlyRedisClient.ExpireEntryAt(key, DateTime.Now.AddMinutes(20));
#endif
                        }
                    }
                    else
                    {
                        permissionIds = setItems.ToArray();
                    }

                    result = StringUtil.Concat(result, permissionIds);
                }
            }

            return(result);
        }