Asp.net webapi中的服务器端缓存和客户端缓存

Asp.net webapi中的服务器端缓存和客户端缓存,asp.net,asp.net-mvc,caching,asp.net-web-api,asp.net-web-api2,Asp.net,Asp.net Mvc,Caching,Asp.net Web Api,Asp.net Web Api2,我必须在asp.net web api方法中实现缓存,因为我从第三方数据源访问数据,调用第三方数据源的成本很高,而且数据每24小时才更新一次。因此,在的帮助下,我实现了这样的缓存 /// <summary> /// Returns the sorted list of movies /// </summary> /// <returns>Collection of Movies</returns> [Cach

我必须在asp.net web api方法中实现缓存,因为我从第三方数据源访问数据,调用第三方数据源的成本很高,而且数据每24小时才更新一次。因此,在的帮助下,我实现了这样的缓存

    /// <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;
}
//
///返回已排序的电影列表
/// 
///电影集
[CacheOutput(ClientTimeSpan=86400,ServerTimeSpan=86400)]
公共IEnumerable Get()
{
返回repository.GetMovies().OrderBy(c=>c.MovieId);
}
/// 
///返回一部电影
/// 
///电影ID
///电影
[CacheOutput(ClientTimeSpan=86400,ServerTimeSpan=86400)]
公共电影获取(int-movieId)
{
var movie=repository.GetMovieById(movieId);
如果(电影==null)
{
var httpResponseMessage=新的httpResponseMessage(HttpStatusCode.NotFound)
{
Content=newstringcontent(string.Format(“没有ID为{0},movieId的电影”),
ReasonPhrase=“未找到电影ID”
};
抛出新的HttpResponseException(httpResponseMessage);
}
回归电影;
}
但在Strathweb,我看到了两个属性,一个是ClientTimeSpan,另一个是ServerTimeSpan。我不确定何时使用ClientTimeSpan和何时使用ServerTimeSpan。最简单的说法是,我想了解何时使用服务器端缓存和何时使用客户端缓存,以及两者之间的区别。

正如定义所示

ClientTimeSpan
(对应于CacheControl MaxAge HTTP头)

ServerTimeSpan
(响应应在服务器端缓存多长时间)

代码示例,附带说明

//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";
}
//在服务器上缓存100s,通知客户端响应在100s内有效
[CacheOutput(ClientTimeSpan=100,ServerTimeSpan=100)]
公共IEnumerable Get()
{
返回新字符串[]{“value1”,“value2”};
}
//通知客户响应有效期为50秒。强制客户端重新验证。
[CacheOutput(ClientTimeSpan=50,MustRevalidate=true)]
公共字符串Get(int-id)
{
返回“值”;
}
正如定义所说

ClientTimeSpan
(对应于CacheControl MaxAge HTTP头)

ServerTimeSpan
(响应应在服务器端缓存多长时间)

代码示例,附带说明

//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";
}
//在服务器上缓存100s,通知客户端响应在100s内有效
[CacheOutput(ClientTimeSpan=100,ServerTimeSpan=100)]
公共IEnumerable Get()
{
返回新字符串[]{“value1”,“value2”};
}
//通知客户响应有效期为50秒。强制客户端重新验证。
[CacheOutput(ClientTimeSpan=50,MustRevalidate=true)]
公共字符串Get(int-id)
{
返回“值”;
}

ClientTimeSpan

如果希望允许客户端(通常是浏览器)在用户计算机上本地缓存数据,请使用客户端缓存。好处是,在缓存过期之前,客户端可能不会请求您的API。另一方面,您不能使该缓存无效,因为它存储在客户端。将此缓存用于非动态/不经常更改的数据

ServerTimeSpan


在服务器上存储数据。您可以轻松地使此缓存无效,但它需要一些资源(内存)

ClientTimeSpan

如果希望允许客户端(通常是浏览器)在用户计算机上本地缓存数据,请使用客户端缓存。好处是,在缓存过期之前,客户端可能不会请求您的API。另一方面,您不能使该缓存无效,因为它存储在客户端。将此缓存用于非动态/不经常更改的数据

ServerTimeSpan


在服务器上存储数据。您可以轻松地使此缓存无效,但它需要一些资源(内存)

可能会对您有所帮助。可能会帮助您。感谢Arindam在两天内帮助我解决2个问题。感谢Arindam在两天内帮助我解决2个问题。