I'm given the task of programming a website and I'm new to website creation.
The website has to show an overview of trips and allow navigation to a single trips detail and this trips places. Our databaselayer will return a list with trips and all its places:
class Trip { List<Place> places; }
List<Trip> trip = datalayer.GetTrips();
So the request will already contain the trips and all the places. When we want to show a single trip or place it's not necessary to go to the DB. Is it proper to store the trips list in the cache and then use the cache when showing a trip or one of its places?
Example code:
//GET /Trips
public ActionResult Index()
{
List<Trip> tripList;
_dbLayer.GetTrips(out tripList);
HttpContext.Current.Cache.["Trips-" + clientId] = tripList;
return View(tripList);
}
//GET: /Trips/Details/5
public ActionResult Detail(int id)
{
List<Trip> tripList = HttpContext.Current.Cache.["Trips-" + clientId];
if(tripList != null)
{
Trip trip = tripList.SingleOrDefault(i => i.TechNr == id)
return View(trip);
}
else
{
return View("NotFound");
}
}