YH_DNSCacheManager.m
5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// YH_DNSCacheManager.m
// YH_Mall
//
// Created by jhsonzhi on 16/7/18.
// Copyright © 2016年 YOHO. All rights reserved.
//
#import "YH_DNSCacheManager.h"
#import "YH_DNSMemoryCache.h"
#import "YH_DNSDomainModel.h"
#import "YH_DNSUtils.h"
#import "YH_DNSHttpDnsPack.h"
@interface YH_DNSCacheManager()
@property (strong, nonatomic) YH_DNSMemoryCache *memoryCacheDic;
@end
@implementation YH_DNSCacheManager
+ (instancetype)sharedInstance {
static YH_DNSCacheManager *sharedInstance = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedInstance = [[YH_DNSCacheManager alloc]init];
});
return sharedInstance;
}
- (id)init {
if (self = [super init]) {
_memoryCacheDic = [YH_DNSMemoryCache sharedInstance];
}
return self;
}
- (NSArray *)getExpireDnsCache {
@synchronized(self) {
return [self.memoryCacheDic getExpireDnsCache];
}
}
- (NSArray *)getAllModels {
@synchronized(self) {
return [self.memoryCacheDic getAllModels];
}
}
- (void)addMemoryCache:(NSString *)hostName model:(YH_DNSDomainModel *)model {
@synchronized(self) {
if (model == nil) {
NSLog(@"ERROR:%s:%d model is nil.", __func__, __LINE__);
return;
}
if (model.ipModelArray == nil || model.ipModelArray.count == 0) {
NSLog(@"ERROR:%s:%d model.ipModelArray is nil.", __func__, __LINE__);
return;
}
for (YH_DNSIpModel *ipModel in model.ipModelArray) {
if (!ipModel) {
NSLog(@"WARNING:%s an ipModel in model.ipModelArray is nil.", __func__);
return;
}
}
[self.memoryCacheDic addModel:model keyHost:hostName];
return;
}
}
//本函数只用于更新从本地dns获取的ip, 从httpserver 获取的ip 不应该用此函数更新。
//- (void)updateMemoryCache:(NSString *)domain model:(YH_DNSDomainModel *)model
//{
// @synchronized(self) {
// if (model == nil) {
// NSLog(@"ERROR:%s:%d model is nil.", __func__, __LINE__);
// return;
// }
// if (model.ipModelArray == nil || model.ipModelArray.count == 0) {
// NSLog(@"ERROR:%s:%d model.ipModelArray is nil.", __func__, __LINE__);
// return;
// }
//
// [self.memoryCacheDic updateModel:model keyHost:domain];
// return;
// }
//}
- (NSString *)getRandomIp:(YH_DNSDomainModel *)domainModel{
@synchronized(self) {
NSString *randomIp = nil;
NSInteger modelCount = [domainModel.ipModelArray count];
if (modelCount > 0) {
YH_DNSIpModel *ipModel = domainModel.ipModelArray[arc4random()%modelCount];
randomIp = ipModel.ip;
}
return randomIp;
}
}
- (NSDictionary *)getDnsCacheByHost:(NSString *)hostName {
//这个锁是防止 多个线程重复的 从数据库取数据插入缓存。
@synchronized(self) {
NSMutableDictionary *resultDic = nil;
YH_DNSDomainModel *model = [self.memoryCacheDic getModelByKeyUrl:hostName];
NSString* source = @"memorycache";
//TODO:读取数据库
if (model != nil) {
if ([YH_DNSUtils isDomainModelExpired:model]) {
NSLog(@"INFO:%s:%d model(domain:%@) from %@ is expired.", __func__, __LINE__, model.domain, source);
return nil;
} else if (model.ipModelArray == nil || model.ipModelArray.count == 0) {
NSLog(@"INFO:%s:%d model(domain:%@) from %@ all ip are expired.", __func__, __LINE__, model.domain, source);
return nil;
} else {
//TODO:把数据库里的有效model 存到缓存里。
}
NSString * resultIpString = [self getRandomIp:model];
resultDic = [NSMutableDictionary dictionary];
[resultDic setValue:((!YHDNS_isStrEmpty(resultIpString)) ? resultIpString: @"") forKey:@"ip"];
[resultDic setValue:@(model.isNeedPreFetch) forKey:@"prefetch"];
}
return resultDic;
}
}
- (YH_DNSDomainModel *)insertDnsCache:(YH_DNSHttpDnsPack *) dnsPack {
@synchronized(self) {
YH_DNSDomainModel* model = [[YH_DNSDomainModel alloc]init];
model.domain = dnsPack.domain;
model.time = [[YH_DNSUtils sharedInstance] stringFromDate:[NSDate date]];
NSInteger t = 60;
for (YH_DNSIpModel* tempIp in dnsPack.dns) {
YH_DNSIpModel* ipModel = [[YH_DNSIpModel alloc]init];
ipModel.ip = tempIp.ip;
ipModel.ttl = tempIp.ttl;
ipModel.finally_update_time = [[YH_DNSUtils sharedInstance] stringFromDate:[NSDate date]];
[model.ipModelArray addObject:ipModel];
if ([ipModel.ttl intValue] < t) {
t = [ipModel.ttl intValue];
}
}
model.ttl = [NSString stringWithFormat:@"%zd", t];
if (model.ipModelArray != nil && model.ipModelArray.count > 0) {
[self addMemoryCache:model.domain model:model];
}
return model;
}
}
- (YH_DNSIpModel *)findIpModel:(YH_DNSIpModel *)ipModel inArray:(NSArray *)array {
for (YH_DNSIpModel* modelItem in array) {
if ([modelItem.ip isEqualToString:ipModel.ip]) {
return modelItem;
}
}
return nil;
}
- (void)clear {
@synchronized(self) {
[self clearMemoryCache];
}
}
- (void)clearDB{
}
- (void)clearMemoryCache {
@synchronized(self) {
[self.memoryCacheDic removeAllModels];
}
}
@end