Advertisement
stak441

StudentPortal - appCache example 2

Jan 13th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.Caching;
  4. using System.Web.Mvc;
  5. using StudentPortal.Repository;
  6.  
  7. namespace StudentPortal.Controllers
  8. {
  9.     public class HomeController : Controller
  10.     {
  11.         private BooksRepository _booksRepository = new BooksRepository();
  12.  
  13.         public ActionResult Index()
  14.         {
  15.             if (HttpContext.Cache["allBooks"] == null)
  16.             {
  17.                 HttpContext.Cache.Insert("allBooks", _booksRepository.GetAll(), null,
  18.                     DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
  19.             }
  20.  
  21.             IEnumerable<Book> allBooks = HttpContext.Cache["allBooks"] as IEnumerable<Book>;
  22.             return View(allBooks);
  23.         }
  24.  
  25.         public ActionResult About()
  26.         {
  27.             ViewBag.SessionValue = Session["test"] != null ? Session["test"].ToString() : "Session is empty";
  28.            
  29.             return View();
  30.         }
  31.  
  32.         public ActionResult Contact()
  33.         {
  34.             ViewBag.Message = "Your contact page.";
  35.  
  36.             return View();
  37.         }
  38.  
  39.         [Authorize]
  40.         public ActionResult MyPage()
  41.         {
  42.             var currentUser = User.Identity.Name;
  43.             Session["test"] = "Session contains data for user " + currentUser;
  44.  
  45.             ViewBag.UserName = currentUser;
  46.             if (HttpContext.Cache[currentUser] == null)
  47.             {
  48.                 HttpContext.Cache.Insert(currentUser, _booksRepository.GetBooksForUser(currentUser), null,
  49.                     DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
  50.             }
  51.  
  52.             ViewBag.BooksRead = HttpContext.Cache[currentUser] as IEnumerable<Book>;
  53.  
  54.             return View();
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement