单军华
2018-07-11 7b02207537d35bfa1714bf8beafc921f717d100a
screendisplay/screendisplay/Classes/Network/PPNetworkHelper/PPNetworkCache.m
New file
@@ -0,0 +1,50 @@
//
//  PPNetworkCache.m
//  PPNetworkHelper
//
//  Created by AndyPang on 16/8/12.
//  Copyright © 2016年 AndyPang. All rights reserved.
//
#import "PPNetworkCache.h"
#import "YYCache.h"
static NSString *const kPPNetworkResponseCache = @"kPPNetworkResponseCache";
@implementation PPNetworkCache
static YYCache *_dataCache;
+ (void)initialize {
    _dataCache = [YYCache cacheWithName:kPPNetworkResponseCache];
}
+ (void)setHttpCache:(id)httpData URL:(NSString *)URL parameters:(NSDictionary *)parameters {
    NSString *cacheKey = [self cacheKeyWithURL:URL parameters:parameters];
    //异步缓存,不会阻塞主线程
    [_dataCache setObject:httpData forKey:cacheKey withBlock:nil];
}
+ (id)httpCacheForURL:(NSString *)URL parameters:(NSDictionary *)parameters {
    NSString *cacheKey = [self cacheKeyWithURL:URL parameters:parameters];
    return [_dataCache objectForKey:cacheKey];
}
+ (NSInteger)getAllHttpCacheSize {
    return [_dataCache.diskCache totalCost];
}
+ (void)removeAllHttpCache {
    [_dataCache.diskCache removeAllObjects];
}
+ (NSString *)cacheKeyWithURL:(NSString *)URL parameters:(NSDictionary *)parameters {
    if(!parameters || parameters.count == 0){return URL;};
    // 将参数字典转换成字符串
    NSData *stringData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
    NSString *paraString = [[NSString alloc] initWithData:stringData encoding:NSUTF8StringEncoding];
    return [NSString stringWithFormat:@"%@%@",URL,paraString];
}
@end