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

 

 

THANKS

About Author

Author Image
Sameer Grover

Sameer is an experienced Java developer with good working knowledge on Swing, Socket API, Collections, JDBC, Spring and Hibernate/JPA

Request for Proposal

Name is required

Comment is required

Sending message..