1/** 2 * angular-cache 3 * @version 4.6.0 - Homepage <https://github.com/jmdobry/angular-cache> 4 * @copyright (c) 2013-2016 angular-cache project authors 5 * @license MIT <https://github.com/jmdobry/angular-cache/blob/master/LICENSE> 6 * @overview angular-cache is a very useful replacement for Angular's $cacheFactory. 7 */ 8(function (global, factory) { 9 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(typeof angular === 'undefined' ? require('angular') : angular) : 10 typeof define === 'function' && define.amd ? define('angular-cache', ['angular'], factory) : 11 (global.angularCacheModuleName = factory(global.angular)); 12}(this, function (angular) { 'use strict'; 13 14 angular = 'default' in angular ? angular['default'] : angular; 15 16 var babelHelpers = {}; 17 babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { 18 return typeof obj; 19 } : function (obj) { 20 return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; 21 }; 22 23 /** 24 * @method bubbleUp 25 * @param {array} heap The heap. 26 * @param {function} weightFunc The weight function. 27 * @param {number} n The index of the element to bubble up. 28 */ 29 var bubbleUp = function bubbleUp(heap, weightFunc, n) { 30 var element = heap[n]; 31 var weight = weightFunc(element); 32 // When at 0, an element can not go up any further. 33 while (n > 0) { 34 // Compute the parent element's index, and fetch it. 35 var parentN = Math.floor((n + 1) / 2) - 1; 36 var parent = heap[parentN]; 37 // If the parent has a lesser weight, things are in order and we 38 // are done. 39 if (weight >= weightFunc(parent)) { 40 break; 41 } else { 42 heap[parentN] = element; 43 heap[n] = parent; 44 n = parentN; 45 } 46 } 47 }; 48 49 /** 50 * @method bubbleDown 51 * @param {array} heap The heap. 52 * @param {function} weightFunc The weight function. 53 * @param {number} n The index of the element to sink down. 54 */ 55 var bubbleDown = function bubbleDown(heap, weightFunc, n) { 56 var length = heap.length; 57 var node = heap[n]; 58 var nodeWeight = weightFunc(node); 59 60 while (true) { 61 var child2N = (n + 1) * 2; 62 var child1N = child2N - 1; 63 var swap = null; 64 if (child1N < length) { 65 var child1 = heap[child1N]; 66 var child1Weight = weightFunc(child1); 67 // If the score is less than our node's, we need to swap. 68 if (child1Weight < nodeWeight) { 69 swap = child1N; 70 } 71 } 72 // Do the same checks for the other child. 73 if (child2N < length) { 74 var child2 = heap[child2N]; 75 var child2Weight = weightFunc(child2); 76 if (child2Weight < (swap === null ? nodeWeight : weightFunc(heap[child1N]))) { 77 swap = child2N; 78 } 79 } 80 81 if (swap === null) { 82 break; 83 } else { 84 heap[n] = heap[swap]; 85 heap[swap] = node; 86 n = swap; 87 } 88 } 89 }; 90 91 function BinaryHeap(weightFunc, compareFunc) { 92 if (!weightFunc) { 93 weightFunc = function weightFunc(x) { 94 return x; 95 }; 96 } 97 if (!compareFunc) { 98 compareFunc = function compareFunc(x, y) { 99 return x === y; 100 }; 101 } 102 if (typeof weightFunc !== 'function') { 103 throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!'); 104 } 105 if (typeof compareFunc !== 'function') { 106 throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!'); 107 } 108 this.weightFunc = weightFunc; 109 this.compareFunc = compareFunc; 110 this.heap = []; 111 } 112 113 var BHProto = BinaryHeap.prototype; 114 115 BHProto.push = function (node) { 116 this.heap.push(node); 117 bubbleUp(this.heap, this.weightFunc, this.heap.length - 1); 118 }; 119 120 BHProto.peek = function () { 121 return this.heap[0]; 122 }; 123 124 BHProto.pop = function () { 125 var front = this.heap[0]; 126 var end = this.heap.pop(); 127 if (this.heap.length > 0) { 128 this.heap[0] = end; 129 bubbleDown(this.heap, this.weightFunc, 0); 130 } 131 return front; 132 }; 133 134 BHProto.remove = function (node) { 135 var length = this.heap.length; 136 for (var i = 0; i < length; i++) { 137 if (this.compareFunc(this.heap[i], node)) { 138 var removed = this.heap[i]; 139 var end = this.heap.pop(); 140 if (i !== length - 1) { 141 this.heap[i] = end; 142 bubbleUp(this.heap, this.weightFunc, i); 143 bubbleDown(this.heap, this.weightFunc, i); 144 } 145 return removed; 146 } 147 } 148 return null; 149 }; 150 151 BHProto.removeAll = function () { 152 this.heap = []; 153 }; 154 155 BHProto.size = function () { 156 return this.heap.length; 157 }; 158 159 var _Promise = null; 160 try { 161 _Promise = window.Promise; 162 } catch (e) {} 163 164 var utils = { 165 isNumber: function isNumber(value) { 166 return typeof value === 'number'; 167 }, 168 isString: function isString(value) { 169 return typeof value === 'string'; 170 }, 171 isObject: function isObject(value) { 172 return value !== null && (typeof value === 'undefined' ? 'undefined' : babelHelpers.typeof(value)) === 'object'; 173 }, 174 isFunction: function isFunction(value) { 175 return typeof value === 'function'; 176 }, 177 fromJson: function fromJson(value) { 178 return JSON.parse(value); 179 }, 180 equals: function equals(a, b) { 181 return a === b; 182 }, 183 184 Promise: _Promise 185 }; 186 187 function _keys(collection) { 188 var keys = []; 189 var key = void 0; 190 if (!utils.isObject(collection)) { 191 return keys; 192 } 193 for (key in collection) { 194 if (collection.hasOwnProperty(key)) { 195 keys.push(key); 196 } 197 } 198 return keys; 199 } 200 201 function _isPromiseLike(value) { 202 return value && typeof value.then === 'function'; 203 } 204 205 function _stringifyNumber(number) { 206 if (utils.isNumber(number)) { 207 return number.toString(); 208 } 209 return number; 210 } 211 212 function _keySet(collection) { 213 var keySet = {}; 214 var key = void 0; 215 if (!utils.isObject(collection)) { 216 return keySet; 217 } 218 for (key in collection) { 219 if (collection.hasOwnProperty(key)) { 220 keySet[key] = key; 221 } 222 } 223 return keySet; 224 } 225 226 var defaults = { 227 capacity: Number.MAX_VALUE, 228 maxAge: Number.MAX_VALUE, 229 deleteOnExpire: 'none', 230 onExpire: null, 231 cacheFlushInterval: null, 232 recycleFreq: 1000, 233 storageMode: 'memory', 234 storageImpl: null, 235 disabled: false, 236 storagePrefix: 'cachefactory.caches.', 237 storeOnResolve: false, 238 storeOnReject: false 239 }; 240 241 var caches = {}; 242 243 function createCache(cacheId, options) { 244 if (cacheId in caches) { 245 throw new Error(cacheId + ' already exists!'); 246 } else if (!utils.isString(cacheId)) { 247 throw new Error('cacheId must be a string!'); 248 } 249 250 var $$data = {}; 251 var $$promises = {}; 252 var $$storage = null; 253 var $$expiresHeap = new BinaryHeap(function (x) { 254 return x.expires; 255 }, utils.equals); 256 var $$lruHeap = new BinaryHeap(function (x) { 257 return x.accessed; 258 }, utils.equals); 259 260 var cache = caches[cacheId] = { 261 262 $$id: cacheId, 263 264 destroy: function destroy() { 265 clearInterval(this.$$cacheFlushIntervalId); 266 clearInterval(this.$$recycleFreqId); 267 this.removeAll(); 268 if ($$storage) { 269 $$storage().removeItem(this.$$prefix + '.keys'); 270 $$storage().removeItem(this.$$prefix); 271 } 272 $$storage = null; 273 $$data = null; 274 $$lruHeap = null; 275 $$expiresHeap = null; 276 this.$$prefix = null; 277 delete caches[this.$$id]; 278 }, 279 disable: function disable() { 280 this.$$disabled = true; 281 }, 282 enable: function enable() { 283 delete this.$$disabled; 284 }, 285 get: function get(key, options) { 286 var _this2 = this; 287 288 if (Array.isArray(key)) { 289 var _ret = function () { 290 var keys = key; 291 var values = []; 292 293 keys.forEach(function (key) { 294 var value = _this2.get(key, options); 295 if (value !== null && value !== undefined) { 296 values.push(value); 297 } 298 }); 299 300 return { 301 v: values 302 }; 303 }(); 304 305 if ((typeof _ret === 'undefined' ? 'undefined' : babelHelpers.typeof(_ret)) === "object") return _ret.v; 306 } else { 307 key = _stringifyNumber(key); 308 309 if (this.$$disabled) { 310 return; 311 } 312 } 313 314 options = options || {}; 315 if (!utils.isString(key)) { 316 throw new Error('key must be a string!'); 317 } else if (options && !utils.isObject(options)) { 318 throw new Error('options must be an object!'); 319 } else if (options.onExpire && !utils.isFunction(options.onExpire)) { 320 throw new Error('options.onExpire must be a function!'); 321 } 322 323 var item = void 0; 324 325 if ($$storage) { 326 if ($$promises[key]) { 327 return $$promises[key]; 328 } 329 330 var itemJson = $$storage().getItem(this.$$prefix + '.data.' + key); 331 332 if (itemJson) { 333 item = utils.fromJson(itemJson); 334 } else { 335 return; 336 } 337 } else if (utils.isObject($$data)) { 338 if (!(key in $$data)) { 339 return; 340 } 341 342 item = $$data[key]; 343 } 344 345 var value = item.value; 346 var now = new Date().getTime(); 347 348 if ($$storage) { 349 $$lruHeap.remove({ 350 key: key, 351 accessed: item.accessed 352 }); 353 item.accessed = now; 354 $$lruHeap.push({ 355 key: key, 356 accessed: now 357 }); 358 } else { 359 $$lruHeap.remove(item); 360 item.accessed = now; 361 $$lruHeap.push(item); 362 } 363 364 if (this.$$deleteOnExpire === 'passive' && 'expires' in item && item.expires < now) { 365 this.remove(key); 366 367 if (this.$$onExpire) { 368 this.$$onExpire(key, item.value, options.onExpire); 369 } else if (options.onExpire) { 370 options.onExpire.call(this, key, item.value); 371 } 372 value = undefined; 373 } else if ($$storage) { 374 $$storage().setItem(this.$$prefix + '.data.' + key, JSON.stringify(item)); 375 } 376 377 return value; 378 }, 379 info: function info(key) { 380 if (key) { 381 var item = void 0; 382 if ($$storage) { 383 var itemJson = $$storage().getItem(this.$$prefix + '.data.' + key); 384 385 if (itemJson) { 386 item = utils.fromJson(itemJson); 387 return { 388 created: item.created,
389 accessed: item.accessed, 390 expires: item.expires, 391 isExpired: new Date().getTime() - item.created > (item.maxAge || this.$$maxAge) 392 }; 393 } else { 394 return undefined; 395 } 396 } else if (utils.isObject($$data) && key in $$data) { 397 item = $$data[key]; 398 399 return { 400 created: item.created, 401 accessed: item.accessed, 402 expires: item.expires, 403 isExpired: new Date().getTime() - item.created > (item.maxAge || this.$$maxAge) 404 }; 405 } else { 406 return undefined; 407 } 408 } else { 409 return { 410 id: this.$$id, 411 capacity: this.$$capacity, 412 maxAge: this.$$maxAge, 413 deleteOnExpire: this.$$deleteOnExpire, 414 onExpire: this.$$onExpire, 415 cacheFlushInterval: this.$$cacheFlushInterval, 416 recycleFreq: this.$$recycleFreq, 417 storageMode: this.$$storageMode, 418 storageImpl: $$storage ? $$storage() : undefined, 419 disabled: !!this.$$disabled, 420 size: $$lruHeap && $$lruHeap.size() || 0 421 }; 422 } 423 }, 424 keys: function keys() { 425 if ($$storage) { 426 var keysJson = $$storage().getItem(this.$$prefix + '.keys'); 427 428 if (keysJson) { 429 return utils.fromJson(keysJson); 430 } else { 431 return []; 432 } 433 } else { 434 return _keys($$data); 435 } 436 }, 437 keySet: function keySet() { 438 if ($$storage) { 439 var keysJson = $$storage().getItem(this.$$prefix + '.keys'); 440 var kSet = {}; 441 442 if (keysJson) { 443 var keys = utils.fromJson(keysJson); 444 445 for (var i = 0; i < keys.length; i++) { 446 kSet[keys[i]] = keys[i]; 447 } 448 } 449 return kSet; 450 } else { 451 return _keySet($$data); 452 } 453 }, 454 put: function put(key, value, options) { 455 var _this3 = this; 456 457 options || (options = {}); 458 459 var storeOnResolve = 'storeOnResolve' in options ? !!options.storeOnResolve : this.$$storeOnResolve; 460 var storeOnReject = 'storeOnReject' in options ? !!options.storeOnReject : this.$$storeOnReject; 461 462 var getHandler = function getHandler(store, isError) { 463 return function (v) { 464 if (store) { 465 delete $$promises[key]; 466 if (utils.isObject(v) && 'status' in v && 'data' in v) { 467 v = [v.status, v.data, v.headers(), v.statusText]; 468 _this3.put(key, v); 469 } else { 470 _this3.put(key, v); 471 } 472 } 473 if (isError) { 474 if (utils.Promise) { 475 return utils.Promise.reject(v); 476 } else { 477 throw v; 478 } 479 } else { 480 return v; 481 } 482 }; 483 }; 484 485 if (this.$$disabled || !utils.isObject($$data) || value === null || value === undefined) { 486 return; 487 } 488 key = _stringifyNumber(key); 489 490 if (!utils.isString(key)) { 491 throw new Error('key must be a string!'); 492 } 493 494 var now = new Date().getTime(); 495 var item = { 496 key: key, 497 value: _isPromiseLike(value) ? value.then(getHandler(storeOnResolve, false), getHandler(storeOnReject, true)) : value, 498 created: options.created === undefined ? now : options.created, 499 accessed: options.accessed === undefined ? now : options.accessed 500 }; 501 if (options.maxAge) { 502 item.maxAge = options.maxAge; 503 } 504 505 if (options.expires === undefined) { 506 item.expires = item.created + (item.maxAge || this.$$maxAge); 507 } else { 508 item.expires = options.expires; 509 } 510 511 if ($$storage) { 512 if (_isPromiseLike(item.value)) { 513 $$promises[key] = item.value; 514 return $$promises[key]; 515 } 516 var keysJson = $$storage().getItem(this.$$prefix + '.keys'); 517 var keys = keysJson ? utils.fromJson(keysJson) : []; 518 var itemJson = $$storage().getItem(this.$$prefix + '.data.' + key); 519 520 // Remove existing 521 if (itemJson) { 522 this.remove(key); 523 } 524 // Add to expires heap 525 $$expiresHeap.push({ 526 key: key, 527 expires: item.expires 528 }); 529 // Add to lru heap 530 $$lruHeap.push({ 531 key: key, 532 accessed: item.accessed 533 }); 534 // Set item 535 $$storage().setItem(this.$$prefix + '.data.' + key, JSON.stringify(item)); 536 var exists = false;
537 for (var i = 0; i < keys.length; i++) { 538 if (keys[i] === key) { 539 exists = true; 540 break; 541 } 542 } 543 if (!exists) { 544 keys.push(key); 545 } 546 $$storage().setItem(this.$$prefix + '.keys', JSON.stringify(keys)); 547 } else { 548 // Remove existing 549 if ($$data[key]) { 550 this.remove(key); 551 } 552 // Add to expires heap 553 $$expiresHeap.push(item); 554 // Add to lru heap 555 $$lruHeap.push(item); 556 // Set item 557 $$data[key] = item; 558 delete $$promises[key]; 559 } 560 561 // Handle exceeded capacity 562 if ($$lruHeap.size() > this.$$capacity) { 563 this.remove($$lruHeap.peek().key); 564 } 565 566 return value; 567 }, 568 remove: function remove(key) { 569 key += ''; 570 delete $$promises[key]; 571 if ($$storage) { 572 var itemJson = $$storage().getItem(this.$$prefix + '.data.' + key); 573 574 if (itemJson) { 575 var item = utils.fromJson(itemJson); 576 $$lruHeap.remove({ 577 key: key, 578 accessed: item.accessed 579 }); 580 $$expiresHeap.remove({ 581 key: key, 582 expires: item.expires 583 }); 584 $$storage().removeItem(this.$$prefix + '.data.' + key); 585 var keysJson = $$storage().getItem(this.$$prefix + '.keys'); 586 var keys = keysJson ? utils.fromJson(keysJson) : []; 587 var index = keys.indexOf(key); 588 589 if (index >= 0) { 590 keys.splice(index, 1); 591 } 592 $$storage().setItem(this.$$prefix + '.keys', JSON.stringify(keys)); 593 return item.value; 594 } 595 } else if (utils.isObject($$data)) { 596 var value = $$data[key] ? $$data[key].value : undefined; 597 $$lruHeap.remove($$data[key]); 598 $$expiresHeap.remove($$data[key]); 599 $$data[key] = null; 600 delete $$data[key]; 601 return value; 602 } 603 }, 604 removeAll: function removeAll() { 605 if ($$storage) { 606 $$lruHeap.removeAll(); 607 $$expiresHeap.removeAll(); 608 var keysJson = $$storage().getItem(this.$$prefix + '.keys'); 609 610 if (keysJson) { 611 var keys = utils.fromJson(keysJson); 612 613 for (var i = 0; i < keys.length; i++) { 614 this.remove(keys[i]); 615 } 616 } 617 $$storage().setItem(this.$$prefix + '.keys', JSON.stringify([])); 618 } else if (utils.isObject($$data)) { 619 $$lruHeap.removeAll(); 620 $$expiresHeap.removeAll(); 621 for (var key in $$data) { 622 $$data[key] = null; 623 } 624 $$data = {}; 625 } else { 626 $$lruHeap.removeAll(); 627 $$expiresHeap.removeAll(); 628 $$data = {}; 629 } 630 $$promises = {}; 631 }, 632 removeExpired: function removeExpired() { 633 var now = new Date().getTime(); 634 var expired = {}; 635 var key = void 0; 636 var expiredItem = void 0; 637 638 while ((expiredItem = $$expiresHeap.peek()) && expiredItem.expires <= now) { 639 expired[expiredItem.key] = expiredItem.value ? expiredItem.value : null; 640 $$expiresHeap.pop(); 641 } 642 643 if ($$storage) { 644 for (key in expired) { 645 var itemJson = $$storage().getItem(this.$$prefix + '.data.' + key); 646 if (itemJson) { 647 expired[key] = utils.fromJson(itemJson).value; 648 this.remove(key); 649 } 650 } 651 } else { 652 for (key in expired) { 653 this.remove(key); 654 } 655 } 656 657 if (this.$$onExpire) { 658 for (key in expired) { 659 this.$$onExpire(key, expired[key]); 660 } 661 } 662 663 return expired; 664 }, 665 setCacheFlushInterval: function setCacheFlushInterval(cacheFlushInterval) { 666 var _this = this; 667 if (cacheFlushInterval === null) { 668 delete _this.$$cacheFlushInterval; 669 } else if (!utils.isNumber(cacheFlushInterval)) { 670 throw new Error('cacheFlushInterval must be a number!'); 671 } else if (cacheFlushInterval < 0) { 672 throw new Error('cacheFlushInterval must be greater than zero!'); 673 } else if (cacheFlushInterval !== _this.$$cacheFlushInterval) { 674 _this.$$cacheFlushInterval = cacheFlushInterval; 675 676 clearInterval(_this.$$cacheFlushIntervalId); // eslint-disable-line 677 678 _this.$$cacheFlushIntervalId = setInterval(function () { 679 _this.removeAll(); 680 }, _this.$$cacheFlushInterval); 681 } 682 }, 683 setCapacity: function setCapacity(capacity) { 684 if (capacity === null) { 685 delete this.$$capacity; 686 } else if (!utils.isNumber(capacity)) { 687 throw new Error('capacity must be a number!'); 688 } else if (capacity < 0) { 689 throw new Error('capacity must be greater than zero!'); 690 } else { 691 this.$$capacity = capacity; 692 } 693 var removed = {}; 694 while ($$lruHeap.size() > this.$$capacity) { 695 removed[$$lruHeap.peek().key] = this.remove($$lruHeap.peek().key); 696 } 697 return removed; 698 }, 699 setDeleteOnExpire: function setDeleteOnExpire(deleteOnExpire, setRecycleFreq) { 700 if (deleteOnExpire === null) { 701 delete this.$$deleteOnExpire; 702 } else if (!utils.isString(deleteOnExpire)) { 703 throw new Error('deleteOnExpire must be a string!'); 704 } else if (deleteOnExpire !== 'none' && deleteOnExpire !== 'passive' && deleteOnExpire !== 'aggressive') { 705 throw new Error('deleteOnExpire must be "none", "passive" or "aggressive"!'); 706 } else { 707 this.$$deleteOnExpire = deleteOnExpire; 708 } 709 if (setRecycleFreq !== false) { 710 this.setRecycleFreq(this.$$recycleFreq); 711 } 712 },
713 setMaxAge: function setMaxAge(maxAge) { 714 if (maxAge === null) { 715 this.$$maxAge = Number.MAX_VALUE; 716 } else if (!utils.isNumber(maxAge)) { 717 throw new Error('maxAge must be a number!'); 718 } else if (maxAge < 0) { 719 throw new Error('maxAge must be greater than zero!'); 720 } else { 721 this.$$maxAge = maxAge; 722 } 723 var i = void 0, 724 keys = void 0, 725 key = void 0; 726 727 $$expiresHeap.removeAll(); 728 729 if ($$storage) { 730 var keysJson = $$storage().getItem(this.$$prefix + '.keys'); 731 732 keys = keysJson ? utils.fromJson(keysJson) : []; 733 734 for (i = 0; i < keys.length; i++) { 735 key = keys[i]; 736 var itemJson = $$storage().getItem(this.$$prefix + '.data.' + key); 737 738 if (itemJson) { 739 var item = utils.fromJson(itemJson); 740 if (this.$$maxAge === Number.MAX_VALUE) { 741 item.expires = Number.MAX_VALUE; 742 } else { 743 item.expires = item.created + (item.maxAge || this.$$maxAge); 744 } 745 $$expiresHeap.push({ 746 key: key, 747 expires: item.expires 748 }); 749 } 750 } 751 } else { 752 keys = _keys($$data); 753 754 for (i = 0; i < keys.length; i++) { 755 key = keys[i]; 756 if (this.$$maxAge === Number.MAX_VALUE) { 757 $$data[key].expires = Number.MAX_VALUE; 758 } else { 759 $$data[key].expires = $$data[key].created + ($$data[key].maxAge || this.$$maxAge); 760 } 761 $$expiresHeap.push($$data[key]); 762 } 763 } 764 if (this.$$deleteOnExpire === 'aggressive') { 765 return this.removeExpired(); 766 } else { 767 return {}; 768 } 769 }, 770 setOnExpire: function setOnExpire(onExpire) { 771 if (onExpire === null) { 772 delete this.$$onExpire; 773 } else if (!utils.isFunction(onExpire)) { 774 throw new Error('onExpire must be a function!'); 775 } else { 776 this.$$onExpire = onExpire; 777 } 778 }, 779 setOptions: function setOptions(cacheOptions, strict) { 780 cacheOptions = cacheOptions || {}; 781 strict = !!strict; 782 if (!utils.isObject(cacheOptions)) { 783 throw new Error('cacheOptions must be an object!'); 784 } 785 786 if ('storagePrefix' in cacheOptions) { 787 this.$$storagePrefix = cacheOptions.storagePrefix; 788 } else if (strict) { 789 this.$$storagePrefix = defaults.storagePrefix; 790 } 791 792 this.$$prefix = this.$$storagePrefix + this.$$id; 793 794 if ('disabled' in cacheOptions) { 795 this.$$disabled = !!cacheOptions.disabled; 796 } else if (strict) { 797 this.$$disabled = defaults.disabled; 798 } 799 800 if ('deleteOnExpire' in cacheOptions) { 801 this.setDeleteOnExpire(cacheOptions.deleteOnExpire, false); 802 } else if (strict) { 803 this.setDeleteOnExpire(defaults.deleteOnExpire, false); 804 } 805 806 if ('recycleFreq' in cacheOptions) { 807 this.setRecycleFreq(cacheOptions.recycleFreq); 808 } else if (strict) { 809 this.setRecycleFreq(defaults.recycleFreq); 810 } 811 812 if ('maxAge' in cacheOptions) { 813 this.setMaxAge(cacheOptions.maxAge); 814 } else if (strict) { 815 this.setMaxAge(defaults.maxAge); 816 } 817 818 if ('storeOnResolve' in cacheOptions) { 819 this.$$storeOnResolve = !!cacheOptions.storeOnResolve; 820 } else if (strict) { 821 this.$$storeOnResolve = defaults.storeOnResolve; 822 } 823 824 if ('storeOnReject' in cacheOptions) { 825 this.$$storeOnReject = !!cacheOptions.storeOnReject; 826 } else if (strict) { 827 this.$$storeOnReject = defaults.storeOnReject; 828 } 829 830 if ('capacity' in cacheOptions) { 831 this.setCapacity(cacheOptions.capacity); 832 } else if (strict) { 833 this.setCapacity(defaults.capacity); 834 } 835 836 if ('cacheFlushInterval' in cacheOptions) { 837 this.setCacheFlushInterval(cacheOptions.cacheFlushInterval); 838 } else if (strict) { 839 this.setCacheFlushInterval(defaults.cacheFlushInterval); 840 } 841 842 if ('onExpire' in cacheOptions) { 843 this.setOnExpire(cacheOptions.onExpire); 844 } else if (strict) { 845 this.setOnExpire(defaults.onExpire); 846 } 847 848 if ('storageMode' in cacheOptions || 'storageImpl' in cacheOptions) { 849 this.setStorageMode(cacheOptions.storageMode || defaults.storageMode, cacheOptions.storageImpl || defaults.storageImpl); 850 } else if (strict) { 851 this.setStorageMode(defaults.storageMode, defaults.storageImpl); 852 } 853 }, 854 setRecycleFreq: function setRecycleFreq(recycleFreq) { 855 if (recycleFreq === null) { 856 delete this.$$recycleFreq; 857 } else if (!utils.isNumber(recycleFreq)) { 858 throw new Error('recycleFreq must be a number!'); 859 } else if (recycleFreq < 0) { 860 throw new Error('recycleFreq must be greater than zero!'); 861 } else { 862 this.$$recycleFreq = recycleFreq; 863 } 864 clearInterval(this.$$recycleFreqId); 865 if (this.$$deleteOnExpire === 'aggressive') { 866 (function (self) { 867 self.$$recycleFreqId = setInterval(function () { 868 self.removeExpired(); 869 }, self.$$recycleFreq); 870 })(this); 871 } else { 872 delete this.$$recycleFreqId; 873 } 874 }, 875 setStorageMode: function setStorageMode(storageMode, storageImpl) { 876 if (!utils.isString(storageMode)) { 877 throw new Error('storageMode must be a string!'); 878 } else if (storageMode !== 'memory' && storageMode !== 'localStorage' && storageMode !== 'sessionStorage') { 879 throw new Error('storageMode must be "memory", "localStorage" or "sessionStorage"!'); 880 } 881 882 var prevStorage = $$storage; 883 var prevData = $$data; 884 var shouldReInsert = false;
885 var items = {}; 886 887 function load(prevStorage, prevData) { 888 var keys = this.keys(); 889 var length = keys.length; 890 if (length) { 891 var _key = void 0; 892 var prevDataIsObject = utils.isObject(prevData); 893 for (var i = 0; i < length; i++) { 894 _key = keys[i]; 895 if (prevStorage) { 896 var itemJson = prevStorage().getItem(this.$$prefix + '.data.' + _key); 897 if (itemJson) { 898 items[_key] = utils.fromJson(itemJson); 899 } 900 } else if (prevDataIsObject) { 901 items[_key] = prevData[_key]; 902 } 903 this.remove(_key); 904 } 905 shouldReInsert = true; 906 } 907 } 908 909 if (!this.$$initializing) { 910 load.call(this, prevStorage, prevData); 911 } 912 913 this.$$storageMode = storageMode; 914 915 if (storageImpl) { 916 if (!utils.isObject(storageImpl)) { 917 throw new Error('storageImpl must be an object!'); 918 } else if (!('setItem' in storageImpl) || typeof storageImpl.setItem !== 'function') { 919 throw new Error('storageImpl must implement "setItem(key, value)"!'); 920 } else if (!('getItem' in storageImpl) || typeof storageImpl.getItem !== 'function') { 921 throw new Error('storageImpl must implement "getItem(key)"!'); 922 } else if (!('removeItem' in storageImpl) || typeof storageImpl.removeItem !== 'function') { 923 throw new Error('storageImpl must implement "removeItem(key)"!'); 924 } 925 $$storage = function $$storage() { 926 return storageImpl; 927 }; 928 } else if (this.$$storageMode === 'localStorage') { 929 try { 930 localStorage.setItem('cachefactory', 'cachefactory'); 931 localStorage.removeItem('cachefactory'); 932 $$storage = function $$storage() { 933 return localStorage; 934 }; 935 } catch (e) { 936 $$storage = null; 937 this.$$storageMode = 'memory'; 938 } 939 } else if (this.$$storageMode === 'sessionStorage') { 940 try { 941 sessionStorage.setItem('cachefactory', 'cachefactory'); 942 sessionStorage.removeItem('cachefactory'); 943 $$storage = function $$storage() { 944 return sessionStorage; 945 }; 946 } catch (e) { 947 $$storage = null; 948 this.$$storageMode = 'memory'; 949 } 950 } else { 951 $$storage = null; 952 this.$$storageMode = 'memory'; 953 } 954 955 if (this.$$initializing) { 956 load.call(this, $$storage, $$data); 957 } 958 959 if (shouldReInsert) { 960 var item = void 0; 961 for (var key in items) { 962 item = items[key]; 963 this.put(key, item.value, { 964 created: item.created, 965 accessed: item.accessed, 966 expires: item.expires 967 }); 968 } 969 } 970 }, 971 touch: function touch(key, options) { 972 var _this4 = this; 973 974 if (key) { 975 var val = this.get(key, { 976 onExpire: function onExpire(k, v) { 977 return _this4.put(k, v); 978 } 979 }); 980 if (val) { 981 this.put(key, val, options); 982 } 983 } else { 984 var keys = this.keys(); 985 for (var i = 0; i < keys.length; i++) { 986 this.touch(keys[i], options); 987 } 988 } 989 }, 990 values: function values() { 991 var keys = this.keys(); 992 var items = []; 993 for (var i = 0; i < keys.length; i++) { 994 items.push(this.get(keys[i])); 995 } 996 return items; 997 } 998 }; 999 1000 cache.$$initializing = true; 1001 cache.setOptions(options, true); 1002 cache.$$initializing = false; 1003 1004 return cache; 1005 } 1006 1007 function CacheFactory(cacheId, options) { 1008 return createCache(cacheId, options); 1009 } 1010 1011 CacheFactory.createCache = createCache; 1012 CacheFactory.defaults = defaults; 1013 1014 CacheFactory.info = function () { 1015 var keys = _keys(caches); 1016 var info = { 1017 size: keys.length, 1018 caches: {} 1019 }; 1020 for (var opt in defaults) { 1021 if (defaults.hasOwnProperty(opt)) { 1022 info[opt] = defaults[opt]; 1023 } 1024 } 1025 for (var i = 0; i < keys.length; i++) { 1026 var key = keys[i]; 1027 info.caches[key] = caches[key].info(); 1028 } 1029 return info; 1030 }; 1031 1032 CacheFactory.get = function (cacheId) { 1033 return caches[cacheId]; 1034 }; 1035 CacheFactory.keySet = function () { 1036 return _keySet(caches); 1037 }; 1038 CacheFactory.keys = function () { 1039 return _keys(caches); 1040 }; 1041 CacheFactory.destroy = function (cacheId) { 1042 if (caches[cacheId]) { 1043 caches[cacheId].destroy(); 1044 delete caches[cacheId]; 1045 } 1046 }; 1047 CacheFactory.destroyAll = function () { 1048 for (var cacheId in caches) { 1049 caches[cacheId].destroy(); 1050 } 1051 caches = {}; 1052 }; 1053 CacheFactory.clearAll = function () { 1054 for (var cacheId in caches) { 1055 caches[cacheId].removeAll(); 1056 } 1057 }; 1058 CacheFactory.removeExpiredFromAll = function () { 1059 var expired = {}; 1060 for (var cacheId in caches) { 1061 expired[cacheId] = caches[cacheId].removeExpired(); 1062 } 1063 return expired; 1064 }; 1065 CacheFactory.enableAll = function () { 1066 for (var cacheId in caches) { 1067 caches[cacheId].$$disabled = false;
1068 } 1069 }; 1070 CacheFactory.disableAll = function () { 1071 for (var cacheId in caches) { 1072 caches[cacheId].$$disabled = true; 1073 } 1074 }; 1075 CacheFactory.touchAll = function () { 1076 for (var cacheId in caches) { 1077 caches[cacheId].touch(); 1078 } 1079 }; 1080 1081 CacheFactory.utils = utils; 1082 CacheFactory.BinaryHeap = BinaryHeap; 1083 1084 CacheFactory.utils.equals = angular.equals; 1085 CacheFactory.utils.isObject = angular.isObject; 1086 CacheFactory.utils.fromJson = angular.fromJson; 1087 1088 function BinaryHeapProvider() { 1089 this.$get = function () { 1090 return CacheFactory.BinaryHeap; 1091 }; 1092 } 1093 1094 function CacheFactoryProvider() { 1095 this.defaults = CacheFactory.defaults; 1096 this.defaults.storagePrefix = 'angular-cache.caches.'; 1097 1098 this.$get = ['$q', function ($q) { 1099 CacheFactory.utils.Promise = $q; 1100 return CacheFactory; 1101 }]; 1102 } 1103 1104 angular.module('angular-cache', []).provider('BinaryHeap', BinaryHeapProvider).provider('CacheFactory', CacheFactoryProvider); 1105 1106 var index = 'angular-cache'; 1107 1108 return index; 1109 1110}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.