Skip to content

Commit 9371d38

Browse files
committed
feat(Cache): implement Cache
1 parent 7ecaf89 commit 9371d38

File tree

7 files changed

+84
-2
lines changed

7 files changed

+84
-2
lines changed

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
insert_final_newline = true

gulpfile.babel.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,3 @@ gulp.task('dev', [
255255
'babel-demo'
256256
]).on('change', reload);
257257
});
258-

src/av.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ AV._ = require('underscore');
1919
AV.version = require('./version');
2020
AV.Promise = require('./promise');
2121
AV.localStorage = require('./localstorage');
22+
AV.Cache = require('./Cache');
2223

2324
// 挂载所有内部配置项
2425
AV._config = AV._config || {};

src/cache.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
const storage = require('./localstorage');
3+
const AV = require('./av');
4+
5+
exports.get = (key) => {
6+
return storage.getItemAsync(`${AV.applicationId}/${key}`)
7+
.then(cache => {
8+
try {
9+
cache = JSON.parse(cache);
10+
} catch (e) {
11+
return null;
12+
}
13+
if (cache) {
14+
const expired = cache.expiredAt && cache.expiredAt < Date.now();
15+
if (!expired) {
16+
return cache.value;
17+
}
18+
}
19+
return null;
20+
});
21+
};
22+
23+
exports.set = (key, value, ttl) => {
24+
const cache = {
25+
value
26+
};
27+
if (typeof ttl === 'number') {
28+
cache.expiredAt = Date.now() + ttl;
29+
}
30+
return storage.setItemAsync(
31+
`${AV.applicationId}/${key}`,
32+
JSON.stringify(cache)
33+
);
34+
};

src/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const _ = require('underscore');
99
const ajax = require('./browserify-wrapper/ajax');
10+
const Cache = require('./cache');
1011

1112
const init = (AV) => {
1213

@@ -115,7 +116,29 @@ const init = (AV) => {
115116
AV._useMasterKey = false;
116117
};
117118

118-
const setRegionServer = (region) => {
119+
const setRegionServer = (region = 'cn') => {
120+
// AVConfig.region = region;
121+
// AVConfig.APIServerURL = API_HOST[region];
122+
// if (region === 'cn') {
123+
// Cache.get('APIServerURL').then(cachedServerURL => {
124+
// if (cachedServerURL) {
125+
// return cachedServerURL;
126+
// } else {
127+
// return ajax('get', `http://app-router.leancloud.cn/route?appId=${AV.applicationId}`)
128+
// .then(servers => {
129+
// if (servers.api_server) {
130+
// Cache.set('APIServerURL', servers.api_server, 30 * 24 * 3600000);
131+
// return servers.api_server;
132+
// }
133+
// });
134+
// }
135+
// }).then(serverURL => {
136+
// if (AVConfig.APIServerURL === API_HOST[region]) {
137+
// AVConfig.APIServerURL = serverURL;
138+
// }
139+
// })
140+
// }
141+
119142
// 服务器地区选项,默认为中国大陆
120143
switch (region) {
121144
case 'us':

test/cache.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var Cache = AV.Cache;
2+
const wait = time => new Promise(resolve => setTimeout(resolve, time));
3+
4+
describe('Cache', () => {
5+
const getValue = () => Cache.get('__test');
6+
it('get/set', () =>
7+
Cache.set('__test', 1).then(getValue).then(value => {
8+
expect(value).to.be(1);
9+
return Cache.set('__test', '1', 100).then(getValue);
10+
}).then(value => {
11+
expect(value).to.be('1');
12+
return wait(110).then(getValue);
13+
}).then(value => {
14+
expect(value).to.be(null);
15+
})
16+
);
17+
});

test/test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</script>
2626
<script src="promise.js"></script>
2727
<script src="storage.js"></script>
28+
<script src="cache.js"></script>
2829
<script src="file.js"></script>
2930
<script src="error.js"></script>
3031
<script src="object.js"></script>

0 commit comments

Comments
 (0)