public static BaseDepartmentEntity GetObjectByCodeByCache(string code) { BaseDepartmentEntity result = null; if (string.IsNullOrEmpty(code)) { return(result); } // string key = "DepartmentByCode:" + code; string key = "DBC:" + code; string id = string.Empty; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { id = redisClient.Get <string>(key); } if (!string.IsNullOrWhiteSpace(id)) { result = GetObjectByCache(id); } else { // 从数据库读取数据 BaseDepartmentManager departmentManager = new BaseDepartmentManager(); result = departmentManager.GetObjectByCode(code); // 设置缓存,没必要来个空操作 if (result != null) { SetCache(result); } } return(result); }
/// <summary> /// 设置缓存 /// </summary> /// <param name="entity">登录信息</param> public static void SetCache(BaseOrganizeLogOnEntity entity) { using (var redisClient = PooledRedisHelper.GetCallLimitClient()) { SetCache(redisClient, entity); } }
public static BaseDepartmentEntity GetObjectByNameByCache(BaseUserInfo userInfo, string companyId, string fullName) { BaseDepartmentEntity result = null; string key = "DBN:" + companyId + ":" + fullName; string id = string.Empty; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { id = redisClient.Get <string>(key); if (!string.IsNullOrWhiteSpace(id)) { key = "D:" + id; result = redisClient.Get <BaseDepartmentEntity>(key); } } // 远程通过接口获取数据 if (result == null) { result = GetObjectByName(userInfo, companyId, fullName); } return(result); }
public int AfterDeleted(object[] ids, bool enabled = false, bool modifiedUser = false) { int result = 0; for (int i = 0; i < ids.Length; i++) { string id = ids[i].ToString(); BaseUserEntity entity = GetObjectByCache(id); // 2016-02-19 宋彪 PooledRedisHelper.GetClient() 改为 PooledRedisHelper.GetSpellingClient() using (var redisClient = PooledRedisHelper.GetSpellingClient()) { // 2016-02-02 吉日嘎拉,修改数据后重新设置缓存。 BaseUserManager.CachePreheatingSpelling(redisClient, entity); } } if (ids != null && ids.Length > 0) { // 删除后把已经删除的数据搬迁到被删除表里去。 /* * string where = BaseUserEntity.FieldId + " IN (" + StringUtil.ArrayToList((string[])ids, "") + ") "; * * string commandText = @"INSERT INTO BASEUSER_DELETED SELECT * FROM " + BaseUserEntity.TableName + " WHERE " + where; * IDbHelper dbHelper1 = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType, BaseSystemInfo.UserCenterDbConnection); * dbHelper1.ExecuteNonQuery(commandText); */ // 进行删除操作 this.Delete(ids); } return(result); }
public static BaseOrganizeEntity GetObjectByNameByCache(BaseUserInfo userInfo, string fullName) { BaseOrganizeEntity result = null; string key = "OBN:" + fullName; string id = string.Empty; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { id = redisClient.Get <string>(key); } if (!string.IsNullOrEmpty(id)) { key = "O:" + id; result = BaseOrganizeManager.GetCacheByKey(key); } // 远程通过接口获取数据 if (result == null) { result = GetObjectByName(userInfo, fullName); } return(result); }
/// <summary> /// 获取缓存的网点登录信息 /// </summary> /// <param name="companyId">网点Id</param> public static BaseOrganizeLogOnEntity GetCache(string companyId) { using (var redisClient = PooledRedisHelper.GetCallLimitClient()) { return(redisClient.Get <BaseOrganizeLogOnEntity>("OrganizeLogOn:" + companyId)); } }
/// <summary> /// 通过 openId 获取用户信息 /// </summary> /// <param name="openId">唯一键</param> /// <returns>用户实体</returns> public static BaseUserEntity GetObjectByOpenIdByCache(string openId) { BaseUserEntity result = null; string key = "OpenId"; string userId = string.Empty; if (!string.IsNullOrWhiteSpace(openId)) { key = key + openId; # if Redis // 2015-12-14 吉日嘎拉 这里可以支持不走缓存的方式 using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { userId = redisClient.Get <string>(key); if (!string.IsNullOrWhiteSpace(userId)) { result = GetObjectByCache(redisClient, userId); } if (result == null) { // 若没获取到用户?到数据库里查一次 BaseUserLogOnManager userLogOnManager = new BaseUserLogOnManager(); userId = userLogOnManager.GetIdByOpenId(openId); if (!string.IsNullOrWhiteSpace(userId)) { result = GetObjectByCache(redisClient, userId); } } } #endif }
/// <summary> /// 把未阅读的消息,进行缓存处理 /// 2015-09-27 吉日嘎拉 优化代码 /// </summary> /// <param name="entity">消息</param> public void CacheProcessing(BaseMessageEntity entity, DateTime?expireAt = null) { using (var redisClient = PooledRedisHelper.GetMessageClient()) { CacheProcessing(redisClient, entity, expireAt); } }
public static BaseUserEntity GetObjectByCompanyIdByCodeByCache(string companyId, string userCode) { BaseUserEntity result = null; // 检查参数有效性 if (string.IsNullOrWhiteSpace(companyId) || string.IsNullOrWhiteSpace(userCode)) { return(result); } string key = "User:ByCompanyId:ByCode" + companyId + ":" + userCode; string id = string.Empty; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { id = redisClient.Get <string>(key); if (!string.IsNullOrWhiteSpace(id)) { result = GetObjectByCache(redisClient, id); } } if (result == null) { BaseUserManager manager = new BaseUserManager(); result = manager.GetObjectByCompanyIdByCode(companyId, userCode); SetCache(result); } return(result); }
/// <summary> /// 用户是否在公司里 /// </summary> /// <param name="companyCode">公司编号</param> /// <param name="userCode">用户编号</param> /// <returns></returns> public static bool IsInOrganizeByCode(string companyCode, string userCode) { // 返回值 bool result = false; // 检查参数有效性 if (string.IsNullOrWhiteSpace(companyCode) || string.IsNullOrWhiteSpace(userCode)) { return(result); } // 先判断缓存,减少数据库查询 string key = "User:ByCompanyCode:ByCode" + companyCode + ":" + userCode; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { result = redisClient.ContainsKey(key); if (result) { return(result); } } if (!result) { BaseUserManager manager = new BaseUserManager(); BaseUserEntity entity = manager.GetObjectByCompanyCodeByCode(companyCode, userCode); SetCache(entity); result = true; } return(result); }
/// <summary> /// 设置缓存 /// </summary> /// <param name="entity">用户实体</param> public static void SetCache(BaseUserEntity entity) { using (var redisClient = PooledRedisHelper.GetClient()) { SetCache(redisClient, entity); } }
public static int RefreshCache(string userId) { int result = 0; // 刷新用户的缓存 BaseUserEntity userEntity = BaseUserManager.GetObjectByCache(userId, true); if (userEntity != null) { // 刷新用户的登录限制 BaseUserManager.ResetIPAddressByCache(userId); BaseUserManager.ResetMACAddressByCache(userId); // 刷新组织机构缓存 BaseOrganizeManager.GetObjectByCache(userEntity.CompanyId, true); // 刷新部门缓存 BaseDepartmentManager.GetObjectByCache(userEntity.DepartmentId, true); // 2016-02-18 吉日嘎拉 刷新拒绝权限(把用户的权限放在一起方便直接移除、刷新) string key = "User:IsAuthorized:" + userId; using (var redisClient = PooledRedisHelper.GetPermissionClient()) { redisClient.Remove(key); } // 每个子系统都可以循环一次 string[] systemCodes = BaseSystemManager.GetSystemCodes(); for (int i = 0; i < systemCodes.Length; i++) { BaseUserPermissionManager.ResetPermissionByCache(systemCodes[i], userId); } } return(result); }
/// <summary> /// 缓存预热,强制重新缓存 /// </summary> /// <returns>影响行数</returns> public static int CachePreheating() { int result = 0; // 把所有的数据都缓存起来的代码 BaseUserLogOnManager manager = new BaseUserLogOnManager(); // 基础用户的登录信息重新缓存起来 string commandText = "SELECT * FROM baseuserlogon t WHERE t.userpassword IS NOT NULL AND t.openidtimeout IS NOT NULL AND t.enabled = 1 AND t.openidtimeout - sysdate < 0.5"; using (IDataReader dataReader = manager.ExecuteReader(commandText)) { using (var redisClient = PooledRedisHelper.GetClient()) { while (dataReader.Read()) { BaseUserLogOnEntity entity = BaseEntity.Create <BaseUserLogOnEntity>(dataReader, false); # if Redis Utilities.SetUserOpenId(redisClient, entity.Id, entity.OpenId); #else Utilities.SetUserOpenId(entity.Id, entity.OpenId); # endif result++; System.Console.WriteLine(result.ToString() + " : User : " + entity.Id); } dataReader.Close(); }
public static string GetParameterByCache(string tableName, string categoryCode, string parameterId, string parameterCode, bool refreshCache = false) { string result = string.Empty; if (!string.IsNullOrEmpty(categoryCode) && !string.IsNullOrEmpty(parameterId) && !string.IsNullOrEmpty(parameterCode)) { string key = "Parameter:" + tableName + ":" + categoryCode + ":" + parameterId + ":" + parameterCode; using (var redisClient = PooledRedisHelper.GetClient()) { if (!refreshCache) { result = redisClient.Get <string>(key); } if (string.IsNullOrEmpty(result)) { BaseParameterManager parameterManager = new Business.BaseParameterManager(tableName); result = parameterManager.GetParameter(tableName, categoryCode, parameterId, parameterCode); if (!string.IsNullOrEmpty(result)) { redisClient.Set <string>(key, result, DateTime.Now.AddMinutes(10)); } } } } return(result); }
/// <summary> /// 从缓存获取获取实体 /// </summary> /// <param name="companyId">公司主键</param> /// <param name="fullName">部门名称</param> /// <returns>实体</returns> public static BaseDepartmentEntity GetObjectByNameByCache(string companyId, string fullName) { BaseDepartmentEntity result = null; if (!string.IsNullOrEmpty(companyId) && !string.IsNullOrEmpty(fullName)) { string id = string.Empty; string key = "DBN:" + companyId + ":" + fullName; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { id = redisClient.Get <string>(key); if (!string.IsNullOrWhiteSpace(id)) { result = GetObjectByCache(id); } if (result == null) { BaseDepartmentManager departmentManager = new BaseDepartmentManager(); result = departmentManager.GetObjectByName(companyId, fullName); // 若是空的不用缓存,继续读取实体 if (result != null) { SetCache(result); } } } } return(result); }
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); }
public static BaseOrganizeEntity GetObjectByNameByCache(string fullName) { BaseOrganizeEntity result = null; // string key = "OrganizeByName:" + fullName; string key = "OBN:" + fullName; string id = string.Empty; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { id = redisClient.Get <string>(key); if (!string.IsNullOrWhiteSpace(id)) { result = GetObjectByCache(redisClient, id); } else { // 从数据库读取数据 BaseOrganizeManager organizeManager = new BaseOrganizeManager(); result = organizeManager.GetObjectByName(fullName); // 设置缓存 if (result != null) { SetCache(result); } } } return(result); }
/// <summary> /// 某一个网点是否另外一个网点的父级 /// </summary> /// <param name="parentId">父级别主键</param> /// <param name="id">主键盘</param> /// <returns>是父亲节点</returns> public static bool IsParentByCache(string parentId, string id) { bool result = false; // 打开缓存、进行查找 using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { // 若已经在缓存里保存了,是上级网点、就不用计算了,提高判断的效率 string key = "IsParent:" + parentId + ":" + id; result = redisClient.Get <bool>(key); if (!result) { // 从缓存读取数据、提高读取的效率,这里需要防止死循环,有时候会有脏数据 BaseOrganizeEntity organizeEntity = BaseOrganizeManager.GetObjectByCache(redisClient, id); while (organizeEntity != null && !string.IsNullOrEmpty(organizeEntity.ParentId) && !id.Equals(organizeEntity.ParentId) ) { // 若已经找到了,就退出循环,提高效率 if (organizeEntity.ParentId.Equals(parentId)) { result = true; break; } organizeEntity = BaseOrganizeManager.GetObjectByCache(redisClient, organizeEntity.ParentId); } // 设置一个过期时间,提高效率,10分钟内不需要重复判断,把关系写入缓存数据库里,是否成立都写入缓存服务器里。 redisClient.Set <bool>(key, result, DateTime.Now.AddMinutes(10)); } } return(result); }
/// <summary> /// 缓存预热,强制重新缓存 /// </summary> /// <returns>影响行数</returns> public static int CachePreheatingService() { int result = 0; List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >(); parameters.Add(new KeyValuePair <string, object>(BaseServicesLicenseEntity.FieldEnabled, 1)); parameters.Add(new KeyValuePair <string, object>(BaseServicesLicenseEntity.FieldDeletionStateCode, 0)); // 把所有的数据都缓存起来的代码 BaseServicesLicenseManager manager = new BaseServicesLicenseManager(); using (var redisClient = PooledRedisHelper.GetClient()) { using (IDataReader dataReader = manager.ExecuteReader(parameters)) { while (dataReader.Read()) { string key = "User:"******":Service"; string privateKey = dataReader[BaseServicesLicenseEntity.FieldPrivateKey].ToString(); redisClient.AddItemToSet(key, privateKey); redisClient.ExpireEntryAt(key, DateTime.Now.AddMonths(3)); result++; System.Console.WriteLine(result.ToString() + " : " + privateKey); } dataReader.Close(); } } return(result); }
/// <summary> /// 获取最近一个月登录系统的用户,从缓存服务器,登录接口获取,这样僵尸账户就会可以过滤了。 /// </summary> /// <returns>影响行数</returns> public int GetActiveUsers() { int result = 0; // 先把用户来源都修改为 空。 string commandText = "UPDATE " + BaseUserEntity.TableName + " SET " + BaseUserEntity.FieldUserFrom + " = NULL WHERE " + BaseUserEntity.FieldUserFrom + " = 'Base'"; result = this.DbHelper.ExecuteNonQuery(commandText); // 通过中天登录的,都设置为 "Base"; string key = string.Empty; result = 0; int i = 0; using (var redisClient = PooledRedisHelper.GetClient()) { key = "LogOn:UserCount:" + DateTime.Now.ToString("yyyy-MM"); HashSet <string> list = redisClient.GetAllItemsFromSet(key); foreach (string id in list) { i++; commandText = "UPDATE " + BaseUserEntity.TableName + " SET " + BaseUserEntity.FieldUserFrom + " = 'Base' WHERE " + BaseUserEntity.FieldId + " = " + id; result += this.DbHelper.ExecuteNonQuery(commandText); System.Console.WriteLine("Count:" + i.ToString() + "/" + list.Count + " Id:" + id); } } return(result); }
public static BaseUserInfo GetUserInfoCaching(string openId, string cachingSystemCode = null) { BaseUserInfo result = null; // 2015-11-20 吉日嘎拉 为了编译通过进行改进 using (var redisClient = PooledRedisHelper.GetClient()) { string key = string.Empty; if (string.IsNullOrEmpty(cachingSystemCode)) { key = "openId:" + openId; } else { key = "openId:" + cachingSystemCode + ":" + openId; } string userId = redisClient.Get <string>(key); if (!string.IsNullOrEmpty(userId)) { key = "userInfo:" + userId; result = redisClient.Get <BaseUserInfo>(key); } } return(result); }
/// <summary> /// 缓存预热,强制重新缓存 /// </summary> /// <returns>影响行数</returns> public static int CachePreheatingMACAddress() { int result = 0; List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >(); parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldCategoryCode, "MacAddress")); parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldEnabled, 1)); parameters.Add(new KeyValuePair <string, object>(BaseParameterEntity.FieldDeletionStateCode, 0)); // 把所有的数据都缓存起来的代码 BaseParameterManager manager = new BaseParameterManager(); using (var redisClient = PooledRedisHelper.GetClient()) { using (IDataReader dataReader = manager.ExecuteReader(parameters)) { while (dataReader.Read()) { string key = "MAC:" + dataReader[BaseParameterEntity.FieldParameterId].ToString(); string macAddress = dataReader[BaseParameterEntity.FieldParameterContent].ToString().ToLower(); redisClient.AddItemToSet(key, macAddress); redisClient.ExpireEntryAt(key, DateTime.Now.AddMonths(3)); result++; System.Console.WriteLine(result.ToString() + " : " + macAddress); } dataReader.Close(); } } return(result); }
// // 获取用户的最新 OpenId 的方法 // public static string GetUserOpenId(BaseUserInfo userInfo, string cachingSystemCode = null, bool useCaching = true, bool useDataBase = false, bool useUserCenterHost = true) { string result = string.Empty; if (useCaching && userInfo != null && !string.IsNullOrWhiteSpace(userInfo.Id)) { // 2015-11-20 吉日嘎拉 为了编译通过进行改进 using (var redisClient = PooledRedisHelper.GetClient()) { string key = string.Empty; if (string.IsNullOrEmpty(cachingSystemCode)) { key = "userId:" + userInfo.Id; } else { key = "userId:" + cachingSystemCode + ":" + userInfo.Id; } result = redisClient.Get <string>(key); // 2016-04-05 吉日嘎拉 这里代码有错误,不为空时进行处理 if (!string.IsNullOrWhiteSpace(result)) { userInfo.OpenId = result; HttpContext.Current.Session[DotNet.Business.Utilities.SessionName] = userInfo; HttpContext.Current.Session["openId"] = userInfo.OpenId; // 2016-04-05 吉日嘎拉,这里可以提前结束了,提高效率 return(result); } } if (useDataBase) { BaseUserLogOnManager userLogOnManager = new Business.BaseUserLogOnManager(userInfo); result = userLogOnManager.GetUserOpenId(userInfo, cachingSystemCode); } if (string.IsNullOrEmpty(result) && useUserCenterHost) { result = LogOnUtilities.GetUserOpenId(userInfo, cachingSystemCode); } // 从数据库获取,这里要考虑读写分离的做法 if (!string.IsNullOrWhiteSpace(result)) { userInfo.OpenId = result; HttpContext.Current.Session[DotNet.Business.Utilities.SessionName] = userInfo; HttpContext.Current.Session["openId"] = userInfo.OpenId; // 这里这样处理一下,提高下次处理的效率 if (useCaching) { SetUserOpenId(userInfo.Id, userInfo.OpenId, cachingSystemCode); } } } return(result); }
public static string[] ResetPermissionByCache(string systemCode, string roleId) { string key = "Permission:" + systemCode + ":Role:" + roleId; using (var redisClient = PooledRedisHelper.GetPermissionClient()) { redisClient.Remove(key); } return(GetPermissionIdsByCache(systemCode, new string[] { roleId })); }
private static void SetListCache(string key, List <BaseContactEntity> list) { if (!string.IsNullOrWhiteSpace(key) && list != null && list.Count > 0) { using (var redisClient = PooledRedisHelper.GetClient()) { redisClient.Set <List <BaseContactEntity> >(key, list, DateTime.Now.AddMinutes(10)); } } }
public static string[] ResetPermissionByCache(string systemCode, string organizeId) { string key = "Permission:" + systemCode + ":Organize:" + organizeId; using (var redisClient = PooledRedisHelper.GetPermissionClient()) { redisClient.Remove(key); } return(GetPermissionIdsByCache(systemCode, organizeId)); }
/// <summary> /// 添加短信,可以发给多个人 /// </summary> /// <param name="entity">实体</param> /// <param name="receiverIds">接收者主键组</param> /// <param name="saveSend">保存每个发送记录</param> /// <returns>影响行数</returns> public int Send(BaseMessageEntity entity, string[] receiverIds, bool saveSend = true) { int result = 0; using (var redisClient = PooledRedisHelper.GetMessageClient()) { result = Send(redisClient, entity, receiverIds, saveSend); } return(result); }
/// <summary> /// 缓存中加载用户的MAC /// </summary> /// <param name="userId">用户主键</param> /// <returns>影响行数</returns> public static int CachePreheatingMACAddressByUser(string userId) { int result = 0; using (var redisClient = PooledRedisHelper.GetClient()) { result = CachePreheatingMACAddressByUser(redisClient, userId); } return(result); }
private static void SetListCache(string key, List <BaseItemDetailsEntity> list) { if (!string.IsNullOrWhiteSpace(key) && list != null) { using (var redisClient = PooledRedisHelper.GetClient()) { // 2016-03-14 其实缓存10分钟也已经非常好了,就是缓存5分钟其实也足够好了 redisClient.Set <List <BaseItemDetailsEntity> >(key, list, DateTime.Now.AddMinutes(10)); } } }
public static void SetUserOpenId(string userId, string openId, string cachingSystemCode = null) { if (!string.IsNullOrWhiteSpace(userId) && !string.IsNullOrWhiteSpace(openId)) { // 2015-11-20 吉日嘎拉 为了编译通过进行改进 using (var redisClient = PooledRedisHelper.GetClient()) { SetUserOpenId(redisClient, userId, openId, cachingSystemCode); } } }