如何缓存的.NET Web API请求(安培;使用W / AngularJS $ HTTP) [英] How to cache .NET Web API requests (& use w/ AngularJS $http)

查看:245
本文介绍了如何缓存的.NET Web API请求(安培;使用W / AngularJS $ HTTP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ASP.NET编写一个Web的API,我通过AngularJS $ HTTP 消耗。

I have a Web API written in ASP.NET that I'm consuming via AngularJS $http.

我在AngularJS厂启用高速缓存如下,但每一个请求仍返回 200 ,从未 200(从缓存) 304 (和每个请求我的意思是做相同的Web API请求在同一页上无数次,重温我已经访问过的页面,其中包含一个Web API请求,刷新上述页等)。

I have enabled caching in my AngularJS factory as follows but every request still returns a response of 200, never 200 (from cache) or 304 (and by every request I mean making the same web api request numerous times on the same page, revisiting a page I've already visited that contains a Web API request, refreshing said page etc).

angular.module('mapModule')
    .factory('GoogleMapService', ['$http', function ($http) {
         var googleMapService = {               
            getTags: function () {
                // $http returns a promise, which has a 'then' function, which also returns a promise
                return $http({ cache: true, dataType: 'json', url: '/api/map/GetTags', method: 'GET', data: '' })
                .then(function (response) {                     
                    return response.data;
                });
            };

        return googleMapService;
    }]);

我缺少从事物的AngularJS侧的东西吗?或者这是一个Web API的问题。或者两者都是?

Am I missing something from the AngularJS side of things? Or is this a Web API problem. Or both?

推荐答案

原来这是一个Web API的事情。我忽略了一个事实,即响应头部明确表示,缓存被禁用。

Turns out it was a Web API thing. I'd overlooked the fact that the response header clearly stated that caching was disabled.

响应,谷歌浏览器的网络选项卡浏览:

Response as viewed in the Network tab of Google Chrome:

在这里输入的形象描述

当<一href=\"http://stackoverflow.com/questions/14811772/how-to-use-caching-in-asp-net-mvc-web-api?rq=1\">further调查(和上面的图片中看到的),缓存是Web API控制器禁用。即使是 [的OutputCache] 属性,它是在常规的MVC控制器配合使用,不支持。

Upon further investigation (and as seen in the image above), caching is disabled in Web API controllers. Even the [OutputCache] attribute, which is used in regular MVC controllers, isn't supported.

幸运的是,我发现这个博客:
<一href=\"http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/\">http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/

Luckily I found this blog: http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/

这导致我这两种解决方案:

which lead me to these two solutions:

  • ASP.NET Web API CacheOutput
  • CacheCow

我决定去与CacheOutput,因为它让我用像属性:

I decided to go with CacheOutput as it lets me use attributes like:

[CacheOutputUntilToday] 支持服务器和放大器;客户端缓存。

[CacheOutputUntilToday] which supports server & client side caching.

或者,如果我想只使用客户端的缓存我可以使用类似:

Or if I wanted to just use client-side caching I can use something like:

[CacheOutput(ClientTimeSpan = 100,ServerTimeSpan = 0)]

乍一看这CacheCow的做法似乎更容易一些。而之后更容易重构出如果需要的话。

Which seemed a little easier at first glance that CacheCow's approach. And easier to refactor out later if need be.

现在额外的请求给我一个 200(从缓存)

Now additional requests give me a 200 (from cache):

在这里输入的形象描述

通过刷新给我一个 304未修改

问题解决了!希望这可以帮助别人。

Problem solved! Hope this helps someone else.

这篇关于如何缓存的.NET Web API请求(安培;使用W / AngularJS $ HTTP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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