1

在 next(Route) 到其他页面后,回来它仍然回调链接。如何缓存来自 http 调用的 JSON 数据以优化性能?

尝试一些解决方案但不起作用

$http.get(url, { cache: true}).success(...);

有什么更好的解决方案吗?

4

3 回答 3

2

更好的方法是CacheFactory: -

 var cache = $cacheFactory('myCache');

 var data = cache.get(anyKey);

 if (!data) {
   $http.get(url).success(function(result) {
      data = result;
      cache.put(anyKey, data);
   });
 } 
于 2014-08-10T03:45:48.940 回答
1

您还可以使用 angular-data 指令进行缓存。它允许您指定缓存位置:本地存储/会话/内存,并且您可以设置要将请求保留在缓存中的时间。

http://angular-data.pseudobry.com/documentation/guide/angular-cache/index

要初始化缓存,请在 app.run() 函数中添加以下代码:

     DSCacheFactory('defaultCache', {
        maxAge: 900000, // Items added to this cache expire after 15 minutes.
        cacheFlushInterval: 6000000, // This cache will clear itself every hour.
        deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
        storageMode:'memory' // [default: memory] sessionStorage, localStorage
    });

    $http.defaults.cache = DSCacheFactory.get('defaultCache');

然后像你一样在你的代码中使用它:

$http.get(url, { cache: true}).success(...);
于 2014-09-12T08:48:06.077 回答
0

我推荐你下载angular-cache!它是 Angular 的 $cacheFactory 的一个非常有用的替代品

在 .run() 块中,定义缓存:

.run(function (DSCacheFactory) {
    DSCacheFactory("dataCache", { 
        storageMode: "localStorage",
        maxAge: 720000, // time in milliseconds
        deleteOnExpire: "aggressive"
    });
}

然后在您的服务中,您可以管理如何使用您的数据、在缓存过期时从缓存中获取数据、进行新调用和刷新数据。

(function (){
'use strict';

app.factory('DataService', ['$http','$q','DSCacheFactory',DataService]);

function DataService($http, $q,DSCacheFactory){
    self.dataCache= DSCacheFactory.get("dataCache");

self.dataCache.setOptions({
        onExpire: function(key,value){
            getData()
                .then(function(){
                    console.log("Data Cache was automatically refreshed", new Date());
                }, function(){
                    console.log("Error getting data. Putting expired info again", new Date());
                    // This line of code will be used if we want to refresh data from cache when it expires
                    self.dealerListCache.put(key,value);
                });
        }
    });

function getData(){
        var deferred = $q.defer(),
            cacheKey = "myData",
            dataFromHttpCall = self.dataCache.get(cacheKey);

        if(dataFromHttpCall){
            console.log("Found in cache");
            deferred.resolve(dealersList);
        } else {
            $http.get('/api/dataSource')
                .success(function (data) {
                    console.log("Received data via HTTP");
                    self.dataCache.put(cacheKey, data);
                    deferred.resolve(data);
                })
                .error(function () {
                    console.log('Error while calling the service');
                    deferred.reject();
                });
        }

        return deferred.promise;
    }
};

})();

就这样!

于 2014-11-25T18:22:48.910 回答