@@ -3,6 +3,7 @@ var Promise = require('bluebird');
33var models = require ( '../../models' ) ;
44var _ = require ( 'lodash' ) ;
55var common = require ( '../utils/common' ) ;
6+ var factory = require ( '../utils/factory' ) ;
67
78var proto = module . exports = function ( ) {
89 function ClientManager ( ) {
@@ -12,6 +13,56 @@ var proto = module.exports = function (){
1213 return ClientManager ;
1314} ;
1415
16+ const UPDATE_CHECK = "UPDATE_CHECK" ;
17+ const EXPIRED = 600 ;
18+
19+ proto . getUpdateCheckCacheKey = function ( deploymentKey , appVersion , label , packageHash ) {
20+ return [ UPDATE_CHECK , deploymentKey , appVersion , label , packageHash ] . join ( ':' ) ;
21+ }
22+
23+ proto . clearUpdateCheckCache = function ( deploymentKey , appVersion , label , packageHash ) {
24+ let redisCacheKey = this . getUpdateCheckCacheKey ( deploymentKey , appVersion , label , packageHash ) ;
25+ var client = factory . getRedisClient ( "default" ) ;
26+ return client . keysAsync ( redisCacheKey )
27+ . then ( function ( data ) {
28+ if ( _ . isArray ( data ) ) {
29+ return Promise . map ( data , function ( key ) {
30+ return client . delAsync ( key ) ;
31+ } ) ;
32+ }
33+ return null ;
34+ } ) ;
35+ }
36+
37+ proto . updateCheckFromCache = function ( deploymentKey , appVersion , label , packageHash ) {
38+ const self = this ;
39+ let redisCacheKey = self . getUpdateCheckCacheKey ( deploymentKey , appVersion , label , packageHash ) ;
40+ var updateCheckCache = _ . get ( require ( '../config' ) , 'common.updateCheckCache' , false ) ;
41+ if ( updateCheckCache === false ) {
42+ return self . updateCheck ( deploymentKey , appVersion , label , packageHash ) ;
43+ }
44+ var client = factory . getRedisClient ( "default" ) ;
45+ return client . getAsync ( redisCacheKey )
46+ . then ( function ( data ) {
47+ if ( data ) {
48+ try {
49+ var obj = JSON . parse ( data ) ;
50+ return obj ;
51+ } catch ( e ) {
52+ }
53+ }
54+ return self . updateCheck ( deploymentKey , appVersion , label , packageHash )
55+ . then ( function ( rs ) {
56+ try {
57+ var strRs = JSON . stringify ( rs ) ;
58+ client . setexAsync ( redisCacheKey , EXPIRED , strRs ) ;
59+ } catch ( e ) {
60+ }
61+ return rs ;
62+ } ) ;
63+ } )
64+ }
65+
1566proto . updateCheck = function ( deploymentKey , appVersion , label , packageHash ) {
1667 var rs = {
1768 downloadURL : "" ,
0 commit comments