Skip to content

Commit 3572ba7

Browse files
author
Eric Koleda
authored
Merge pull request #146 from gsuitedevs/notoken
Cache null values in storage
2 parents bcf34f8 + 8055620 commit 3572ba7

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/Storage.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,38 @@ function Storage_(prefix, properties, optCache) {
4040
*/
4141
Storage_.CACHE_EXPIRATION_TIME_SECONDS = 21600; // 6 hours.
4242

43+
/**
44+
* The special value to use in the cache to indicate that there is no value.
45+
* @type {string}
46+
* @private
47+
*/
48+
Storage_.CACHE_NULL_VALUE = '__NULL__';
49+
4350
/**
4451
* Gets a stored value.
4552
* @param {string} key The key.
4653
* @return {*} The stored value.
4754
*/
4855
Storage_.prototype.getValue = function(key) {
49-
// Check memory.
50-
if (this.memory_[key]) {
51-
return this.memory_[key];
52-
}
53-
5456
var prefixedKey = this.getPrefixedKey_(key);
5557
var jsonValue;
5658
var value;
5759

60+
// Check memory.
61+
if (value = this.memory_[key]) {
62+
if (value === Storage_.CACHE_NULL_VALUE) {
63+
return null;
64+
}
65+
return value;
66+
}
67+
5868
// Check cache.
5969
if (this.cache_ && (jsonValue = this.cache_.get(prefixedKey))) {
6070
value = JSON.parse(jsonValue);
6171
this.memory_[key] = value;
72+
if (value === Storage_.CACHE_NULL_VALUE) {
73+
return null;
74+
}
6275
return value;
6376
}
6477

@@ -73,7 +86,13 @@ Storage_.prototype.getValue = function(key) {
7386
return value;
7487
}
7588

76-
// Not found.
89+
// Not found. Store a special null value in the memory and cache to reduce
90+
// hits on the PropertiesService.
91+
this.memory_[key] = Storage_.CACHE_NULL_VALUE;
92+
if (this.cache_) {
93+
this.cache_.put(prefixedKey, JSON.stringify(Storage_.CACHE_NULL_VALUE),
94+
Storage_.CACHE_EXPIRATION_TIME_SECONDS);
95+
}
7796
return null;
7897
};
7998

test/test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@ describe('Service', function() {
9696
assert.equal(cache.counter, cacheStart);
9797
assert.equal(properties.counter, propertiesStart);
9898
});
99+
100+
it('should load null tokens from the cache',
101+
function() {
102+
var cache = new MockCache();
103+
var properties = new MockProperties();
104+
for (var i = 0; i < 10; ++i) {
105+
var service = OAuth2.createService('test')
106+
.setPropertyStore(properties)
107+
.setCache(cache);
108+
service.getToken();
109+
}
110+
assert.equal(properties.counter, 1);
111+
});
112+
113+
it('should load null tokens from memory',
114+
function() {
115+
var cache = new MockCache();
116+
var properties = new MockProperties();
117+
var service = OAuth2.createService('test')
118+
.setPropertyStore(properties)
119+
.setCache(cache);
120+
121+
service.getToken();
122+
var cacheStart = cache.counter;
123+
var propertiesStart = properties.counter;
124+
for (var i = 0; i < 10; ++i) {
125+
service.getToken();
126+
}
127+
assert.equal(cache.counter, cacheStart);
128+
assert.equal(properties.counter, propertiesStart);
129+
});
99130
});
100131

101132
describe('#saveToken_()', function() {

0 commit comments

Comments
 (0)