summaryrefslogtreecommitdiffstats
path: root/Classes/HJMOBigFileCache.m
blob: f1a8725f5cc5dacc35f01141753662edf44ba4e7 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
//  HJMOBigFileCache.m
//  hjlib
//
//  Copyright Hunter and Johnson 2009, 2010, 2011
//  HJCache may be used freely in any iOS or Mac application free or commercial.
//  May be redistributed as source code only if all the original files are included.
//  See http://www.markj.net/hjcache-iphone-image-cache/


#import "HJMOBigFileCache.h"


NSString* HJMOBigFileCache_trimDirKey = @"HJMOBigFileCacheTrimDir";
NSString* HJMOBigFileCache_trimDateKey = @"HJMOBigFileCacheTrimDate";


@implementation HJMOBigFileCache

@synthesize lastTrimDate;
@synthesize lastTrimDirNum;

- (void) dealloc {
	[lastTrimDirNum release];
	[lastTrimDate release];	
	[super dealloc];
}

-(void)createCacheDirsIfNeeded {
	[super createCacheDirsIfNeeded];
	for (int i=0; i<=9; i++) {
		NSString* path = [NSString stringWithFormat:@"%@%i",readyPath,i];
		[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
	}
}

-(NSString*)subdirForFilename:(NSString*)filename {
	unichar c1 = [filename characterAtIndex:[filename length]-1];
	unichar c2 = [filename characterAtIndex:[filename length]-2];
	int hashpath = (c1+c2)%10;
	return [NSString stringWithFormat:@"%i",hashpath];
}

-(NSString*)readyFilePathForOid:(id)oid {
	NSString* filename = [self filenameForOid:oid];
	NSString* subdir = [self subdirForFilename:filename];
	NSString* path;
	if (subdir==nil) {
		path = [readyPath stringByAppendingString:filename];
	} else {
		path = [NSString stringWithFormat:@"%@%@/%@",readyPath,subdir,filename];
	}
	return path;
}


-(void)updateLastTrimState {
	[[NSUserDefaults standardUserDefaults] setObject:self.lastTrimDirNum forKey:HJMOBigFileCache_trimDirKey];
	[[NSUserDefaults standardUserDefaults] setObject:self.lastTrimDate forKey:HJMOBigFileCache_trimDateKey];
	[[NSUserDefaults standardUserDefaults] synchronize];
}


/*
 This only trims 1/10th of the cache at a time, and does this no more than once every 1hrs.
 */
-(void)trimCache {
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	
	self.lastTrimDirNum = (NSNumber*) [[NSUserDefaults standardUserDefaults] objectForKey:HJMOBigFileCache_trimDirKey];
	self.lastTrimDate = (NSDate*) [[NSUserDefaults standardUserDefaults] objectForKey:HJMOBigFileCache_trimDateKey];
	NSDate* now = [[[NSDate alloc] init] autorelease];
	
	if (lastTrimDate==nil || [now timeIntervalSinceDate:lastTrimDate]>60*60*1) {
		if (lastTrimDirNum==nil) {
			self.lastTrimDirNum = [NSNumber numberWithInt:-1];
		}
		int num = [lastTrimDirNum intValue];
		num++; 
		if (num>=10) { num=0; }
		NSString* nextTrimPath = [NSString stringWithFormat:@"%@%i/",readyPath,num];
		[self trimCacheDir:nextTrimPath];
		
		self.lastTrimDate = now;
		self.lastTrimDirNum = [NSNumber numberWithInt:num];
		[self updateLastTrimState];
	}
	[self performSelectorOnMainThread:@selector(updateLastTrimState) withObject:nil waitUntilDone:YES];
	
	[pool release];
}


@end