3

I would like to start caching some of the data in web API But I am getting some errors. I am using the HttpContent and storing it in cache using the CacheHelper.SaveTocache. On the next connection I am checking if it in cache then it’s creating a new httpmessage and setting the httpcontent for the new message but I am getting some errors ? How can I cache a httpmessage?

Errors when trying to get the data from cache

Error: read ECONNRESET
This site can’t be reached

Controller

    public class UserDataController : ApiController
    {
        [System.Web.Http.HttpGet]
        public IHttpActionResult GetUser(string accessKey, string id)
        {
   
           DateTime cacheDuration = DateTime.Now.AddSeconds(cacheDurationTime);
           string cacheKey = String.Format("{0}_{1}", accessKey, id);
           if (CacheHelper.GetObjectFromCach(cacheKey) != null){
              var cacheContent = CacheHelper.GetFromCache<HttpContent>(cacheKey);

              var response = this.Request.CreateResponse(HttpStatusCode.OK);
              response.Content = cacheContent;

              return ResponseMessage(response);

          else {
            var Results = User.UserFromId(id);

            CacheHelper.SaveTocache(cacheKey, Results.Content, cacheDuration);
            return ResponseMessage(Results);
       
          }
        }

    }

Helpers

   public static void SaveTocache(string cacheKey, object savedItem, DateTime absoluteExpiration)
   {
        MemoryCache.Default.Add(cacheKey, savedItem, absoluteExpiration);
   }


   public static T GetFromCache<T>(string cacheKey) where T : class
   {
        return MemoryCache.Default[cacheKey] as T;
   }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.