52 lines
1.5 KiB
Objective-C
Executable File
52 lines
1.5 KiB
Objective-C
Executable File
//
|
|
// MLNetworkCache.m
|
|
// HuiBao
|
|
//
|
|
// Created by 玛丽 on 2017/11/22.
|
|
// Copyright © 2017年 玛丽. All rights reserved.
|
|
//
|
|
|
|
#import "MLNetworkCache.h"
|
|
|
|
#import <YYCache.h>
|
|
|
|
static NSString *const kMLNetworkResponseCache = @"kMLNetworkResponseCache";
|
|
|
|
@implementation MLNetworkCache
|
|
static YYCache *_dataCache;
|
|
|
|
+ (void)initialize {
|
|
_dataCache = [YYCache cacheWithName:kMLNetworkResponseCache];
|
|
}
|
|
|
|
+ (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
|