0

I have to implement caching in asp.net web api methods because i am accessing data from third party datasource and Calling the third party data source is costly and data only gets updated every 24 hours.So with the help of Strathweb.I have implemented caching like this

    /// <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;
}

but in Strathweb,i have seen two attributes one is ClientTimeSpan and Other is ServerTimeSpan.I am not sure when to use ClientTimeSpan and when to use ServerTimeSpan.In the simplest term i want to understand when to use serverside caching and when to use clientside caching and what are the diffrences between both.

F11
  • 3,703
  • 12
  • 49
  • 83
  • [Similar Question on SO](http://stackoverflow.com/questions/17287144/how-to-cache-net-web-api-requests-use-w-angularjs-http) may help you. – शेखर Oct 08 '14 at 05:01

2 Answers2

2

As the definition says

ClientTimeSpan (corresponds to CacheControl MaxAge HTTP header)

ServerTimeSpan (time how long the response should be cached on the server side)

Code sample , with explanation.

//Cache for 100s on the server, inform the client that response is valid for 100s
[CacheOutput(ClientTimeSpan = 100, ServerTimeSpan = 100)]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}


//Inform the client that response is valid for 50s. Force client to revalidate.
[CacheOutput(ClientTimeSpan = 50, MustRevalidate = true)]
public string Get(int id)
{
    return "value";
}

SOURCE

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
2

ClientTimeSpan

Use client side caching if you want to allow clients (usually browsers) to cache your data locally on the users computer. The benefits are that the clients may not requests your API until the cache expires. On the other side you can't invalidate this cache because it is stored on the client side. Use this cache for non-dynamic/not frequently changing data.

ServerTimeSpan

Stores data on your server. You can easily invalidate this cache but it needs some resources (memory)

Marian Ban
  • 8,158
  • 1
  • 32
  • 45