YH_DNSMemoryCache.m 3.88 KB
//
//  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