Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing data between actions

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");
    }
 }
like image 215
Carra Avatar asked Jun 11 '26 03:06

Carra


1 Answers

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.

like image 130
C. Ross Avatar answered Jun 13 '26 06:06

C. Ross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!