1 |
|
2 | var Cache, localStorage;
|
3 |
|
4 | localStorage = window.localStorage;
|
5 |
|
6 | Cache = (function() {
|
7 | Cache.prototype.memoryCache = {};
|
8 |
|
9 | function Cache(_arg) {
|
10 | this.$log = _arg.$log, this.localStoragePrefix = _arg.localStoragePrefix;
|
11 | }
|
12 |
|
13 | Cache.prototype.getItem = function(key, fallbackValue) {
|
14 | var item, out;
|
15 | key = this._buildKey(key);
|
16 | item = this.memoryCache[key];
|
17 | if (item == null) {
|
18 | item = localStorage.getItem(key);
|
19 | }
|
20 | out = item != null ? angular.fromJson(item) : fallbackValue;
|
21 | this.$log.debug("CACHE GET: " + key, out);
|
22 | return out;
|
23 | };
|
24 |
|
25 | Cache.prototype.setItem = function(key, value) {
|
26 | var stringValue;
|
27 | key = this._buildKey(key);
|
28 | stringValue = angular.toJson(value);
|
29 | try {
|
30 | localStorage.setItem(key, stringValue);
|
31 | if (this.memoryCache[key] != null) {
|
32 | delete this.memoryCache[key];
|
33 | }
|
34 | } catch (_error) {
|
35 | this.memoryCache[key] = stringValue;
|
36 | }
|
37 | this.$log.debug("CACHE PUT: " + key, angular.fromJson(angular.toJson(value)));
|
38 | return value;
|
39 | };
|
40 |
|
41 | Cache.prototype.clear = function(_arg) {
|
42 | var cacheKey, cacheKeys, exceptFor, exception, i, key, skipKey, where, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _results, _results1;
|
43 | _ref = _arg != null ? _arg : {}, key = _ref.key, exceptFor = _ref.exceptFor, where = _ref.where;
|
44 | if (where && exceptFor) {
|
45 | return this.$log.error("Using where and exceptFor arguments at once in clear() method is forbidden!");
|
46 | }
|
47 | if (exceptFor) {
|
48 | if (exceptFor == null) {
|
49 | exceptFor = [];
|
50 | }
|
51 | cacheKeys = [];
|
52 | for (i = _i = 0, _ref1 = localStorage.length; 0 <= _ref1 ? _i < _ref1 : _i > _ref1; i = 0 <= _ref1 ? ++_i : --_i) {
|
53 | cacheKey = localStorage.key(i);
|
54 | if (!this._cacheKeyHasPrefix(cacheKey, key)) {
|
55 | continue;
|
56 | }
|
57 | skipKey = false;
|
58 | for (_j = 0, _len = exceptFor.length; _j < _len; _j++) {
|
59 | exception = exceptFor[_j];
|
60 | if (!(this._cacheKeyHasPrefix(cacheKey, exception))) {
|
61 | continue;
|
62 | }
|
63 | skipKey = true;
|
64 | break;
|
65 | }
|
66 | if (skipKey) {
|
67 | continue;
|
68 | }
|
69 | cacheKeys.push(cacheKey);
|
70 | }
|
71 | _results = [];
|
72 | for (_k = 0, _len1 = cacheKeys.length; _k < _len1; _k++) {
|
73 | cacheKey = cacheKeys[_k];
|
74 | _results.push(localStorage.removeItem(cacheKey));
|
75 | }
|
76 | return _results;
|
77 | } else {
|
78 | _results1 = [];
|
79 | for (_l = 0, _len2 = where.length; _l < _len2; _l++) {
|
80 | cacheKey = where[_l];
|
81 | _results1.push(localStorage.removeItem(this._buildKey(cacheKey)));
|
82 | }
|
83 | return _results1;
|
84 | }
|
85 | };
|
86 |
|
87 | Cache.prototype._buildKey = function(key) {
|
88 | return "" + this.localStoragePrefix + key;
|
89 | };
|
90 |
|
91 | Cache.prototype._cacheKeyHasPrefix = function(cacheKey, prefix) {
|
92 | var index, nextChar;
|
93 | if (prefix == null) {
|
94 | return cacheKey.indexOf(this.localStoragePrefix) === 0;
|
95 | }
|
96 | prefix = this._buildKey(prefix);
|
97 | index = cacheKey.indexOf(prefix);
|
98 | nextChar = cacheKey[prefix.length];
|
99 | return index === 0 && ((nextChar == null) || (nextChar === '?' || nextChar === '/'));
|
100 | };
|
101 |
|
102 | return Cache;
|
103 |
|
104 | })();
|
105 |
|
106 | module.exports = function(providerParams) {
|
107 | return new Cache(providerParams);
|
108 | };
|