Debian Codegraph @

closure-library/closure/goog/storage/expiringstorage.js

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