AngularJS $http cache expiration time

后端 未结 4 931
你的背包
你的背包 2021-02-13 12:50

Can you set an expiration time for Angular\'s $http default caching system ? I would like that an URL stays in the cache for 1 minute, so that any requests made to it after 1 mi

相关标签:
4条回答
  • 2021-02-13 13:16

    according to this site here are the $cacheFactory specs

    • Storage Javascript heap memory
    • Eviction algorithm Least recently used (LRU)
    • Time-to-live until page refresh
    • Support for revalidation No
    • Requires cooperation of remote server No
    0 讨论(0)
  • 2021-02-13 13:21

    An expiration time-based cache implementation for AngularJS might look like this:

    MyAngularApp.factory('cache', function($cacheFactory) {    
        const cache = $cacheFactory('expire-cache');
    
        function get(key, defaultValue = null, expire = null) {    
            let cached = cache.get(key);
            let expired = cached && cached.expire > 0 && cached.expire < performance.now() - cached.created;
           
            if (!cached || expired) {
                cached = {
                    value: typeof (defaultValue) === 'function' ? defaultValue() : defaultValue,
                    expire: expire,
                    created: performance.now()
                }
    
                if (cache.value !== null && 
                    cache.value !== undefined) {
    
                    cache.put(key, cached);
                }
            }
            
            return cache.value;
        }
    
        return {
            get: get,
            remove: function remove(key) { cache.remove(key) },
            clear: function clear() { cache.removeAll() },
        };    
    });
    

    The possible method calls would be these:

    read = cache.get('keyName1', 'my permanent value');
    read = cache.get('keyName2', 'my static value for 5 secs', 5000);
    read = cache.get('keyName3', () => 'my on time value for 5 secs', 5000);
    
    cache.remove('keyName1'); /* Remove one */
    
    cache.clear(); /* Remove all */
    
    0 讨论(0)
  • 2021-02-13 13:27

    Another Solution:

    Use $httpProvider to hook the request.

    http_ttl_cache = {}  # save the last cache time
    MyApp.config ['$httpProvider', ($httpProvider) ->
        $httpProvider.interceptors.push ['$cacheFactory', ($cacheFactory) ->
            request: (config) ->
                if config.params and config.params.__cache__
                    config.cache = true
    
                    N = config.params.__cache__
                    delete config.params.__cache__
    
                    if moment() - (http_ttl_cache[config.url] or 0) > 1000 * N     
                        cache = $cacheFactory.get('$http')                            
                        cache.remove(config.url)                                      
                        http_ttl_cache[config.url] = moment()
                return config
        ]
    ]
    

    In your controller:

    # 600 is 600 seconds
    $http.get("/api/books?limit=100", 
              {
                   params: {'__cache__': 600, 'foo': 'bar'}
              }
    )
    
    0 讨论(0)
  • 2021-02-13 13:39

    There is no way to set the expiration time. But you can do it manually. You have to access the $http cache:

    var cache = $cacheFactory.get('$http');
    

    and remove the url that is cached by:

    cache.remove(theUrl);
    

    See documentation for more information.

    0 讨论(0)
提交回复
热议问题