angularjs - 使用 Restangular 取消对服务器的 GET 请求

标签 angularjs caching restangular angular-cache

我创建了一个 UserService,如下所示:

angular.module('nrApp').factory('userService', ['Restangular', 'UserModel', 'DSCacheFactory', function (Restangular, UserModel, DSCacheFactory) {
    // Create a new cache called "profileCache"
    var userCache = DSCacheFactory('userCache', {
        maxAge: 3600000,
        deleteOnExpire: 'aggressive',
        storageMode: 'localStorage', // This cache will sync itself with `localStorage`.
        onExpire: function (key, value) {
            Restangular.oneUrl('users', key).get().then(function(data) {
                userCache.put(key, data);
            });
        }
    });

    Restangular.extendModel('users', function(obj) {
        return UserModel.mixInto(obj);
    });

    Restangular.addRequestInterceptor(function(element, operation, what, url) {
        if(operation === 'get') {
            debugger;
            //Check the cache to see if the resource is already cached
            var data = userCache.get(url);
            //If cache object does exist, return it
            if(data !== undefined) {
                angular.extend(element, data);
            }

            return element;
        }
    });

    Restangular.addResponseInterceptor(function(data, operation, what, url, response) {
        //Cache the response from a get method
        if(operation === 'get') {
            debugger;
            userCache.put(url, data);
        }

        //Unvalidate the cache when a 'put', 'post' and 'delete' is performed to update the cached version.
        if (operation === 'put' || operation === 'post' || operation === 'delete') {
            userCache.destroy();
        }

        return response;
    });

    return Restangular.service('users');
}]);

从评论中可以看出,我想要实现的目标是,每当使用 Restangular 通过此服务执行 Get 请求时,都会检查本地缓存,如果缓存返回一个对象,则将其扩展到 Restangular 元素中。想要实现的流程是,当找到该请求的缓存对象时,取消对服务器的请求。

但是,如果运气不好,即使在缓存中找到了该对象,addResponseInterceptor 方法仍然会执行。

是否有任何可能的解决方案可以在“获取”请求期间取消对服务器的请求?

谢谢! :)

最佳答案

一种方法是通过 httpConfig 取消它。 Restangular 为您提供 httpConfig 对象作为 addFullRequestInterceptor 中的参数方法。您可以像下面这样使用它:

RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params, httpConfig ) {
    ...
    if found in cache {
        var defer = $q.defer();
        httpConfig.timeOut = defer.promise;
        defer.resolve();
    }
    ...
}

希望这有帮助。

关于angularjs - 使用 Restangular 取消对服务器的 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25626981/

相关文章:

angularjs - 使用 Gruntjs 配置 Restangular.baseUrl

javascript - 如何设计基于 angularJS 资源的授权路由?

angularjs - 登录后设置 RestAngular 默认请求参数

javascript - 在 Angular 单元测试中使用 Modernizr

javascript - 如何从 AngularJs Controller 中的 ION-Range-Slider 获取 "from"和 "to"值

c# - ConcurrentDictionary 作为静态缓存

javascript - 如何要求浏览器刷新(刷新)我网站的缓存?

css - 为什么我定义的 CSS 文件没有被加载?这个其他文件是从哪里来的?

angularjs - 如何让 Controller 等待服务返回值以及如何访问 Controller 中的返回值?

javascript - ng-action 不在表单中添加 action 属性