/// <summary> /// 获取县区 /// </summary> /// <param name="userInfo">用户</param> /// <param name="city">城市</param> /// <returns>县区数组</returns> public string[] GetDistrict(BaseUserInfo userInfo, string city) { var parameter = ServiceParameter.CreateWithMessage(userInfo , MethodBase.GetCurrentMethod() , this.serviceName , AppMessage.OrganizeService_GetDistrict); string[] result = null; ServiceUtil.ProcessUserCenterReadDb(parameter, (dbHelper) => { System.Web.Caching.Cache cache = HttpRuntime.Cache; string cacheObject = "OrganizeDistrict" + city; if (cache == null || cache[cacheObject] == null) { lock (locker) { if (cache == null || cache[cacheObject] == null) { var manager = new BaseOrganizeManager(dbHelper, userInfo); result = manager.GetDistrict(city); cache.Add(cacheObject, result, null, DateTime.Now.AddHours(8), TimeSpan.Zero, CacheItemPriority.Normal, null); } } } result = cache[cacheObject] as string[]; }); return(result); }
/// <summary> /// 获取县区 /// </summary> /// <param name="userInfo">用户</param> /// <param name="province">省份</param> /// <param name="city">城市</param> /// <returns>县区数组</returns> public string[] GetDistrict(BaseUserInfo userInfo, string province, string city) { string[] result = null; #if Redis result = BaseOrganizeManager.GetDistrictByCache(province, city); #else var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod()); ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) => { System.Web.Caching.Cache cache = HttpRuntime.Cache; string cacheObject = "AreaOrganizeDistrict" + city; if (cache != null && cache[cacheObject] == null) { // BaseAreaManager areaManager = new BaseAreaManager(dbHelper, result); // result = areaManager.GetDistrictList(cityId); var manager = new BaseOrganizeManager(dbHelper, userInfo); result = manager.GetDistrict(province, city); cache.Add(cacheObject, result, null, DateTime.Now.AddHours(8), TimeSpan.Zero, CacheItemPriority.Normal, null); } result = cache[cacheObject] as string[]; }); #endif return(result); }
/// <summary> /// 获取县区 /// 2015-11-25 吉日嘎拉 采用缓存方式,效率应该会更高 /// </summary> /// <param name="province">省份</param> /// <param name="city">城市</param> /// <returns>县区数组</returns> public static string[] GetDistrictByCache(string province, string city) { string[] result = null; string district = string.Empty; if (string.IsNullOrWhiteSpace(province)) { province = string.Empty; } if (string.IsNullOrWhiteSpace(city)) { city = string.Empty; } // string key = "OrganizeDistrict:" + province + ":" + city; string key = "OD:" + province + ":" + city; using (var redisClient = PooledRedisHelper.GetReadOnlyClient()) { district = redisClient.Get <string>(key); } if (!string.IsNullOrWhiteSpace(district)) { result = district.Split('.'); } else { // 从数据库读取数据 BaseOrganizeManager organizeManager = new BaseOrganizeManager(); result = organizeManager.GetDistrict(province, city); // 设置缓存 if (result != null && result.Length > 0) { district = string.Join(".", result); using (var redisClient = PooledRedisHelper.GetClient()) { redisClient.Set <string>(key, district, DateTime.Now.AddHours(4)); } } } return(result); }