0

I need to do a multiple caching with different timings in the single webapi call .is it possible in webapi using memory caching.

In Http caching , in single request need to set single expiration, , there are 2 caching, one is for 5 minutes and another for 2 hours in single http request.

In 5 minute caching set in webapi method, but if cache is not available , it will hit other external service url be:https ://www.domain.com/api/sss/ss/ But webapi should call each time ,but external webapi should call only cache expires.which means after 5th second. inside the webapi 2 hours caching , it is memory caching.

 public IHttpActionResult getProduct()
        {

             // cache for 5 minutes:

             if( 5 min cache == null){


                 // call another services url
                 }

        else{
         // taken from  5 MIN cache.
         }


   // cache available take from 5 minutes cache data.

        next : // cache for 2 hours 

             if(2  hours cache != null){
               // get an original data

             }
       //cache available take from 2 hours cache data.




       // add the values in single response as json.
          return  response;

        }
Mohamed Sahir
  • 2,482
  • 8
  • 40
  • 71
  • Seems like you could simply use different key values (e.g. prod id = 123, 5 min key = 123, 2 hours key = 123_2hours) and expiration policies for different time periods. – Sixto Saez Feb 02 '18 at 15:45
  • @sixto saez, yes the functionality is working fine problem is when i tested with webapi tools like fiddler and postman, it gets expired after 5 minutes and after 5 minutes it hits again, but when test with browser like chrome ,first time it hit into webapi and cache the data after expiration of cache 6th minute i am triggered the webapi , but it is not hit the webapi and taken from cache. – Mohamed Sahir Feb 05 '18 at 06:55
  • You are describing the behavior of the [HTTP cache-control header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control), not the [service-side caching](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.caching.memorycache?view=netframework-4.7.1) logic in C#. You can only select a single expiration period for the client-side cache-control header. – Sixto Saez Feb 05 '18 at 15:08
  • as per my understanding , In Http caching , in single request need to only set single expiration in the header. But my case , webapi single method call , have to call other external service and it should be cached for 5 minutes and after 5 minutes it should again call the external service , within the 5 minutes it should be call webapi method , but it shouldn't calls external service because its cached for 5 min and another is 2 hours cache logic also there, that is from other source such as XML file,if the data is null in XML or related xml not available , take from cache for 2 hours – Mohamed Sahir Feb 06 '18 at 13:04
  • You'll need to add more code to your question to get a more precise answer. If you are using `HttpClient` to call external service, be aware that it **does not** honor the cache-control header sent from the external service. Any caching of responses from external services will need to be handled by your code. – Sixto Saez Feb 06 '18 at 16:11

0 Answers0