Skip to content

Commit

Permalink
Merge pull request #19 from HuYuee/feature/cacheSwitch
Browse files Browse the repository at this point in the history
缓存刷新启停功能开发
  • Loading branch information
Vastness authored Aug 10, 2022
2 parents 8817608 + 595b90e commit db74661
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion MSDKDns/CacheManager/MSDKDnsNetworkManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ - (void)getHostsByKeepAliveDomains{
//对保活域名发送解析请求
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains];
if (keepAliveDomains && keepAliveDomains.count > 0) {
BOOL enableKeepDomainsAlive = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableKeepDomainsAlive];
if (enableKeepDomainsAlive && keepAliveDomains && keepAliveDomains.count > 0) {
[[MSDKDnsManager shareInstance] refreshCacheDelay:keepAliveDomains clearDispatchTag:NO];
}
});
Expand Down
2 changes: 2 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsParamsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- (void)msdkDnsSetPreResolvedDomains: (NSArray *)domains;
- (void)msdkDnsSetAddressType: (HttpDnsAddressType)addressType;
- (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains;
- (void)msdkDnsSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive;

- (NSString *) msdkDnsGetMDnsIp;
- (NSString *) msdkDnsGetMOpenId;
Expand All @@ -46,4 +47,5 @@
- (NSArray *)msdkDnsGetPreResolvedDomains;
- (HttpDnsAddressType)msdkDnsGetAddressType;
- (NSArray *)msdkDnsGetKeepAliveDomains;
- (BOOL)msdkDnsGetEnableKeepDomainsAlive;
@end
10 changes: 10 additions & 0 deletions MSDKDns/CacheManager/MSDKDnsParamsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ @interface MSDKDnsParamsManager()
@property (strong, nonatomic, readwrite) NSArray* preResolvedDomains;
@property (assign, nonatomic, readwrite) HttpDnsAddressType msdkAddressType;
@property (strong, nonatomic, readwrite) NSArray* keepAliveDomains;
@property (assign, nonatomic, readwrite) BOOL enableKeepDomainsAlive;

@end

Expand All @@ -56,6 +57,7 @@ - (id) init {
_minutesBeforeSwitchToMain = 10;
_enableReport = NO;
_msdkAddressType = HttpDnsAddressTypeAuto;
_enableKeepDomainsAlive = YES;
}
return self;
}
Expand Down Expand Up @@ -164,6 +166,11 @@ - (void)msdkDnsSetKeepAliveDomains: (NSArray *)domains {
});
}

- (void)msdkDnsSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive {
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
self.enableKeepDomainsAlive = enableKeepDomainsAlive;
});
}


#pragma mark - getter
Expand Down Expand Up @@ -243,6 +250,9 @@ - (NSArray *)msdkDnsGetKeepAliveDomains {
return _keepAliveDomains;
}

- (BOOL)msdkDnsGetEnableKeepDomainsAlive {
return _enableKeepDomainsAlive;
}


@end
5 changes: 5 additions & 0 deletions MSDKDns/MSDKDns.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ typedef struct DnsConfigStruct {
*/
- (void) WGSetKeepAliveDomains:(NSArray *)domains;

/**
* 设置开启和关闭缓存启停功能,默认开启
*/
- (void) WGSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive;

#pragma mark - 域名解析接口,按需调用
/**
域名同步解析(通用接口)
Expand Down
4 changes: 4 additions & 0 deletions MSDKDns/MSDKDns.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ - (void) WGSetKeepAliveDomains:(NSArray *)domains {
}
}

- (void) WGSetEnableKeepDomainsAlive: (BOOL)enableKeepDomainsAlive {
[[MSDKDnsParamsManager shareInstance] msdkDnsSetEnableKeepDomainsAlive:enableKeepDomainsAlive];
}

- (void) WGSetHijackDomainArray:(NSArray *)hijackDomainArray {
if (hijackDomainArray) {
[[MSDKDnsParamsManager shareInstance] setHijackDomainArray:[hijackDomainArray copy]];
Expand Down
17 changes: 12 additions & 5 deletions MSDKDns/MSDKDnsService.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ - (void)startCheck:(float)timeOut DnsId:(int)dnsId DnsKey:(NSString *)dnsKey enc
[self callNotify];
return;
}

if (_netStack == msdkdns::MSDKDNS_ELocalIPStack_None) {
MSDKDNSLOG(@"No network stack, please check your network setting!");
[self callNotify];
Expand Down Expand Up @@ -158,15 +158,16 @@ - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)do
[self callBack:resolver Info:info];
if (resolver == self.httpDnsResolver_A || resolver == self.httpDnsResolver_4A || resolver == self.httpDnsResolver_BOTH) {
NSArray *keepAliveDomains = [[MSDKDnsParamsManager shareInstance] msdkDnsGetKeepAliveDomains];
BOOL enableKeepDomainsAlive = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableKeepDomainsAlive];
// 获取延迟记录字典
NSMutableDictionary *domainISOpenDelayDispatch = [[MSDKDnsManager shareInstance] msdkDnsGetDomainISOpenDelayDispatch];
[domainInfo enumerateKeysAndObjectsUsingBlock:^(id _Nonnull domain, id _Nonnull obj, BOOL * _Nonnull stop) {
// NSLog(@"domain = %@", domain);
// NSLog(@"domainInfo = %@", domainInfo);

// 判断此次请求的域名中有多少属于保活域名,是则开启延时解析请求,自动刷新缓存
if (keepAliveDomains && domain && [keepAliveDomains containsObject:domain]) {

if (enableKeepDomainsAlive && keepAliveDomains && domain && [keepAliveDomains containsObject:domain]) {
NSMutableString * afterTime = [[NSMutableString alloc] init];

if(resolver == self.httpDnsResolver_BOTH){
Expand Down Expand Up @@ -198,8 +199,14 @@ - (void)resolver:(MSDKDnsResolver *)resolver didGetDomainInfo:(NSDictionary *)do
MSDKDNSLOG(@"Start the delayed execution task, it is expected to start requesting the domain name %@ after %f seconds", domain, afterTime.floatValue);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,afterTime.floatValue* NSEC_PER_SEC), [MSDKDnsInfoTool msdkdns_queue], ^{
// NSLog(@"延时更新请求开始!请求域名为%@",domain);
MSDKDNSLOG(@"The cache update request start! request domain:%@",domain);
[[MSDKDnsManager shareInstance] refreshCacheDelay:@[domain] clearDispatchTag:YES];
BOOL enableKeepDomainsAlive = [[MSDKDnsParamsManager shareInstance] msdkDnsGetEnableKeepDomainsAlive];
if (enableKeepDomainsAlive) {
MSDKDNSLOG(@"The cache update request start! request domain:%@",domain);
[[MSDKDnsManager shareInstance] refreshCacheDelay:@[domain] clearDispatchTag:YES];
}else {
[[MSDKDnsManager shareInstance] msdkDnsClearDomainsOpenDelayDispatch:@[domain]];

}
});
}

Expand Down

0 comments on commit db74661

Please sign in to comment.