Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

c# - How to use caching in ASP.NET Web API?

I am using ASP.NET MVC 4 with WEB API

I have the following action, in the action shown below, my service method makes a db call to DoMagic() method and returns an integer value which I am then using on every page, this action is called using an ajax call.

Below is my WEB API action :

[OutputCache(Duration = 86400, VaryByParam = "none")]
[ActionName("GetMyMagicNumber")]
public int GetMyMagicNumber()
{
    if (WebSecurity.IsAuthenticated)
    {
        var revenue = _magicService.DoMagic();
        return revenue;
    }
    return 0;
}

My question : I haved tried using [OutputCache(Duration = 86400, VaryByParam = "none")] and I excepted that only the first time the db call will be made and next subsequent request to this action will return me the cached value, but this is not happening.

A db call is again made, the db call takes time how do I get this working ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately, caching is not built into ASP.NET Web API.

Check this out to get you on track: http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/

An updated resource here: https://github.com/filipw/AspNetWebApi-OutputCache

EDIT: As of 2020-02-03, even though this answer is quite old, it's still valid.

Both of the URL's above lead to the same project, ASP.NET Web API CacheOutput by Filip W


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...