Optimize SharePoint REST API with client side caching | Angular JS

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

//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`.
});
}]);

Further configurations http://jmdobry.github.io/angular-cache/#configuration-options

Else you can define configurations per Angular service or may be per request.

(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`
});
}
}
})();

Finally, you should should bind your cache with the request.

 //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;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s