简体   繁体   English

角度服务Restangular缓存

[英]Angular Service Restangular Caching

Are there scenarios where an angular service will cache Restangular/$http calls without being explicitly told to do so? 是否存在角度服务将缓存Restangular / $ http调用而未明确告知这样做的情况? For example I have a service doing something like this: 例如,我有一个服务做这样的事情:

    function getSomeThings(){
        return Restangular.one('things').get().then(function (thing) {
            return thing;
        });
    }

This service gets called every time a page refreshes (it's in the UI-router route resolve). 每次页面刷新时都会调用此服务(它在UI路由器路由解析中)。 Is there any chance that this call WON'T be made every time, but will be cached by Angular somehow, without explicitly being told to do so? 是否有可能每次都不会进行此调用,但Angular会以某种方式缓存这些调用,而没有明确告知这样做?

I am familiar with caching explicitly like so: 我熟悉如此明确的缓存

RestangularProvider.setDefaultHttpFields({cache: true});

This is NOT the intent. 这不是意图。 My question is whether angular services have some innate caching logic, and if so, how to override it. 我的问题是角度服务是否具有一些固有的缓存逻辑,如果是,则如何覆盖它。

By default Restangular doesn't implement any caching strategies or scenarios, you will need to build your owns. 默认情况下, Restangular不会实现任何缓存策略或方案,您需要构建自己的所有者。 As far as i know, those are what you can do with cache when working with Restangular : 据我所知,使用Restangular时可以使用cache执行以下操作

  1. You can cache everything as you said but you might find yourself working with stale data, so be careful with that : 你可以像你说的那样缓存所有内容,但你可能会发现自己正在使用陈旧的数据,所以要小心:

     RestangularProvider.setDefaultHttpFields({cache: true}); 
  2. You can cache response for single requests like : 您可以缓存单个请求的响应,例如:

     function getSomeThings(){ Restangular.one('thing', 123).withHttpConfig({ cache: true}).get().then(function (thing) { return thing; }); } 
  3. You can involve a custom $cacheFactory instance to expire or invalidate cached responses when necessary by invoking this : $cacheFactory.get('$http').removeAll() 您可以通过调用以下内容来使用自定义$ cacheFactory实例来使缓存响应失效或无效: $cacheFactory.get('$http').removeAll()

  4. You can roll in your own cache interface instead of setting true to the cache. 您可以在自己的缓存界面中滚动,而不是将true设置为缓存。 This is a factory example that I'm using to remove all cached data whenever I'm sending a create , update or delete request : 这是我用来在发送创建更新删除请求时删除所有缓存数据的工厂示例:

      .factory('HTTPCache', ['Restangular', '$cacheFactory', function(Restangular, $cacheFactory) { var service = {}; var cache; // Creates the cache service.init = function() { cache = $cacheFactory('http'); Restangular.setDefaultHttpFields({cache: cache}); Restangular.setResponseInterceptor(function(response, operation) { if (operation === 'put' || operation === 'post' || operation === 'remove') { cache.removeAll(); } return response; }) } return service; }]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM