最近在看通用權限管理系統提供用戶中心接口,發現有很多變化,現將個人理解分享給你們:web
用戶中心對外提供了基礎信息、權限的接口,剛開始的部署方式採用了以下圖的方式,因爲客戶端及應用服務器的網絡環境,接口服務器配置了多個電信運營商的網絡連接;redis
最初在用戶量不大的時候,調用接口時是直接訪問數據庫獲取數據嚮應用服務器輸出,隨着客戶端訪問量的增大,用戶中心庫的壓力也逐漸增大,爲了保證接口的穩定性,減輕數據庫壓力,進行了緩存化改造,主要使用了Redis,並進行了讀寫分離,改造後的示意圖以下:數據庫
基礎信息管理應用系統主要是維護基礎信息、權限配置的,能夠直觀的看到,改造後,基礎信息在更新後會將數據緩存到Redis服務器,經過Redis的主從配置,進行了讀寫分離,接口服務器裏將原有的訪問數據庫獲取的方法所有改造爲從Redis中獲取,極大的提升了效率。緩存
底層中封裝好了Redis獲取數據的方法,項目截圖以下:服務器
提供部分參考代碼以下,能夠根據這個思路進行緩存優化,提升效率:網絡
namespace DotNet.Business { using DotNet.Utilities; /// <summary> /// 用戶數據的緩存獨立庫。 /// /// 修改紀錄 /// /// 2016-01-07 版本:1.1 JiRiGaLa 實現讀寫分離。 /// 2015-04-11 版本:1.0 JiRiGaLa 建立主鍵。 /// /// <author> /// <name>JiRiGaLa</name> /// <date>2016-01-07</date> /// </author> /// </summary> public sealed partial class PooledRedisHelper { // Redis數據庫 public static int InitialDbUser = 6; private static PooledRedisClientManager instanceUser = null; public static PooledRedisClientManager InstanceUser { get { if (instanceUser == null) { instanceUser = new PooledRedisClientManager(InitialDbUser, new string[] { BaseSystemInfo.RedisHosts }); } return instanceUser; } } public static IRedisClient GetUserClient() { return InstanceUser.GetClient(); } private static PooledRedisClientManager instanceUserReadOnly = null; public static PooledRedisClientManager InstanceUserReadOnly { get { if (instanceUserReadOnly == null) { instanceUserReadOnly = new PooledRedisClientManager(InitialDbUser, new string[] { BaseSystemInfo.RedisReadOnlyHosts }); } return instanceUserReadOnly; } } public static IRedisClient GetUserReadOnlyClient() { return InstanceUserReadOnly.GetClient(); } } }
設置和獲取緩存的方法post
/// <summary> /// 獲取緩存 /// </summary> /// <param name="key">緩存主鍵</param> /// <returns>用戶信息</returns> public static BaseUserEntity GetCache(string key) { BaseUserEntity result = null; if (!string.IsNullOrWhiteSpace(key)) { // 2016-01-08 吉日嘎拉 實現只讀鏈接,讀寫分離 using (var redisClient = PooledRedisHelper.GetUserReadOnlyClient()) { result = redisClient.Get<BaseUserEntity>(key); } } return result; } /// <summary> /// 獲取緩存 /// 20151007 吉日嘎拉,須要在一個鏈接上進行大量的操做 /// </summary> /// <param name="key">緩存主鍵</param> /// <returns>用戶信息</returns> private static BaseUserEntity GetCache(IRedisClient redisClient, string key) { BaseUserEntity result = null; if (!string.IsNullOrWhiteSpace(key)) { result = redisClient.Get<BaseUserEntity>(key); } return result; }
對外提供接口參考:優化
/// <summary> /// 用戶是否在某個角色裏 /// </summary> /// <param name="userInfo">用戶信息</param> /// <param name="systemCode">系統編號</param> /// <param name="userId">用戶主鍵</param> /// <param name="roleCode">角色編號</param> /// <returns>在角色裏</returns> public static bool IsInRoleByCode(BaseUserInfo userInfo, string systemCode, string userId, string roleCode) { bool result = false; string url = BaseSystemInfo.UserCenterHost + "/UserCenter/UserService"; WebClient webClient = new WebClient(); NameValueCollection postValues = new NameValueCollection(); postValues.Add("system", BaseSystemInfo.SoftFullName); postValues.Add("systemCode", systemCode); postValues.Add("securityKey", BaseSystemInfo.SecurityKey); postValues.Add("function", "IsInRoleByCode"); postValues.Add("userInfo", userInfo.Serialize()); postValues.Add("userId", userId); postValues.Add("roleCode", roleCode); // 向服務器發送POST數據 byte[] responseArray = webClient.UploadValues(url, postValues); string response = Encoding.UTF8.GetString(responseArray); if (!string.IsNullOrEmpty(response)) { result = JsonConvert.DeserializeObject<bool>(response); } return result; }
經過這一番改造,各個應用系統要獲取基礎信息,能夠直接調用底層的方法,或者經過接口獲取,同時接口實現了若是緩存沒有數據或數據已過時會去數據庫獲取,同時進行數據緩存。url