Cocoa touch 使用NSCache实现缓存过期
我正在使用NSCache在我的应用程序中实现缓存。我想给它添加过期时间,这样它会在一段时间后获得新数据。有哪些选择?最好的方法是什么 我应该在访问缓存时查看时间戳并使其失效吗?缓存是否应该使用固定的间隔计时器自动使自身失效 缓存是否应该通过使用固定的 间隔计时器 这将是一个糟糕的解决方案,因为您可能会在计时器启动前几秒钟添加一些内容。有效期应根据具体物品的使用年限确定。(当然,可以使用计时器有条件地使项目无效;请参阅此答案上的注释。) 这里有一个例子。我考虑过子类化Cocoa touch 使用NSCache实现缓存过期,cocoa-touch,caching,Cocoa Touch,Caching,我正在使用NSCache在我的应用程序中实现缓存。我想给它添加过期时间,这样它会在一段时间后获得新数据。有哪些选择?最好的方法是什么 我应该在访问缓存时查看时间戳并使其失效吗?缓存是否应该使用固定的间隔计时器自动使自身失效 缓存是否应该通过使用固定的 间隔计时器 这将是一个糟糕的解决方案,因为您可能会在计时器启动前几秒钟添加一些内容。有效期应根据具体物品的使用年限确定。(当然,可以使用计时器有条件地使项目无效;请参阅此答案上的注释。) 这里有一个例子。我考虑过子类化NSCache,但认为使用组合
NSCache
,但认为使用组合更简单
接口
另一种解决方案是在设置对象时设置过期时间,并与对象的过期时间进行比较 例如: 用法
#import "PTCache.h"
NSInteger const PROFILE_CACHE_EXPIRE = 3600;
- (void) cacheSomething: (id) obj
forKey: (NSString*) key
{
[PTCache sharedInstance] setObject: obj
forKey: key
expire: PROFILE_CACHE_EXPIRE
];
}
#import <Foundation/Foundation.h>
@interface PTCache : NSCache
+ (PTCache *) sharedInstance;
- (void) setObject: (id) obj
forKey: (NSString *) key
expire: (NSInteger) seconds;
- (id) objectForKey: (NSString *) key;
@end
接口
#import "PTCache.h"
NSInteger const PROFILE_CACHE_EXPIRE = 3600;
- (void) cacheSomething: (id) obj
forKey: (NSString*) key
{
[PTCache sharedInstance] setObject: obj
forKey: key
expire: PROFILE_CACHE_EXPIRE
];
}
#import <Foundation/Foundation.h>
@interface PTCache : NSCache
+ (PTCache *) sharedInstance;
- (void) setObject: (id) obj
forKey: (NSString *) key
expire: (NSInteger) seconds;
- (id) objectForKey: (NSString *) key;
@end
这看起来不像是驱逐政策。如果资源过期,则仅在尝试访问时才会将其逐出。实例可能永远存在,即使已过期。@RaduSimionescu True。如果需要的话,您可以添加一个计时器来定期逐出过期的对象。(或者,如果你想更严格的话,每个到期时间都有一个计时器。)但是,由于
NSCache
会在内存压力下逐出对象,所以可以让它们无限期地驻留。
#import <Foundation/Foundation.h>
@interface PTCache : NSCache
+ (PTCache *) sharedInstance;
- (void) setObject: (id) obj
forKey: (NSString *) key
expire: (NSInteger) seconds;
- (id) objectForKey: (NSString *) key;
@end
#import "PTCache.h"
@implementation PTCache
{
NSMutableArray * expireKeys;
}
+ (PTCache *) sharedInstance
{
static dispatch_once_t predicate = 0;
__strong static id sharedObject = nil;
dispatch_once(&predicate, ^{
sharedObject = [[self alloc] init];
});
return sharedObject;
}
- (id) init
{
if ( self = [super init])
{
expireKeys = [[NSMutableArray alloc] init];
}
return self;
}
/**
* Get Object
*
* @param NSString * key
* @return id obj
*
**/
- (id) objectForKey: (NSString *) key
{
id obj = [super objectForKey: key];
if( obj == nil)
{
return nil;
}
BOOL expired = [self hasExpired: key];
if( expired)
{
[super removeObjectForKey: key];
return nil;
}
return obj;
}
/**
* Set Object
*
* @param id obj
* @param NSString * key
* @param NSInteger seconds
*
*/
- (void) setObject: (id) obj
forKey: (NSString *) key
expire: (NSInteger) seconds
{
[super setObject: obj forKey: key];
[self updateExpireKey: key expire: seconds];
}
/**
* Update Expire Time for Key and Seconds to Expire
*
* @param NSString * key
* @param NSInteger seconds
*
**/
- (void) updateExpireKey: (NSString *) key
expire: (NSInteger) seconds
__block NSInteger index = -1;
[expireKeys enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
if([obj[@"key"] isEqualToString: key])
{
index = idx;
*stop = YES;
return;
}
}];
NSNumber * expires = [NSNumber numberWithFloat: ([[NSDate date] timeIntervalSince1970] + seconds)];
if( index > -1)
{
[[expireKeys objectAtIndex: index] setObject: expires forKey: key];
}
else
{
NSMutableDictionary * element = [[NSMutableDictionary alloc] init];
[element setObject: key forKey: @"key"];
[element setObject: expires forKey: @"expire"];
[expireKeys addObject: element];
}
}
/**
* Has Expired for Key
*
**/
- (BOOL) hasExpired: (NSString *) key
{
NSNumber * expiredObj = [self getExpireTime: key];
NSDate * current = [NSDate date];
NSDate * expireDate = [NSDate dateWithTimeIntervalSince1970: [expiredObj doubleValue]];
return [current compare: expireDate] == NSOrderedDescending;
}
/**
* Get Expire Time
*
* @param NSString * key
* @param NSInteger
*
**/
- (NSNumber *) getExpireTime: (NSString *) key
{
__block NSNumber * expire = nil;
[expireKeys enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
if([obj[@"key"] isEqualToString: key])
{
expire = obj[@"expire"];
*stop = YES;
return;
}
}];
return expire;
}
@end