Skip to content

Commit f31e5e4

Browse files
committed
Refactor adapter example for cache to map old one
1 parent ae763df commit f31e5e4

File tree

1 file changed

+35
-4
lines changed
  • snippets/adapters/cache-lifetime

1 file changed

+35
-4
lines changed

snippets/adapters/cache-lifetime/new.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,40 @@ import DataStore from '@ember-data/store';
44
export default class Store extends DataStore {
55
constructor(args) {
66
super(args);
7-
this.lifetimes = new LifetimesService(this, {
8-
apiCacheSoftExpires: 30_000,
9-
apiCacheHardExpires: 60_000
10-
});
7+
// This is default configuration that would be set automatically be Ember Data
8+
// this.lifetimes = new LifetimesService(this, {
9+
// apiCacheSoftExpires: 30_000,
10+
// apiCacheHardExpires: 60_000
11+
// });
12+
this.lifetimes = {
13+
isHardExpired(identifier) {
14+
const cached = this.store.cache.peekRequest(identifier);
15+
const twentyMinutesInMs = 20 * 60 * 1000;
16+
17+
function isStale(headers, expirationTime) {
18+
const date = headers.get('date');
19+
20+
if (!date) {
21+
return true;
22+
}
23+
24+
const time = new Date(date).getTime();
25+
const now = Date.now();
26+
const deadline = time + expirationTime;
27+
28+
const result = now > deadline;
29+
30+
return result;
31+
}
32+
33+
return !cached || !cached.response || isStale(cached.response.headers, twentyMinutesInMs);
34+
},
35+
36+
isSoftExpired(identifier) {
37+
const { downlink, effectiveType } = navigator.connection;
38+
39+
return downlink > 0 && effectiveType === '4g';
40+
}
41+
}
1142
}
1243
}

0 commit comments

Comments
 (0)