user3153189 user3153189 - 5 months ago 26
AngularJS Question

How to collect all the objects from $cacheFactory

I was planning to use $cacheFactory to cache my objects at client side to solve

  1. Performance

  2. Multiple view to access the same object

General use case is

Client can request a device through cache service. If it is available in $cacheFactory return it. If it is not available, fetch it from the server and add it to $cacheFactory and return it. Everything looks nice.

Another use case is

Client can request all devices. How to return all the devices cached in $cacheFactory.

I didn't see any method which gives key value pairs similar to Java Map.Entry. Can some one please point if there is any way to iterate $cacheFactory


if you look example of $cacheFactory in angularjs docs, they did what you want there...

angular.module('cacheExampleApp', []).
  controller('CacheController', ['$scope', '$cacheFactory', function($scope, $cacheFactory) {
    $scope.keys = [];
    $scope.cache = $cacheFactory('cacheId');
    $scope.put = function(key, value) {
      if ($scope.cache.get(key) === undefined) {
      $scope.cache.put(key, value === undefined ? null : value);

in this controller it pushes key into an array if it is valid and after that in html just traverse through keys array like this...

<p ng-if="keys.length">Cached Values</p>
  <div ng-repeat="key in keys">
    <span ng-bind="key"></span>
    <span>: </span>
    <b ng-bind="cache.get(key)"></b>