AngularJS $http cache expiration time

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.


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

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'}
          }
)

Tags:

Angularjs