Permalink
Browse files

Added convience methods for fetching an array or dictionary from the …

…cache.
  • Loading branch information...
1 parent bb91749 commit 29f8e8f127db9392b40d6232b25e6b28b7817de0 @Inferis committed Jan 25, 2011
Showing with 112 additions and 0 deletions.
  1. +22 −0 EGOCache+Convenience.h
  2. +90 −0 EGOCache+Convenience.m
View
@@ -0,0 +1,22 @@
+//
+// EGOCache+Convenience.h
+//
+// Created by Tom Adriaenssen on 25/01/11.
+//
+
+#import <Foundation/Foundation.h>
+#import "EGOCache.h"
+
+@interface EGOCache (Convenience) {
+
+}
+
+- (NSArray*)arrayForKey:(NSString*)key;
+- (void)setArray:(NSArray*)anArray forKey:(NSString*)key;
+- (void)setArray:(NSArray*)anArray forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
+
+- (NSDictionary*)dictionaryForKey:(NSString*)key;
+- (void)setDictionary:(NSDictionary*)aDictionary forKey:(NSString*)key;
+- (void)setDictionary:(NSDictionary*)aDictionary forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
+
+@end
View
@@ -0,0 +1,90 @@
+//
+// EGOCache+Convenience.m
+//
+// Created by Tom Adriaenssen on 25/01/11.
+//
+
+#import "EGOCache+Convenience.h"
+#import "EGOCache.h"
+
+@interface EGOCache (Convenience_Private)
+{
+}
+
+- (NSData*)encodeDictionary:(NSDictionary*)aDictionary;
+- (NSData*)encodeArray:(NSArray*)anArray;
+
+@end
+
+@implementation EGOCache (Convenience)
+
+
+#pragma mark -
+#pragma mark Array
+
+- (NSArray*)arrayForKey:(NSString*)key {
+ NSData* data = [self dataForKey:key];
+
+ NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
+ NSArray *result = [[unarchiver decodeObjectForKey:@"value"] retain];
+ [unarchiver finishDecoding];
+ [unarchiver release];
+ [data release];
+
+ return result;
+}
+
+- (void)setArray:(NSArray*)anArray forKey:(NSString*)key {
+ [self setData:[self encodeArray:anArray] forKey:key];
+}
+
+- (void)setArray:(NSArray*)anArray forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
+ [self setData:[self encodeArray:anArray] forKey:key withTimeoutInterval:timeoutInterval];
+}
+
+- (NSData*)encodeArray:(NSArray*)anArray {
+ NSMutableData *data = [[NSMutableData alloc] init];
+ NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
+ [archiver encodeObject:anArray forKey:@"value"];
+ [archiver finishEncoding];
+ [archiver release];
+
+ return [data autorelease];
+}
+
+#pragma mark -
+#pragma mark Dictionary
+
+- (NSDictionary*)dictionaryForKey:(NSString*)key {
+ NSData* data = [self dataForKey:key];
+
+ NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
+ NSDictionary *result = [[unarchiver decodeObjectForKey:@"value"] retain];
+ [unarchiver finishDecoding];
+ [unarchiver release];
+ [data release];
+
+ return result;
+}
+
+- (void)setDictionary:(NSDictionary*)aDictionary forKey:(NSString*)key {
+ [self setData:[self encodeDictionary:aDictionary] forKey:key];
+}
+
+- (void)setDictionary:(NSDictionary*)aDictionary forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
+ [self setData:[self encodeDictionary:aDictionary] forKey:key withTimeoutInterval:timeoutInterval];
+}
+
+- (NSData*)encodeDictionary:(NSDictionary*)aDictionary {
+ NSMutableData *data = [[NSMutableData alloc] init];
+ NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
+ [archiver encodeObject:aDictionary forKey:@"value"];
+ [archiver finishEncoding];
+ [archiver release];
+
+ return [data autorelease];
+}
+
+#pragma mark -
+
+@end

0 comments on commit 29f8e8f

Please sign in to comment.