我必须在asp.net web方法中实现缓存,因为我是从第三方数据源访问数据,而调用第三方数据源成本很高,只有在斯特拉网.I的帮助下,每24 hours.So才会更新数据。
/// <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;
}但是在Strathweb中,我看到了两个属性,一个是ClientTimeSpan,另一个是ServerTimeSpan.I,我不知道什么时候使用ClientTimeSpan,什么时候使用ServerTimeSpan.In --这是最简单的术语--我想了解什么时候使用服务器端缓存,什么时候使用客户端缓存,两者之间有什么区别。
发布于 2014-10-08 05:15:52
顾名思义
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";
}发布于 2014-10-08 06:04:13
ClientTimeSpan
如果希望允许客户端(通常是浏览器)在用户计算机上本地缓存数据,请使用客户端缓存。好处是客户端可能在缓存过期之前不会请求您的API。另一方面,您不能使这个缓存失效,因为它存储在客户端。对非动态/不经常更改的数据使用此缓存。
ServerTimeSpan
在服务器上存储数据。您可以轻松地使这个缓存失效,但它需要一些资源(内存)。
https://stackoverflow.com/questions/26249318
复制相似问题