0
Answer

Caching n webapi

Davis India

Davis India

10y
610
1
I have got a requirement in which i have to write a service which accesses data from third party data source and one of the instructions given by client is
Calling the third party data source is costly and data only gets updated every 24 hours. 
I have written following methods keeping in mind of above instuction but i am not sure is it correct or what more i can do for better performance in terms of caching or how to handle caching in webapis.

  /// <summary>
        /// Returns the sorted list of movies
        /// </summary>
        /// <returns>Collection of Movies</returns>
        [CacheOutput(ClientTimeSpan = 86400, ServerTimeSpan = 86400)]
        public IEnumerable<Movie> Get()
        {
            return repository.GetMovies().OrderBy(c => c.MovieId);
        }

        /// <summary>
        /// Returns a movie
        /// </summary>
        /// <param name="movie">movieId</param>
        /// <returns>Movie</returns>
        [CacheOutput(ClientTimeSpan = 86400, ServerTimeSpan = 86400)]
        public Movie Get(int movieId)
        {
                var movie = repository.GetMovieById(movieId);
                if (movie == null)
                {
                    var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.NotFound)
                    {
                        Content = new StringContent(string.Format("No movie with ID = {0}", movieId)),
                        ReasonPhrase = "Movie ID Not Found"
                    };
                    throw new HttpResponseException(httpResponseMessage);
                }
                return movie;
        }