public IPagedList <ClassifiedAdFavouriteList> GetMyFavouritedAds(int pageNumber = 1) { IEnumerable <int> cuf = new List <int>(); if (CurrentUser != null) { cuf = CacheHelper.GetFromCache <IEnumerable <int> >(string.Format("{0}-Favourites", CurrentUser.Id)); if (cuf == null) { using (var newthreadcontext = new ApplicationDbContext()) { cuf = newthreadcontext.Users.SingleOrDefault(x => x.Id == CurrentUser.Id).Favourites.Select(s => s.ClassifiedAdId); CacheHelper.SaveTocache(string.Format("{0}-Favourites", CurrentUser.Id), cuf, DateTime.Now.AddMinutes(5)); } } return(SearchEngineManager.GetUserFavouritedAds(cuf, pageNumber)); } else { var anonymousUserId = HttpContext.Current.Request.Cookies["MyFavourites"]?.Value?.ToString(); if (anonymousUserId != null) { cuf = CacheHelper.GetFromCache <IEnumerable <int> >(string.Format("{0}-Favourites", anonymousUserId)); if (cuf == null) { using (var newthreadcontext = new ApplicationDbContext()) { cuf = newthreadcontext.AnonymousUserDB.SingleOrDefault(x => x.Id.ToString() == anonymousUserId)?.Favourites.Select(s => s.ClassifiedAdId); // cleanup cookie if user does not exist in db if (cuf == null) { var deletecookie = new HttpCookie("MyFavourites", anonymousUserId) { Secure = true, HttpOnly = true, Expires = DateTime.Now.AddDays(-1) }; HttpContext.Current.Response.Cookies.Add(deletecookie); HttpContext.Current.Response.Cookies.Set(deletecookie); HttpContext.Current.Request.Cookies.Remove("MyFavourites"); return(null); } CacheHelper.SaveTocache(string.Format("{0}-Favourites", anonymousUserId), cuf, DateTime.Now.AddMinutes(5)); } } return(SearchEngineManager.GetUserFavouritedAds(cuf, pageNumber)); } } return(new List <ClassifiedAdFavouriteList>().ToPagedList(1, 1)); }