アプリにキャッシュを実装するために 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"
@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