GLD_CacheManager.m
1.65 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
//
// GLD_CacheManager.m
// GLD_Networking
//
// Created by yiyangkeji on 2017/6/27.
// Copyright © 2017年 yiyangkeji. All rights reserved.
//
#import "GLD_CacheManager.h"
@interface GLD_Cache ()
@property (nonatomic, assign)NSUInteger cacheTime; //缓存存入时间
@property (nonatomic, assign)NSUInteger validTime; //缓存有效时间
@property(nonatomic, strong)id data;
@end
#define ValidTimeInterval 60 //秒
@implementation GLD_Cache
+(instancetype)cacheWithData:(id)data validTime:(NSUInteger)validTime{
GLD_Cache *cache = [GLD_Cache new];
cache.cacheTime = [[NSDate date] timeIntervalSince1970];
cache.data = data;
cache.validTime = validTime > 0 ? validTime : ValidTimeInterval;
return cache;
}
-(BOOL)isValid{
if (self.data) {
return [[NSDate date] timeIntervalSince1970] - self.cacheTime < self.validTime;
}
return NO;
}
@end
@interface GLD_CacheManager ()
@property(nonatomic, strong)NSCache *cache;
@end
@implementation GLD_CacheManager
+ (instancetype)shareCacheManager{
static GLD_CacheManager *shareManager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareManager = [[GLD_CacheManager alloc]init];
[shareManager configuration];
});
return shareManager;
}
- (void)configuration{
self.cache = [[NSCache alloc]init];
self.cache.totalCostLimit = 1024 * 1024 * 20; //20 兆
}
- (void)setObjcet:(GLD_Cache *)object key:(id)key{
[self.cache setObject:object forKey:key];
}
- (void)removeObjectForKey:(id)key{
[self.cache removeObjectForKey:key];
}
- (GLD_Cache *)objectForKey:(id)key{
return [self.cache objectForKey:key];
}
@end