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="entity">用户实体</param> public static void SetCache(BaseUserEntity entity) { using (var redisClient = PooledRedisHelper.GetClient()) { SetCache(redisClient, entity); } }
/// <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(); }
// // 获取用户的最新 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); }
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)); } } }
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)); } } }
/// <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); }
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); } } }
/// <summary> /// 添加之后,需要重新刷新缓存,否则其他读取数据的地方会乱了,或者不及时了 /// 宋彪 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> public int AfterAdd(BaseDepartmentEntity entity) { int result = 0; // 2016-01-28 更新用户缓存 using (var redisClient = PooledRedisHelper.GetClient()) { BaseDepartmentManager.CacheContractAreaPreheatingSpelling(redisClient, entity); } return(result); }
/// <summary> /// 快速模糊检索承包区(高速缓存) 宋彪 /// /// 01:网点承包区缓存数据方法 prefix = "ContractArea:" + companyId + ":" /// /// </summary> /// <param name="prefix">搜索前缀</param> /// <param name="key">搜搜关键字</param> /// <param name="returnId">返回主键</param> /// <param name="showCode">返回编号</param> /// <param name="topLimit">返回数量</param> /// <returns>查询结果数组</returns> public static List <KeyValuePair <string, string> > GetContractAreasByKey(string prefix, string key, bool returnId = true, bool showCode = false, int topLimit = 20) { List <KeyValuePair <string, string> > result = new List <KeyValuePair <string, string> >(); if (string.IsNullOrEmpty(key)) { return(result); } // 全部转小写进行比对 key = prefix + key.ToLower(); using (var redisClient = PooledRedisHelper.GetClient()) { // 每次从有序集合中的消息取出20条信息,返回给客户端,在客户端展示阅读成功后才从内存里去掉。 List <string> list = redisClient.GetRangeFromSortedSet(key, 0, topLimit); if (list != null) { for (int i = 0; i < list.Count; i++) { // 从内存里直接获取这个对应的消息 string[] contractArea = list[i].Split(';'); string id = contractArea[0]; string code = contractArea[1]; string fullName = contractArea[2]; if (returnId) { if (showCode) { result.Add(new KeyValuePair <string, string>(id, fullName + " " + code)); } else { result.Add(new KeyValuePair <string, string>(id, fullName)); } } else { if (showCode) { result.Add(new KeyValuePair <string, string>(code, fullName + " " + code)); } else { result.Add(new KeyValuePair <string, string>(code, fullName)); } } } } } return(result); }
public static void SetParameterByCache(string tableName, BaseParameterEntity entity) { string key = "Parameter:" + tableName + ":" + entity.CategoryCode + ":" + entity.ParameterId + ":" + entity.ParameterCode; using (var redisClient = PooledRedisHelper.GetClient()) { if (!string.IsNullOrEmpty(entity.ParameterContent)) { redisClient.Set <string>(key, entity.ParameterContent, DateTime.Now.AddMinutes(10)); } } }
private static void SetCache(string tableName, BaseItemDetailsEntity entity) { if (entity != null) { string key = string.Empty; using (var redisClient = PooledRedisHelper.GetClient()) { key = "ItemDetails:" + tableName + ":" + entity.Id; redisClient.Set <BaseItemDetailsEntity>(key, entity, DateTime.Now.AddMinutes(10)); } } }
private static void SetCache(BaseAreaEntity entity) { if (entity != null && !string.IsNullOrWhiteSpace(entity.Id)) { string key = string.Empty; using (var redisClient = PooledRedisHelper.GetClient()) { key = "Area:" + entity.Id; redisClient.Set <BaseAreaEntity>(key, entity, DateTime.Now.AddMinutes(10)); } } }
/// <summary> /// 移除缓存 /// 2015-12-14 吉日嘎拉 增加移除缓存的功能 /// </summary> /// <param name="companyId">公司主键</param> /// <param name="topLimit">获取前几个</param> public static void RemoveTopListByCompanyIdCache(string companyId, int topLimit) { string key = "BaseContact"; if (!string.IsNullOrEmpty(companyId)) { key = key + "_" + companyId + "_" + topLimit.ToString(); } using (var redisClient = PooledRedisHelper.GetClient()) { redisClient.Remove(key); } }
/// <summary> /// 根据key值获取缓存对象 /// </summary> /// <param name="key"></param> /// <returns></returns> private static BaseUserLogonExtendEntity GetCache(string key) { BaseUserLogonExtendEntity result = null; if (!string.IsNullOrWhiteSpace(key)) { using (var redisClient = PooledRedisHelper.GetClient()) { result = redisClient.Get <BaseUserLogonExtendEntity>(key); } } return(result); }
/// <summary> /// 将对象设置到缓存中 /// </summary> /// <param name="entity"></param> public static void SetCache(BaseUserLogonExtendEntity entity) { string key = string.Empty; if (entity != null && !string.IsNullOrWhiteSpace(entity.Id.ToString())) { using (var redisClient = PooledRedisHelper.GetClient()) { key = cacheKeyPrefix + entity.Id; redisClient.Set <BaseUserLogonExtendEntity>(key, entity, DateTime.Now.AddMinutes(10)); } } }
public static BaseUserContactEntity GetCacheByKey(string key) { BaseUserContactEntity result = null; if (!string.IsNullOrWhiteSpace(key)) { using (var redisClient = PooledRedisHelper.GetClient()) { result = redisClient.Get <BaseUserContactEntity>(key); } } return(result); }
public static bool RemoveCache(string key) { bool result = false; if (!string.IsNullOrWhiteSpace(key)) { using (var redisClient = PooledRedisHelper.GetClient()) { result = redisClient.Remove(key); } } return(result); }
private static List <BaseAreaEntity> GetListCache(string key) { List <BaseAreaEntity> result = null; if (!string.IsNullOrWhiteSpace(key)) { using (var redisClient = PooledRedisHelper.GetClient()) { result = redisClient.Get <List <BaseAreaEntity> >(key); } } return(result); }
public static string[] GetUserByOrganizeByCache(string companyId, string departmentId = null) { string[] result = null; if (string.IsNullOrEmpty(companyId)) { return(result); } string key = string.Empty; string users = string.Empty; if (string.IsNullOrEmpty(departmentId)) { departmentId = string.Empty; key = "OU:" + companyId; } else { key = "OU:" + companyId + ":" + departmentId; } using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { users = redisClient.Get <string>(key); if (!string.IsNullOrEmpty(users)) { result = users.Split(','); } } if (result == null) { BaseMessageManager manager = new BaseMessageManager(); result = manager.GetUserByOrganize(companyId, departmentId); // 若是空的不用缓存,继续读取实体 if (result != null) { users = string.Join(",", result); using (var redisClient = PooledRedisHelper.GetClient()) { redisClient.Set <string>(key, users, DateTime.Now.AddDays(1)); } } } return(result); }
/// <summary> /// 从缓存中去掉MAC限制 /// 防止重复查询数据库? /// </summary> /// <param name="userId">用户主键</param> public static bool ResetMACAddressByCache(string userId) { bool result = false; if (!string.IsNullOrEmpty(userId)) { string key = "MAC:" + userId; using (var redisClient = PooledRedisHelper.GetClient()) { result = redisClient.Remove(key); } } return(result); }
/// <summary> /// 获得公司列表 /// 2015-11-25 吉日嘎拉 进行改进 /// </summary> /// <param name="province">省</param> /// <param name="city">城市</param> /// <param name="district">县区</param> /// <returns>数据表</returns> public static string[] GetOrganizeByDistrictByCache(string province, string city, string district) { string[] result = null; string organize = string.Empty; if (string.IsNullOrWhiteSpace(province)) { province = string.Empty; } if (string.IsNullOrWhiteSpace(city)) { city = string.Empty; } if (string.IsNullOrWhiteSpace(district)) { district = string.Empty; } // string key = "OrganizeByDistrict:" + province + ":" + city + ":" + district; string key = "OBD:" + province + ":" + city + ":" + district; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { organize = redisClient.Get <string>(key); } if (!string.IsNullOrWhiteSpace(organize)) { result = organize.Split('.'); } else { // 从数据库读取数据 BaseOrganizeManager organizeManager = new BaseOrganizeManager(); result = organizeManager.GetOrganizeByDistrict(province, city, district); // 设置缓存 if (result != null && result.Length > 0) { organize = string.Join(".", result); using (var redisClient = PooledRedisHelper.GetClient()) { redisClient.Set <string>(key, organize, DateTime.Now.AddHours(4)); } } } return(result); }
/// <summary> /// 检查用户的 服务访问限制 /// </summary> /// <param name="userId">用户主键</param> /// <param name="privateKey">私钥</param> /// <returns>正确</returns> public static bool CheckServiceByCache(string userId, string privateKey) { // 默认是不成功的,防止出错误 bool result = false; // 检查参数的有效性 if (string.IsNullOrEmpty(userId)) { return(result); } if (string.IsNullOrEmpty(privateKey)) { return(result); } using (var redisClient = PooledRedisHelper.GetClient()) { string key = "User:"******":Service"; // 若是缓存里过期了? if (!redisClient.ContainsKey(key)) { // 重新缓存用户的限制数据 if (CachePreheatingServiceByUser(redisClient, userId) == 0) { result = false; } } // 若还是没有?表示是新增的 if (redisClient.ContainsKey(key)) { // 若已经存在,就需要进行缓存里的判断? // 这里要提高效率,不能反复打开缓存 result = redisClient.SetContainsItem(key, privateKey); if (!result) { // 若没有验证成功、应该记录日志。 } } } return(result); }
/// <summary> /// 承包区缓存预热 宋彪 /// 网点下的承包区查询条件:CATEGORYCODE = 7 AND and COMPANYID=‘网点id’ /// 找到所有承包区的条件 /// CATEGORYCODE = 7 AND COMPANYID IN(SELECT ID FROM BASEORGANIZE) /// </summary> /// <param name="flushDb"></param> public static void CacheContractAreaPreheatingSpelling(bool flushDb = false) { // 组织机构数据缓存预热实现 BaseDepartmentManager departmentManager = new Business.BaseDepartmentManager(); // 减少数据库连接、减少内存站用、一边度取、一边设置缓存,只读取需要的数据 departmentManager.SelectFields = BaseDepartmentEntity.FieldCompanyId + " , " + BaseDepartmentEntity.FieldFullName + " , " + BaseDepartmentEntity.FieldManager + " , " + BaseDepartmentEntity.FieldManagerId + " , " + BaseDepartmentEntity.FieldEnabled + " , " + BaseDepartmentEntity.FieldDeletionStateCode; using (var redisClient = PooledRedisHelper.GetClient()) { if (flushDb) { redisClient.FlushDb(); } double score = 0; // 获取某个网点下的承包区 循环进行缓存 using (IDataReader dataReader = departmentManager.ExecuteReaderByWhere(" CATEGORYCODE = 7 AND COMPANYID IN (SELECT ID FROM BASEORGANIZE) AND DeletionStateCode=0 ", null, 0, BaseDepartmentEntity.FieldFullName)) { while (dataReader.Read()) { // 具体使用时 使用承包区主管的ID和承包区的名字,不要使用部门的ID,因为费用计算时是扣到具体的承包区主管的 承包区是属于具体网点 BaseDepartmentEntity entity = new BaseDepartmentEntity(); entity.ManagerId = dataReader[BaseDepartmentEntity.FieldManagerId].ToString(); entity.Code = dataReader[BaseDepartmentEntity.FieldCode].ToString(); entity.FullName = dataReader[BaseDepartmentEntity.FieldFullName].ToString(); entity.CompanyId = dataReader[BaseDepartmentEntity.FieldCompanyId].ToString(); score++; CacheContractAreaPreheatingSpelling(redisClient, entity, score); // 2016-02-02 吉日嘎拉 设置一下排序属性 departmentManager.SetProperty(entity.Id, new KeyValuePair <string, object>(BaseDepartmentEntity.FieldSortCode, score)); } } } }
private static void SetCache(string systemCode, BaseRoleEntity entity) { if (string.IsNullOrWhiteSpace(systemCode)) { systemCode = "Base"; } if (entity != null && !string.IsNullOrEmpty(entity.Id)) { string key = string.Empty; using (var redisClient = PooledRedisHelper.GetClient()) { key = systemCode + ".Role." + entity.Id; redisClient.Set <BaseRoleEntity>(key, entity, DateTime.Now.AddMinutes(20)); key = systemCode + ".Role." + entity.Code; redisClient.Set <BaseRoleEntity>(key, entity, DateTime.Now.AddMinutes(20)); } } }
/// <summary> /// 设置缓存 /// </summary> /// <param name="entity">实体</param> public static void SetCache(BaseOrganizeEntity entity) { if (entity != null && entity.Id != null) { string key = string.Empty; using (var redisClient = PooledRedisHelper.GetClient()) { // key = "Organize:" + entity.Id; key = "O:" + entity.Id; redisClient.Set <BaseOrganizeEntity>(key, entity, DateTime.Now.AddHours(16)); // key = "OrganizeByCode:" + entity.Code; key = "OBC:" + entity.Code; redisClient.Set <string>(key, entity.Id, DateTime.Now.AddHours(16)); //key = "OrganizeByName:" + entity.FullName; key = "OBN:" + entity.FullName; redisClient.Set <string>(key, entity.Id, DateTime.Now.AddHours(16)); } } }
private static List <BaseContactEntity> GetListCache(string key) { List <BaseContactEntity> result = null; if (!string.IsNullOrWhiteSpace(key)) { using (var redisClient = PooledRedisHelper.GetClient()) { result = redisClient.Get <List <BaseContactEntity> >(key); } if (result != null) { foreach (var entity in result) { entity.CreateOn = entity.CreateOn.Value.ToLocalTime(); } } } return(result); }
/// <summary> /// 把当前登录的用户信息缓存起来 /// </summary> /// <param name="userInfo">当前登录用户信息</param> /// <param name="cachingSystemCode">系统编号</param> public static bool SetUserInfoCaching(BaseUserInfo userInfo, string cachingSystemCode = null) { bool result = false; if (userInfo != null && !string.IsNullOrEmpty(userInfo.Id)) { // 2015-11-20 吉日嘎拉 为了编译通过进行改进 using (var redisClient = PooledRedisHelper.GetClient()) { SetUserOpenId(redisClient, userInfo.Id, userInfo.OpenId, cachingSystemCode); // 2016-03-24 吉日嘎拉、默认保存8个小时,时间太长了 if (string.IsNullOrEmpty(cachingSystemCode)) { DateTime expiresAt = DateTime.Now.AddHours(16); string key = "userInfo:" + userInfo.Id; result = redisClient.Set <BaseUserInfo>(key, userInfo, expiresAt); } } } return(result); }
/// <summary> /// 获取省份 /// 2015-11-25 吉日嘎拉 采用缓存方式,效率应该会更高 /// </summary> /// <param name="area">区域</param> /// <returns>省份数组</returns> public static string[] GetProvinceByCache(string area = null) { string[] result = null; if (string.IsNullOrWhiteSpace(area)) { area = string.Empty; } // string key = "OrganizeProvince:" + area; string key = "OP:" + area; string province = string.Empty; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { province = redisClient.Get <string>(key); } if (!string.IsNullOrWhiteSpace(province)) { result = province.Split('.'); } else { // 从数据库读取数据 BaseOrganizeManager organizeManager = new BaseOrganizeManager(); result = organizeManager.GetProvince(area); // 设置缓存 if (result != null && result.Length > 0) { province = string.Join(".", result); using (var redisClient = PooledRedisHelper.GetClient()) { redisClient.Set <string>(key, province, DateTime.Now.AddHours(4)); } } } return(result); }
/// <summary> /// 获取城市 /// 2015-11-25 吉日嘎拉 采用缓存方式,效率应该会更高 /// </summary> /// <param name="province">省份</param> /// <returns>城市数组</returns> public static string[] GetCityByCache(string province = null) { string[] result = null; string city = string.Empty; if (string.IsNullOrWhiteSpace(province)) { province = string.Empty; } // string key = "OrganizeCity:" + province; string key = "OC:" + province; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { city = redisClient.Get <string>(key); } if (!string.IsNullOrWhiteSpace(city)) { result = city.Split('.'); } else { // 从数据库读取数据 BaseOrganizeManager organizeManager = new BaseOrganizeManager(); result = organizeManager.GetCity(province); // 设置缓存 if (result != null && result.Length > 0) { city = string.Join(".", result); using (var redisClient = PooledRedisHelper.GetClient()) { redisClient.Set <string>(key, city, DateTime.Now.AddHours(4)); } } } return(result); }