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

查看:27
本文介绍了如何缓存 .NET Web API 请求(和使用 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.

在 Google Chrome 的网络"选项卡中查看的响应:

Response as viewed in the Network tab of Google Chrome:

根据进一步调查(如上图所示),Web API 控制器中禁用了缓存.甚至不支持在常规 MVC 控制器中使用的 [OutputCache] 属性.

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.

幸运的是我找到了这个博客: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:

我决定使用 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 Not Modified:

问题解决了!希望这对其他人有帮助.

Problem solved! Hope this helps someone else.

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

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