0

What is the best way to cache Queryable result if every call need to calculate lot of things and return it to client.

Code Sample

 [Queryable]
 public IQueryable<Car> Get()
 {
    try
    {
    var result=GetCarList();
    //GetCarList() calculation is  taking  around 1 min

    return result.AsQueryable();
    } 
 }

GetCarList()
{
var query = from car in db.CarDetail
            where car.color == "white"
            select car;

//10k records of white cars are selected with out considering makers 
//white is mandatory
foreach (var car in query)
{
  //Processing each record in every call
}
}

Query sample

First Page

localhost/api/Car?$filter=(make eq 'ford')&$orderby=carid desc&$top=10

Second Page

localhost/api/Car?$filter=(make eq 'ford')&$orderby=carid desc&$top=10$skip=10

Third Page

localhost/api/Car?$filter=(make eq 'ford')&$orderby=carid desc&$top=10$skip=20

Every time each call is taking 1 min even though the calculation is same for current filter. what is the best way to cache this kind of api call?

niknowj
  • 977
  • 4
  • 19
  • 37
  • possible duplicate of [How to use caching in ASP.NET Web API?](http://stackoverflow.com/questions/14811772/how-to-use-caching-in-asp-net-web-api) – JotaBe May 20 '15 at 11:45
  • I saw those options but there caching is based on full url. here every time url is different but filter is same – niknowj May 20 '15 at 11:51

1 Answers1

0

As the OP explains in his comment, the object to cache is the list returned by the call to GetCarList(); and the result is always the same.

You can simply store this in Cache, see docs: Cache Class.

When you need it, check if it's in cache. If not, create it and store in cache before using (anywhere you want to use it). As the Cache is thread safe you will not have concurernty problems by accesing it from different requests.

JotaBe
  • 38,030
  • 8
  • 98
  • 117