File tree Expand file tree Collapse file tree 7 files changed +84
-2
lines changed
Expand file tree Collapse file tree 7 files changed +84
-2
lines changed Original file line number Diff line number Diff line change 1+ root = true
2+
3+ [* ]
4+ indent_style = space
5+ indent_size = 2
6+ end_of_line = lf
7+ insert_final_newline = true
Original file line number Diff line number Diff line change @@ -255,4 +255,3 @@ gulp.task('dev', [
255255 'babel-demo'
256256 ] ) . on ( 'change' , reload ) ;
257257} ) ;
258-
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ AV._ = require('underscore');
1919AV . version = require ( './version' ) ;
2020AV . Promise = require ( './promise' ) ;
2121AV . localStorage = require ( './localstorage' ) ;
22+ AV . Cache = require ( './Cache' ) ;
2223
2324// 挂载所有内部配置项
2425AV . _config = AV . _config || { } ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 77
88const _ = require ( 'underscore' ) ;
99const ajax = require ( './browserify-wrapper/ajax' ) ;
10+ const Cache = require ( './cache' ) ;
1011
1112const 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' :
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments