-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal-storage-cache-factory.min.js
5 lines (5 loc) · 1.79 KB
/
local-storage-cache-factory.min.js
1
2
3
4
5
/**!
* AngularJS localStorageCacheFactory that mimics the functionality of $cacheFactory
* @author Shane O'Sullivan <[email protected]>
* @version 0.1.2
*/(function(e,t,n){"use strict";var r=angular.module("localStorageCacheFactory",[]);r.provider("$localStorageCacheFactory",["$cacheFactoryProvider","$rootElementProvider",function(r,i){function l(e,t){this.cacheId=e,this.secondaryCache=s(e,t),this.options=t,this.prefix=u+a+"::"+e+"::"}function c(e,t){if(typeof e!="string"||e==="")throw new Error("[$localStorageCacheFactory] Invalid cacheId given!");return f[e]||(f[e]=new l(e,t)),f[e]}var s=r.$get(),o=i.$get(),u="$$LSCF$$",a=o.attr("ng-app")||"",f={};l.prototype={get:function(e){var n=t.getItem(this.prefix+e);return n||(n=this.secondaryCache.get(e)),n},put:function(e,r){if(r===n)return;typeof r=="string"?(t.setItem(this.prefix+e,r),this.secondaryCache.remove(e)):(this.secondaryCache.put(e,r),t.removeItem(this.prefix+e))},remove:function(e){t.removeItem(this.prefix+e),this.secondaryCache.remove(e)},removeAll:function(){for(var e=0;e<t.length;e++){var n=t.key(e);n.substring(0,this.prefix.length)===this.prefix&&(t.removeItem(n),e--)}this.secondaryCache.removeAll()},destroy:function(){this.removeAll(),this.secondaryCache.destroy(),delete f[this.cacheId],this.cacheId=null,this.secondaryCache=null,this.options=null,this.prefix=null},info:function(){var e=this.secondaryCache.info();for(var n=0,r=t.length;n<r;n++){var i=t.key(n);i.substring(0,this.prefix.length)===this.prefix&&e.size++}return e}},c.prefix=u,this.$get=[function(){return t?c:s}],this.clear=function(e){var n=u+a+"::"+(e?e:"");for(var r=0;r<t.length;r++){var i=t.key(r);i.substring(0,n.length)===n&&(t.removeItem(i),r--)}e&&f[e]&&f[e].removeAll();if(!e)for(var e in f)f.hasOwnProperty(cache)&&f[e].removeAll()}}])})(window,window.localStorage);