views:

36

answers:

2

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");
    }
 }
+2  A: 

If you're using the Linq to SQL datacontext class, it automatically caches the data for you. I would suggest leaving the caching at this layer, instead of caching in the controller.

C. Ross
A: 

I will not recommend (in this case) as sql server is also doing caching. Another disadvantage is that on the database side, if the row is updated, you still have dirty record and the chance of redundant data increases. Although you can use SQL Server Cache Invalidation, but it takes some other steps explained here by Scott Hansellman

Adeel
Very original approach : ) +1Anyway I agree with Adeel.
SDReyes