Skip to content

Commit 8055620

Browse files
author
Eric Koleda
committed
Cache null values.
1 parent 4112c3d commit 8055620

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
@@ -91,6 +91,37 @@ describe('Service', function() {
9191
assert.equal(cache.counter, cacheStart);
9292
assert.equal(properties.counter, propertiesStart);
9393
});
94+
95+
it('should load null tokens from the cache',
96+
function() {
97+
var cache = new MockCache();
98+
var properties = new MockProperties();
99+
for (var i = 0; i < 10; ++i) {
100+
var service = OAuth2.createService('test')
101+
.setPropertyStore(properties)
102+
.setCache(cache);
103+
service.getToken();
104+
}
105+
assert.equal(properties.counter, 1);
106+
});
107+
108+
it('should load null tokens from memory',
109+
function() {
110+
var cache = new MockCache();
111+
var properties = new MockProperties();
112+
var service = OAuth2.createService('test')
113+
.setPropertyStore(properties)
114+
.setCache(cache);
115+
116+
service.getToken();
117+
var cacheStart = cache.counter;
118+
var propertiesStart = properties.counter;
119+
for (var i = 0; i < 10; ++i) {
120+
service.getToken();
121+
}
122+
assert.equal(cache.counter, cacheStart);
123+
assert.equal(properties.counter, propertiesStart);
124+
});
94125
});
95126

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

0 commit comments

Comments
 (0)