用单例来临时保存缓存数据
- 作者: 内涵的老男人
- 来源: 51数据库
- 2022-08-17
//进行缓存的步骤:
//1.创建单例(返回自己或者id)
static DYPDataCache *cache = nil;
+(id)sharedInstance{
//保证线程安全
@synchronized(self){
if (cache == nil) {
//cache = [[DYPDataCache alloc]init];
//通过[self class]方法可以获取到当前类的类名
cache = [[[self class]alloc]init];
//初始化有效时间
cache.validTime = 20*60;
}
}
return cache;
}
+(id)allocWithZone:(struct _NSZone *)zone{
if (cache == nil) {
cache = [super allocWithZone:zone];
}
return cache;
}
推荐阅读
