Skip to content

Commit d55b23e

Browse files
committed
removed fetch()
1 parent b48cd14 commit d55b23e

File tree

3 files changed

+17
-64
lines changed

3 files changed

+17
-64
lines changed

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,3 @@ cache.set('kyle', 'rox', { expiresIn: { minutes: 1 } }); // 1 minute
3333
cache.set('kyle', 'rox', { expiresIn: { hours: 1 } }); // 1 hour
3434
cache.set('kyle', 'rox', { expiresIn: { days: 1 } }); // 1 day
3535
```
36-
37-
fetch(key, options);
38-
---
39-
```
40-
cache.fetch('mykey', {
41-
expiresIn: 1000 * 60 * 2, // 2 minutes
42-
get: function(done){
43-
makeAjaxCall(function(data){
44-
done(data);
45-
});
46-
},
47-
done: function(data){
48-
doSomeStuff(data);
49-
}
50-
});
51-
```

lib/cache.js

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,11 @@ Cache.prototype.set = function(key, value, options){
4545
data[key] = value;
4646
store.call(this, data);
4747

48-
if(options){
48+
if(hasExpiration(options)){
4949
setExpiration.call(this, key, options);
5050
}
5151
}
5252

53-
Cache.prototype.fetch = function(key, options){
54-
validateKey(key);
55-
validateOption(options, 'get', 'fetch');
56-
validateOption(options, 'done', 'fetch');
57-
58-
var self = this;
59-
var value = self.get(key);
60-
61-
if(value){
62-
options.done(value);
63-
return;
64-
}
65-
66-
options.done.__freshFetch = true;
67-
options.get(function(value){
68-
if(options.done.__freshFetch){
69-
delete options.done.__freshFetch;
70-
self.set(key, value, options);
71-
}
72-
73-
done(value);
74-
});
75-
}
76-
7753
function load(){
7854
var stringifiedData = this.storage[this.options.namespace];
7955
if(stringifiedData === undefined){
@@ -87,18 +63,6 @@ function store(data){
8763
this.storage[this.options.namespace] = stringifiedData;
8864
}
8965

90-
function validateKey(key){
91-
if(!key){
92-
throw 'key is required!';
93-
}
94-
}
95-
96-
function validateOption(options, key, functionName){
97-
if(!options[key] || typeof options[key] !== 'function'){
98-
throw 'Cache.' + functionName + '() requires you pass in a "' + key + '" callback option';
99-
}
100-
}
101-
10266
function setExpiration(key, options){
10367
var expirationMilli = getExpirationMilliSeconds(options);
10468
var date = new Date();
@@ -164,4 +128,8 @@ function number(num){
164128
}
165129
}
166130

131+
function hasExpiration(options){
132+
return typeof options == 'number' || (typeof options == 'string' && parseInt(options) != NaN) || (typeof options == 'object' && options.expiresIn)
133+
}
134+
167135
module.exports = Cache;

tests/cache_test.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ describe('The module', function() {
1111
cache = new Cache({ localStorage: localStorage });
1212
});
1313

14-
describe('get', function() {
15-
beforeEach(function(){
16-
cache.set('kyle', 'rox');
17-
});
18-
19-
it('Gets the value', function() {
20-
assert(cache.get('kyle') == 'rox');
21-
});
14+
describe('get()', function() {
15+
describe('When a value is set using set()', function(){
16+
beforeEach(function(){
17+
cache.set('kyle', 'rox');
18+
});
2219

20+
it('Gets the value', function() {
21+
assert(cache.get('kyle') == 'rox');
22+
});
23+
})
2324
});
2425

25-
describe('set', function(){
26-
26+
describe('set()', function(){
2727
describe('When expiration is set', function(){
2828
var itPasses = function(){
2929
it('expires in that number * milliseconds', function(done){
@@ -87,5 +87,6 @@ describe('The module', function() {
8787

8888
})
8989

90-
})
90+
});
91+
9192
});

0 commit comments

Comments
 (0)