collectablestorage.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Provides a convenient API for data persistence with data
  16. * expiration and user-initiated expired key collection.
  17. *
  18. */
  19. goog.provide('goog.storage.CollectableStorage');
  20. goog.require('goog.array');
  21. goog.require('goog.iter');
  22. goog.require('goog.storage.ErrorCode');
  23. goog.require('goog.storage.ExpiringStorage');
  24. goog.require('goog.storage.RichStorage');
  25. /**
  26. * Provides a storage with expiring keys and a collection method.
  27. *
  28. * @param {!goog.storage.mechanism.IterableMechanism} mechanism The underlying
  29. * storage mechanism.
  30. * @constructor
  31. * @struct
  32. * @extends {goog.storage.ExpiringStorage}
  33. */
  34. goog.storage.CollectableStorage = function(mechanism) {
  35. goog.storage.CollectableStorage.base(this, 'constructor', mechanism);
  36. };
  37. goog.inherits(goog.storage.CollectableStorage, goog.storage.ExpiringStorage);
  38. /**
  39. * Iterate over keys and returns those that expired.
  40. *
  41. * @param {goog.iter.Iterable} keys keys to iterate over.
  42. * @param {boolean=} opt_strict Also return invalid keys.
  43. * @return {!Array<string>} Keys of values that expired.
  44. * @private
  45. */
  46. goog.storage.CollectableStorage.prototype.getExpiredKeys_ = function(
  47. keys, opt_strict) {
  48. var keysToRemove = [];
  49. goog.iter.forEach(keys, function(key) {
  50. // Get the wrapper.
  51. var wrapper;
  52. try {
  53. wrapper = goog.storage.CollectableStorage.prototype.getWrapper.call(
  54. this, key, true);
  55. } catch (ex) {
  56. if (ex == goog.storage.ErrorCode.INVALID_VALUE) {
  57. // Bad wrappers are removed in strict mode.
  58. if (opt_strict) {
  59. keysToRemove.push(key);
  60. }
  61. // Skip over bad wrappers and continue.
  62. return;
  63. }
  64. // Unknown error, escalate.
  65. throw ex;
  66. }
  67. if (!goog.isDef(wrapper)) {
  68. // A value for a given key is no longer available. Clean it up.
  69. keysToRemove.push(key);
  70. return;
  71. }
  72. // Remove expired objects.
  73. if (goog.storage.ExpiringStorage.isExpired(wrapper)) {
  74. keysToRemove.push(key);
  75. // Continue with the next key.
  76. return;
  77. }
  78. // Objects which can't be decoded are removed in strict mode.
  79. if (opt_strict) {
  80. try {
  81. goog.storage.RichStorage.Wrapper.unwrap(wrapper);
  82. } catch (ex) {
  83. if (ex == goog.storage.ErrorCode.INVALID_VALUE) {
  84. keysToRemove.push(key);
  85. // Skip over bad wrappers and continue.
  86. return;
  87. }
  88. // Unknown error, escalate.
  89. throw ex;
  90. }
  91. }
  92. }, this);
  93. return keysToRemove;
  94. };
  95. /**
  96. * Cleans up the storage by removing expired keys.
  97. *
  98. * @param {goog.iter.Iterable} keys List of all keys.
  99. * @param {boolean=} opt_strict Also remove invalid keys.
  100. * @return {!Array<string>} a list of expired keys.
  101. * @protected
  102. */
  103. goog.storage.CollectableStorage.prototype.collectInternal = function(
  104. keys, opt_strict) {
  105. var keysToRemove = this.getExpiredKeys_(keys, opt_strict);
  106. goog.array.forEach(keysToRemove, function(key) {
  107. goog.storage.CollectableStorage.prototype.remove.call(this, key);
  108. }, this);
  109. return keysToRemove;
  110. };
  111. /**
  112. * Cleans up the storage by removing expired keys.
  113. *
  114. * @param {boolean=} opt_strict Also remove invalid keys.
  115. */
  116. goog.storage.CollectableStorage.prototype.collect = function(opt_strict) {
  117. this.collectInternal(
  118. /** @type {goog.storage.mechanism.IterableMechanism} */ (this.mechanism)
  119. .__iterator__(true),
  120. opt_strict);
  121. };