AngularJS service that extends the $cacheFactory with time out functionality.
Step 1: Install via Bower
bower install tmix-caching --save
Step 2: Add the script reference in Index.html (bower may do this for you already)
<script src="bower_components/tmix-caching/tmixCaching.js"></script>
Step 3: Add tmixCaching as a dependency in your app
angular.module('MYAPPNAME', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
'ngAnimate',
'angulartics.google.analytics',
'tmixCaching'
])
...
Step 4: Inject the service where used
// Example adding tmixCaching dependency
angular.module('myApp')
.factory('myService', function(tmixCaching) {
// Service logic
// ...
});
destroy(name)
Removes an object from angular's cache.
- name (required): string
Sample Code Snippet Using destroy()
intcCaching.destroy('myCache');
get(name)
Returns the specified object from cache. Will return a null if the object does not exist or has timed out.
- name (required): string The name of the cache object.
- Returns the specified object from cache
Sample Code Snippet Using get()
var myData = intcCaching.get('myCache');
getAsCopy(name)
Returns a copy of the specified object from cache. Will return a null if the object does not exist or has timed out.
- name (required): string The name of the cache object.
- Returns a copy of the specified object from cache
Sample Code Snippet Using getAsCopy()
var myData = intcCaching.getAsCopy('myCache');
getTimeout(name)
Returns the specified object's millisecond timeout value. Will return a null if the object does not exist.
- name (required): string The name of the cache object.
- Returns the specified object's millisecond timeout value
Sample Code Snippet Using getTimeout()
var myDataTimeout = intcCaching.getTimeout('myCache');
getTimeRemaining(name)
Returns the number of milliseconds until the object times out. Will return a null if the object does not exist.
- name (required): string The name of the cache object.
- Returns the number of millseconds until the specified object times out
Sample Code Snippet Using getTimeStamp()
var remainingTime = intcCaching.getTimeRemaining('myCache');
getTimeStamp(name)
Returns the specified object's timestamp from when it was put in cache. Will return a null if the object does not exist.
- name (required): string The name of the cache object.
- Returns the specified object's timestamp from when it was put in cache
Sample Code Snippet Using getTimeStamp()
var myDataTimeStamp = intcCaching.getTimeStamp('myCache');
isValid(name)
Returns a boolean indicating if the object is in cache and is still valid (within the timeout value).
- name (required): string The name of the cache object.
- Returns a boolean indicating if the object is valid
Sample Code Snippet Using isValid()
var myDataValid = intcCaching.isValid('myCache');
put(name, value, milliseconds)
Puts an object into angular's cache with an optional timeout value. If a millisecond timeout is included, the get method will test against the millisecond timeout value. If millisecond timeout value is not included, the get method will always return the value object.
- name (required): string The name of the cache object.
- value (required): primitive/object The item to put into cache.
- milliseconds (optional): integer The number of milliseconds until the item's cache expires.
Sample Code Snippet Using put() with a one hour timeout value
intcCaching.put('myCache', myObjectToPutInCache, 3600000);
Sample Code Snippet Using put() with no timeout value
intcCaching.put('myCache', myObjectToPutInCache);
putAsCopy(name, value, milliseconds)
Puts a copy of an object into angular's cache with an optional timeout value. If a millisecond timeout is included, the get method will test against the millisecond timeout value. If millisecond timeout value is not included, the get method will always return the copy of the value object.
- name (required): string The name of the cache object.
- value (required): primitive/object The item to put into cache.
- milliseconds (optional): integer The number of milliseconds until the item's cache expires.
Sample Code Snippet Using putAsCopy() with a one hour timeout value
intcCaching.putAsCopy('myCache', myObjectToPutInCache, 3600000);
Sample Code Snippet Using putAsCopy() with no timeout value
intcCaching.putAsCopy('myCache', myObjectToPutInCache);