If you working with any REST API and having the requirement of getting too many data from the from the DB or lists (in SharePoint) [GET requests]. You may facing slowness in the App, Web Part or in client side web part.
To overcome above issue with the load, one option would be caching of recurring data while you made all other performance improvement points. Following describes how yo improve performance of SharePoint Online/On-prem application which relies on REST API by using caching and uses Angular JS for client side development.
Note: Out of the box (OOTB) Angular JS having caching implementation on the $http, but in following sample I have used "angular-cache" since it give more features such as access to local storage and session storage.
First you should include following references https://github.com/jmdobry/angular-cache/tree/master/dist and include those it in your project.
Secondly, you should configure the caching options. Either you configure caching globally as below on the Angular app.js
[code language="javascript"]
//If need to apply all HTTP requests globally
(function () {
'use strict';
// create app
var app = angular.module('app', [
, 'angular-cache'
]);
// startup code
app.run(['$http', 'CacheFactory', function ($http, CacheFactory) {
//cache configs
$http.defaults.cache = CacheFactory('nylFileData', {
maxAge: 15 * 60 * 1000 // Items added to this cache expire after 15 minutes
, cacheFlushInterval: 60 * 60 * 1000 // This cache will clear itself every hour
, deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
, storageMode: 'localStorage' // This cache will use `localStorage`.
});
}]);
[/code]
Further configurations http://jmdobry.github.io/angular-cache/#configuration-options
Else you can define configurations per Angular service or may be per request.
[code language="javascript"]
(function () {
'use strict';
var serviceId = "myService";
angular
.module('app')
.factory(serviceId, ['$http', '$q', 'CacheFactory', myService]);
function myService($http, $q, CacheFactory) {
// init factory
init();
var service = {
getData: getData
};
return service;
//initialization
function init() {
//Cache initialization
CacheFactory('cacheData', {
maxAge: 15 * 60 * 1000 // Items added to this cache expire after 15 minutes
, cacheFlushInterval: 60 * 60 * 1000 // This cache will clear itself every hour
, deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
, storageMode: 'localStorage' // This cache will use `localStorage`
});
}
}
})();
[/code]
Finally, you should should bind your cache with the request.
[code language="javascript"]
//get data function
function getData() {
var deferred = $q.defer();
var start = new Date().getTime();
//request
$http({
method: 'GET',
cache: CacheFactory.get('cacheData'),
url: webUrl + '/_api/web/lists/getByTitle(\'My List\')/Items',
headers: {
'Accept': 'application/json;odata=verbose',
'Content-Type': 'application/json;odata=verbose'
}
}).then(function successCallback(data) {
// when the response is available
deferred.resolve(data);
console.log('Time to retrieve data - [' + (new Date().getTime() - start) + 'ms]', data, serviceId, false);
}, function errorCallback(error) {
// or server returns response with an error status.
deferred.reject(error);
});
return deferred.promise;
}
[/code]
To overcome above issue with the load, one option would be caching of recurring data while you made all other performance improvement points. Following describes how yo improve performance of SharePoint Online/On-prem application which relies on REST API by using caching and uses Angular JS for client side development.
Note: Out of the box (OOTB) Angular JS having caching implementation on the $http, but in following sample I have used "angular-cache" since it give more features such as access to local storage and session storage.
First you should include following references https://github.com/jmdobry/angular-cache/tree/master/dist and include those it in your project.
Secondly, you should configure the caching options. Either you configure caching globally as below on the Angular app.js
[code language="javascript"]
//If need to apply all HTTP requests globally
(function () {
'use strict';
// create app
var app = angular.module('app', [
, 'angular-cache'
]);
// startup code
app.run(['$http', 'CacheFactory', function ($http, CacheFactory) {
//cache configs
$http.defaults.cache = CacheFactory('nylFileData', {
maxAge: 15 * 60 * 1000 // Items added to this cache expire after 15 minutes
, cacheFlushInterval: 60 * 60 * 1000 // This cache will clear itself every hour
, deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
, storageMode: 'localStorage' // This cache will use `localStorage`.
});
}]);
[/code]
Further configurations http://jmdobry.github.io/angular-cache/#configuration-options
Else you can define configurations per Angular service or may be per request.
[code language="javascript"]
(function () {
'use strict';
var serviceId = "myService";
angular
.module('app')
.factory(serviceId, ['$http', '$q', 'CacheFactory', myService]);
function myService($http, $q, CacheFactory) {
// init factory
init();
var service = {
getData: getData
};
return service;
//initialization
function init() {
//Cache initialization
CacheFactory('cacheData', {
maxAge: 15 * 60 * 1000 // Items added to this cache expire after 15 minutes
, cacheFlushInterval: 60 * 60 * 1000 // This cache will clear itself every hour
, deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
, storageMode: 'localStorage' // This cache will use `localStorage`
});
}
}
})();
[/code]
Finally, you should should bind your cache with the request.
[code language="javascript"]
//get data function
function getData() {
var deferred = $q.defer();
var start = new Date().getTime();
//request
$http({
method: 'GET',
cache: CacheFactory.get('cacheData'),
url: webUrl + '/_api/web/lists/getByTitle(\'My List\')/Items',
headers: {
'Accept': 'application/json;odata=verbose',
'Content-Type': 'application/json;odata=verbose'
}
}).then(function successCallback(data) {
// when the response is available
deferred.resolve(data);
console.log('Time to retrieve data - [' + (new Date().getTime() - start) + 'ms]', data, serviceId, false);
}, function errorCallback(error) {
// or server returns response with an error status.
deferred.reject(error);
});
return deferred.promise;
}
[/code]
Comments
Post a Comment