Beispiel #1
0
        public ActionResult Search(string wd = "", int page = 1, int size = 15)
        {
            var nul = new List <PostOutputDto>();

            ViewBag.Elapsed  = 0;
            ViewBag.Total    = 0;
            ViewBag.PageSize = size;
            ViewBag.Keyword  = wd;
            if (Regex.Match(wd ?? "", CommonHelper.BanRegex + "|" + CommonHelper.ModRegex).Length > 0)
            {
                return(RedirectToAction("Search"));
            }

            string ip  = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            string key = "Search:" + ip;

            if (CacheManager.Exists(key))
            {
                var hotSearches = RedisHelper.Get <List <KeywordsRankOutputDto> >("SearchRank:Week").Take(10).ToList();
                ViewBag.hotSearches = hotSearches;
                ViewBag.ErrorMsg    = "10秒内只能搜索1次!";
                return(View(nul));
            }

            wd = wd?.Trim().Replace("+", " ");
            if (!string.IsNullOrWhiteSpace(wd) && !wd.Contains("锟斤拷"))
            {
                if (!HttpContext.Session.TryGetValue("search:" + wd, out _) && !HttpContext.Request.IsRobot())
                {
                    SearchDetailsService.AddEntity(new SearchDetails
                    {
                        KeyWords   = wd,
                        SearchTime = DateTime.Now,
                        IP         = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString()
                    });
                    SearchDetailsService.SaveChanges();
                    HttpContext.Session.Set("search:" + wd, wd.ToByteArray());
                }

                var posts = PostService.SearchPage(page, size, wd);
                ViewBag.Elapsed = posts.Elapsed;
                ViewBag.Total   = posts.Total;
                if (posts.Total > 1)
                {
                    CacheManager.AddOrUpdate(key, wd, s => wd);
                    CacheManager.Expire(key, TimeSpan.FromSeconds(10));
                }

                ViewBag.hotSearches = new List <KeywordsRankOutputDto>();
                return(View(posts.Results));
            }

            ViewBag.hotSearches = RedisHelper.Get <List <KeywordsRankOutputDto> >("SearchRank:Week").Take(10).ToList();
            return(View(nul));
        }
        public ActionResult Search(string wd = "", int page = 1, int size = 15)
        {
            var nul = new List <PostDto>();

            ViewBag.Elapsed  = 0;
            ViewBag.Total    = 0;
            ViewBag.PageSize = size;
            ViewBag.Keyword  = wd;
            string ip  = ClientIP;
            string key = "Search:" + ip;

            if (CacheManager.Exists(key) && CacheManager.Get(key) != wd)
            {
                var hotSearches = RedisHelper.Get <List <KeywordsRank> >("SearchRank:Week").Take(10).ToList();
                ViewBag.hotSearches = hotSearches;
                ViewBag.ErrorMsg    = "10秒内只能搜索1次!";
                return(View(nul));
            }

            wd = wd?.Trim().Replace("+", " ");
            if (!string.IsNullOrWhiteSpace(wd) && !wd.Contains("锟斤拷"))
            {
                if (!HttpContext.Session.TryGetValue("search:" + wd, out _) && !HttpContext.Request.IsRobot())
                {
                    SearchDetailsService.AddEntity(new SearchDetails
                    {
                        KeyWords   = wd,
                        SearchTime = DateTime.Now,
                        IP         = ClientIP
                    });
                    SearchDetailsService.SaveChanges();
                    HttpContext.Session.Set("search:" + wd, wd.ToByteArray());
                }

                var posts = PostService.SearchPage(page, size, wd);
                ViewBag.Elapsed = posts.Elapsed;
                ViewBag.Total   = posts.Total;
                if (posts.Total > 1)
                {
                    CacheManager.AddOrUpdate(key, wd, s => wd);
                    CacheManager.Expire(key, TimeSpan.FromSeconds(10));
                }

                ViewBag.Ads         = AdsService.GetByWeightedPrice(AdvertiseType.PostList);
                ViewBag.hotSearches = new List <KeywordsRank>();
                return(View(posts.Results));
            }

            ViewBag.hotSearches = RedisHelper.Get <List <KeywordsRank> >("SearchRank:Week").Take(10).ToList();
            ViewData["page"]    = new Pagination(page, size);
            return(View(nul));
        }