Cache Data with Angular Cache Plugin
Posted By : Sameer Grover | 30-Sep-2015
Following step will help you to configure angular cache in your app:-
Step 1.
Import angular cache script in your jsp so that angular-cache can enable with your app:-
<script type='text/javascript' src ="/livesource/quality/scripts/common/angular/angular-cache.min.js"></script>
Step 2:
Now add the angular-cache in your application module : -
angular.module('yourmodule', ['ngRoute']);
Step 3:
Now you need a cachefactory instance to use the cache in your app.
To create a factory & set the properties for your custom configuration.
angular.module('yourmodule').factory('$myCache', ['CacheFactory', function(CacheFactory){
return CacheFactory('myData', {
maxAge: 30 * 1000, // for 30 seconds
deleteOnExpire: 'aggressive',
onExpire: null,
cacheFlushInterval: null,
storageMode: 'none',
storageImpl: null,
recycleFreq: 1000,
disabled: false,
verifyIntegrity: false
});
}]);
// this will return the cache factory instance with setted properties.so that you can add your data to cache with specified properties.
Step 4: Now you configuration done.
Now you need to add the data to your cache.
Simply
$scope.cache.put(‘key’, $scope.myData);
// This will put your data to the angular cache.
And
put the following code to your init function.
var cachedData = $scope.cache.get(‘key’);
if(cachedData){
$scope.myData = cachedData;
}
else{
$scope.myData = myService.getData();
$scope.cache.put(‘key’, $scope.myData);
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sameer Grover
Sameer is an experienced Java developer with good working knowledge on Swing, Socket API, Collections, JDBC, Spring and Hibernate/JPA