expiringstorage.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 expiration.
  16. *
  17. */
  18. goog.provide('goog.storage.ExpiringStorage');
  19. goog.require('goog.storage.RichStorage');
  20. /**
  21. * Provides a storage with expiring keys.
  22. *
  23. * @param {!goog.storage.mechanism.Mechanism} mechanism The underlying
  24. * storage mechanism.
  25. * @constructor
  26. * @struct
  27. * @extends {goog.storage.RichStorage}
  28. */
  29. goog.storage.ExpiringStorage = function(mechanism) {
  30. goog.storage.ExpiringStorage.base(this, 'constructor', mechanism);
  31. };
  32. goog.inherits(goog.storage.ExpiringStorage, goog.storage.RichStorage);
  33. /**
  34. * Metadata key under which the expiration time is stored.
  35. *
  36. * @type {string}
  37. * @protected
  38. */
  39. goog.storage.ExpiringStorage.EXPIRATION_TIME_KEY = 'expiration';
  40. /**
  41. * Metadata key under which the creation time is stored.
  42. *
  43. * @type {string}
  44. * @protected
  45. */
  46. goog.storage.ExpiringStorage.CREATION_TIME_KEY = 'creation';
  47. /**
  48. * Returns the wrapper creation time.
  49. *
  50. * @param {!Object} wrapper The wrapper.
  51. * @return {number|undefined} Wrapper creation time.
  52. */
  53. goog.storage.ExpiringStorage.getCreationTime = function(wrapper) {
  54. return wrapper[goog.storage.ExpiringStorage.CREATION_TIME_KEY];
  55. };
  56. /**
  57. * Returns the wrapper expiration time.
  58. *
  59. * @param {!Object} wrapper The wrapper.
  60. * @return {number|undefined} Wrapper expiration time.
  61. */
  62. goog.storage.ExpiringStorage.getExpirationTime = function(wrapper) {
  63. return wrapper[goog.storage.ExpiringStorage.EXPIRATION_TIME_KEY];
  64. };
  65. /**
  66. * Checks if the data item has expired.
  67. *
  68. * @param {!Object} wrapper The wrapper.
  69. * @return {boolean} True if the item has expired.
  70. */
  71. goog.storage.ExpiringStorage.isExpired = function(wrapper) {
  72. var creation = goog.storage.ExpiringStorage.getCreationTime(wrapper);
  73. var expiration = goog.storage.ExpiringStorage.getExpirationTime(wrapper);
  74. return !!expiration && expiration < goog.now() ||
  75. !!creation && creation > goog.now();
  76. };
  77. /**
  78. * Set an item in the storage.
  79. *
  80. * @param {string} key The key to set.
  81. * @param {*} value The value to serialize to a string and save.
  82. * @param {number=} opt_expiration The number of miliseconds since epoch
  83. * (as in goog.now()) when the value is to expire. If the expiration
  84. * time is not provided, the value will persist as long as possible.
  85. * @override
  86. */
  87. goog.storage.ExpiringStorage.prototype.set = function(
  88. key, value, opt_expiration) {
  89. var wrapper = goog.storage.RichStorage.Wrapper.wrapIfNecessary(value);
  90. if (wrapper) {
  91. if (opt_expiration) {
  92. if (opt_expiration < goog.now()) {
  93. goog.storage.ExpiringStorage.prototype.remove.call(this, key);
  94. return;
  95. }
  96. wrapper[goog.storage.ExpiringStorage.EXPIRATION_TIME_KEY] =
  97. opt_expiration;
  98. }
  99. wrapper[goog.storage.ExpiringStorage.CREATION_TIME_KEY] = goog.now();
  100. }
  101. goog.storage.ExpiringStorage.base(this, 'set', key, wrapper);
  102. };
  103. /**
  104. * Get an item wrapper (the item and its metadata) from the storage.
  105. *
  106. * @param {string} key The key to get.
  107. * @param {boolean=} opt_expired If true, return expired wrappers as well.
  108. * @return {(!Object|undefined)} The wrapper, or undefined if not found.
  109. * @override
  110. */
  111. goog.storage.ExpiringStorage.prototype.getWrapper = function(key, opt_expired) {
  112. var wrapper = goog.storage.ExpiringStorage.base(this, 'getWrapper', key);
  113. if (!wrapper) {
  114. return undefined;
  115. }
  116. if (!opt_expired && goog.storage.ExpiringStorage.isExpired(wrapper)) {
  117. goog.storage.ExpiringStorage.prototype.remove.call(this, key);
  118. return undefined;
  119. }
  120. return wrapper;
  121. };