123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- //
- // ZYLNetworkCacheManager.m
- // NetworkLayerBuild
- //
- // Created by 翟玉磊 on 2017/9/1.
- // Copyright © 2017年 翟玉磊. All rights reserved.
- //
- #import "ZYLNetworkCacheManager.h"
- #pragma mark - ZYLNetworkCache
- @interface ZYLNetworkCache ()
- @property (nonatomic, strong) id data;
- @property (nonatomic, assign) NSUInteger cacheTime;
- @property (nonatomic, assign) NSUInteger validTimeInterval;
- @end
- #define ValidTimeInterval 60
- @implementation ZYLNetworkCache
- + (instancetype)cacheWithData:(id)data {
- return [self cacheWithData:data vaildTimeInterval:ValidTimeInterval];
- }
- + (instancetype)cacheWithData:(id)data vaildTimeInterval:(NSUInteger)interterval {
-
- ZYLNetworkCache *cache = [ZYLNetworkCache new];
- cache.data = data;
- cache.cacheTime = [[NSDate date] timeIntervalSince1970];
- cache.validTimeInterval = interterval > 0 ? interterval : ValidTimeInterval;
- return cache;
- }
- - (BOOL)isValid {
- if (self.data) {
- return [[NSDate date] timeIntervalSince1970] - self.cacheTime < self.validTimeInterval;
- }
- return NO;
- }
- @end
- #pragma mark - ZYLNetworkCacheManager
- @interface ZYLNetworkCacheManager ()
- @property (nonatomic, strong) NSCache *cache;
- @end
- @implementation ZYLNetworkCacheManager
- + (instancetype)sharedManager {
- static ZYLNetworkCacheManager *sharedManager;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
-
- sharedManager = [[super allocWithZone:NULL] init];
- });
- return sharedManager;
- }
- + (instancetype)allocWithZone:(struct _NSZone *)zone {
- return [self sharedManager];
- }
- - (void)configuration {
-
- self.cache = [NSCache new];
- self.cache.totalCostLimit = 1024 * 1024 * 20;
- }
- #pragma mark - Interface
- - (void)setObject:(ZYLNetworkCache *)object forKey:(id)key {
- [self.cache setObject:object forKey:key];
- }
- - (void)removeObjectForKey:(id)key {
- [self.cache removeObjectForKey:key];
- }
- - (ZYLNetworkCache *)objectForKey:(id)key {
- return [self.cache objectForKey:key];
- }
- @end
|