public JsonResponse GetListBlog([FromBody] BaseViewModel model) { try { var dicwhere = new Dictionary <string, object>() { { nameof(Bloginfo.IsDeleted), 0 }, { nameof(Bloginfo.States), 0 }, }; if (model.KID > 0) { dicwhere.Add(nameof(Bloginfo.Type), model.KID); } string key = ConfigUtil.BlogListCacheKey; var cacheobj = CacheHelper.GetCacheItem(key)?.ToString() ?? ""; List <Bloginfo> alllist = new List <Bloginfo>(); if (!string.IsNullOrEmpty(cacheobj)) { alllist = CacheHelper.GetCacheItem(key)?.DeserialObjectToList <Bloginfo>(); } int cut = 0; if (alllist == null || alllist.Count == 0) { var chaxun = BlogHelper.GetJsonListPage_Bloginfo(1, 1000, "CreateTime desc", new Dictionary <string, object>() { { nameof(Bloginfo.IsDeleted), 0 }, { nameof(Bloginfo.States), 0 } }); if (chaxun.code.Toint() != 0) { return(new JsonResponse { Code = chaxun.code.Toint(), Data = "", Msg = chaxun.msg }); } if (chaxun.data != null && chaxun.data.Count > 0) { alllist = chaxun.data; CacheHelper.AddCacheItem(ConfigUtil.BlogListCacheKey, chaxun.data.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High); } } List <Bloginfo> retlist = null; if (model.KID > 0) { retlist = alllist.Where(x => x.Type == model.KID)?.ToList(); } else { retlist = alllist; } cut = retlist.Count; retlist = retlist?.Skip((model.Page - 1) * model.Limit).Take(model.Limit).ToList(); #region 统计访问地址信息 Task.Run(() => { if (model.KID <= 0) { var adddic = new Dictionary <string, object>() { { nameof(Access.AccessType), 0 }, { nameof(Access.IpAddress), GetIP() }, { nameof(Access.CreateTime), DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }, { nameof(Access.CreateUserId), 1 }, { nameof(Access.CreateUserName), "system" } }; BlogHelper.Add_Access(adddic, new OpertionUser { }); } }); #endregion return(new JsonResponse { Code = 0, Data = retlist, Count = cut }); } catch (Exception ex) { LogHelper.WriteLog(ex, "BlogController/GetListBlog"); return(new JsonResponse { Code = 1, Msg = "程序好像开小差了" + ex.Message }); } }
public JsonResponse GetItemBlog([FromBody] BaseViewModel model) { try { if (string.IsNullOrEmpty(model.Num)) { return(new JsonResponse { Code = 1, Msg = "参数不合法" }); } string key = $"{CJJ.Blog.Apiv2.Models.ConfigUtil.BlogItemCacheKeyPrefix}{model.Num}"; BloginfoView bloginfoView = CacheHelper.GetCacheItem(key)?.ToString()?.DeserialObject <BloginfoView>(); if (bloginfoView == null) { bloginfoView = BlogHelper.GetModelByNum(model.Num); CacheHelper.AddCacheItem(key, bloginfoView.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High); } bloginfoView.Views += 1; #region 处理list 和 item 缓存 Task.Run(() => { //CacheHelper.DelCacheItem(key); CacheHelper.AddCacheItem(key, bloginfoView.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High); string alllistkey = ConfigUtil.BlogListCacheKey; string allinfo = CacheHelper.GetCacheItem(alllistkey)?.ToString(); List <Bloginfo> cachelist = allinfo?.DeserialObjectToList <Bloginfo>(); Bloginfo info = cachelist.FirstOrDefault(x => x.BlogNum == model.Num); if (cachelist != null && cachelist.Count > 0 && info != null && info.KID > 0) { cachelist.FirstOrDefault(x => x.BlogNum == model.Num).Views += 1; //CacheHelper.DelCacheItem(alllistkey); CacheHelper.AddCacheItem(alllistkey, cachelist.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High); } }); #endregion #region 异步添加访问次数,统计访问地址信息 Task.Run(() => { try { var dic = new Dictionary <string, object>() { { nameof(Bloginfo.BlogNum), model.Num }, { nameof(Bloginfo.IsDeleted), 0 } }; var bloginfomodel = BlogHelper.GetModelByWhere_Bloginfo(dic); BlogHelper.UpdateNums_Bloginfo(nameof(Bloginfo.Views), 1, dic, new Service.Models.View.OpertionUser()); var adddic = new Dictionary <string, object>() { { nameof(Access.AccessType), 1 }, { nameof(Access.IpAddress), GetIP() }, { nameof(Access.BlogName), bloginfoView.Title }, { nameof(Access.BlogNum), model.Num }, { nameof(Access.CreateTime), DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }, { nameof(Access.CreateUserId), 1 }, { nameof(Access.CreateUserName), "system" } }; BlogHelper.Add_Access(adddic, new OpertionUser { }); } catch (Exception ex) { LogHelper.WriteLog(ex, "BlogController/GetItemBlog 访问次数"); } }); #endregion return(new JsonResponse { Code = bloginfoView != null ? 0 : 1, Data = bloginfoView != null ? bloginfoView : null }); } catch (Exception ex) { LogHelper.WriteLog(ex, "BlogController/GetListBlog"); return(new JsonResponse { Code = 1, Msg = "程序好像开小差了" + ex.Message }); } }