如何在 ASP.NET Web API 中使用缓存? [英] How to use caching in ASP.NET Web API?

查看:34
本文介绍了如何在 ASP.NET Web API 中使用缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ASP.NET MVC 4 和 WEB API

I am using ASP.NET MVC 4 with WEB API

我有以下操作,在下面显示的操作中,我的服务方法对 DoMagic() 方法进行数据库调用并返回一个整数值,然后我将在每个页面上使用该值,此操作使用 ajax 调用调用.

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.

以下是我的 WEB API 操作:

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

我的问题:我曾尝试使用 [OutputCache(Duration = 86400, VaryByParam = "none")] 并且我除外,只有第一次进行 db 调用和下一个后续请求此操作将返回缓存值,但这并没有发生.

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 ?

推荐答案

遗憾的是,ASP.NET Web API 中没有内置缓存.

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

看看这个让你走上正轨:http://www.strathweb.com/2012/05/output-caching-in-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/

此处更新资源:https://github.com/filipw/AspNetWebApi-OutputCache

截至 2020 年 2 月 3 日,即使这个答案很旧,它仍然有效.

以上两个 URL 都指向同一个项目,ASP.NET Web API CacheOutput菲利普 W

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

这篇关于如何在 ASP.NET Web API 中使用缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆