YH_DNSMemoryCache.m
3.88 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
//
// YH_DNSMemoryCache.m
// YH_Mall
//
// Created by jhsonzhi on 16/7/18.
// Copyright © 2016年 YOHO. All rights reserved.
//
#import "YH_DNSMemoryCache.h"
#import "YH_DNSDomainModel.h"
#import "YH_DNSIpModel.h"
#import "YH_DNSUtils.h"
@interface YH_DNSMemoryCache ()
@property (strong, nonatomic) NSMutableDictionary *memoryDic;
@end
@implementation YH_DNSMemoryCache
+ (instancetype)sharedInstance {
static YH_DNSMemoryCache* sharedInstance;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedInstance = [[YH_DNSMemoryCache alloc]init];
});
return sharedInstance;
}
- (instancetype)init{
if (self = [super init]) {
}
return self;
}
-(NSMutableDictionary *)memoryDic{
if (!_memoryDic) {
_memoryDic = [[NSMutableDictionary alloc] init];
}
return _memoryDic;
}
- (YH_DNSDomainModel *)getModelByKeyUrl:(NSString *)keyUrl {
@synchronized(self) {
return self.memoryDic[keyUrl];
}
}
- (void)removeAllModels {
@synchronized(self) {
[self.memoryDic removeAllObjects];
}
}
- (NSArray *)getAllModels {
@synchronized(self) {
return [self.memoryDic allValues];
}
}
- (void)addModel:(YH_DNSDomainModel *)model keyHost:(NSString *)hostName {
@synchronized(self) {
YH_DNSDomainModel* existModel = self.memoryDic[hostName];
if (existModel) {
[self.memoryDic removeObjectForKey:hostName];
}
[self.memoryDic setObject:model forKey:hostName];
}
}
//本函数只用于更新从本地dns获取的ip, 从httpserver 获取的ip 不应该用此函数更新。
//- (void)updateModel:(YH_DNSDomainModel *)model keyHost:(NSString *)hostName
//{
// if (YHDNS_isStrEmpty(hostName)) {
// return;
// }
//
// @synchronized(self) {
// YH_DNSDomainModel* existModel = self.memoryDic[hostName];
// if (existModel) {
// existModel.domain = model.domain;
// existModel.ttl = model.ttl;
// existModel.time = model.time;
//
// NSMutableArray *newIpArray= [NSMutableArray array];
//
// //更新在新老Model里的ip,不更新测速数据;。删除存在着老model里的,但是不在新Model里的ip。
// for (YH_DNSIpModel *ipModel in existModel.ipModelArray) {
// YH_DNSIpModel *newIpModel = [self findIp:ipModel.ip inIPArray:model.ipModelArray];
// if (newIpModel) {
// ipModel.domainID = newIpModel.domainID;
// ipModel.ttl = newIpModel.ttl;
// ipModel.finally_update_time = newIpModel.finally_update_time;
// [newIpArray addObject:ipModel];
// }
// }
//
// //把新Model里存在 但不再老model里的ip 加进来。
// for (YH_DNSIpModel *ipModel in model.ipModelArray) {
// YH_DNSIpModel *newIpModel = [self findIp:ipModel.ip inIPArray:newIpArray];
// if (newIpModel == nil) {
// [newIpArray addObject:ipModel];
// }
// }
//
// existModel.ipModelArray = newIpArray;
// }
// else {
// [self.memoryDic setObject:model forKey:hostName];
// }
// }
//}
- (YH_DNSIpModel *)findIp:(NSString *)ip inIPArray:(NSArray *)ipArray
{
for (YH_DNSIpModel* ipModel in ipArray) {
if ([ipModel.ip isEqualToString:ip]) {
return ipModel;
}
}
return nil;
}
- (NSArray *)getExpireDnsCache
{
@synchronized(self) {
NSMutableArray *domainList = [NSMutableArray array];
for (YH_DNSDomainModel *tempModel in [self.memoryDic allValues]) {
if ([YH_DNSUtils isDomainModelExpired:tempModel]) {
[domainList addObject:tempModel];
}
}
return domainList;
}
}
@end