Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merged gist:243496 (EGOCache+Plist). Rolled EGOCache+Plist category i…

…nto main EGOCache class
  • Loading branch information...
commit dda4c8bc35ef84b9b55de37cf06d18b692c3ef7a 1 parent c3fefe2
@shnhrrsn shnhrrsn authored
Showing with 30 additions and 45 deletions.
  1. +0 −9 EGOCache+Plist.h
  2. +0 −35 EGOCache+Plist.m
  3. +4 −0 EGOCache.h
  4. +26 −1 EGOCache.m
View
9 EGOCache+Plist.h
@@ -1,9 +0,0 @@
-#import "EGOCache.h"
-
-@interface EGOCache (Plist)
-
-- (NSData*)plistForKey:(NSString*)key;
-- (void)setPlist:(id)plistObject forKey:(NSString*)key;
-- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
-
-@end
View
35 EGOCache+Plist.m
@@ -1,35 +0,0 @@
-#import "EGOCache.h"
-#import "EGOCache+Plist.h"
-
-@implementation EGOCache (Plist)
-
-- (NSData*)plistForKey:(NSString*)key;
-{
- NSData *plistData = [self dataForKey:key];
- return [NSPropertyListSerialization
- propertyListFromData:plistData
- mutabilityOption:NSPropertyListImmutable
- format:nil
- errorDescription:nil];
-}
-
-- (void)setPlist:(id)plistObject forKey:(NSString*)key;
-{
- [self setPlist:plistObject forKey:key withTimeoutInterval:60 * 60 * 24];
-}
-
-// XML format would produce cache files that are human-readable but at the
-// expense of performance, so I felt binary format was best.
-
-- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
-{
- NSString *errorString;
- NSData *plistData = [NSPropertyListSerialization
- dataFromPropertyList:plistObject
- format:NSPropertyListBinaryFormat_v1_0
- errorDescription:&errorString];
-
- [self setData:plistData forKey:key withTimeoutInterval:timeoutInterval];
-}
-
-@end
View
4 EGOCache.h
@@ -45,5 +45,9 @@
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
#endif
+- (NSData*)plistForKey:(NSString*)key;
+- (void)setPlist:(id)plistObject forKey:(NSString*)key; // withTimeoutInterval:defaultTimeoutInterval
+- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
+
@property(nonatomic,assign) NSTimeInterval defaultTimeoutInterval; // Default is 1 day
@end
View
27 EGOCache.m
@@ -217,7 +217,7 @@ - (NSImage*)imageForKey:(NSString*)key {
}
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key {
- [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeInterval];
+ [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval];
}
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
@@ -228,6 +228,31 @@ - (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NS
#endif
#pragma mark -
+#pragma mark Property List methods
+
+- (NSData*)plistForKey:(NSString*)key; {
+ NSData* plistData = [self dataForKey:key];
+
+ return [NSPropertyListSerialization propertyListFromData:plistData
+ mutabilityOption:NSPropertyListImmutable
+ format:nil
+ errorDescription:nil];
+}
+
+- (void)setPlist:(id)plistObject forKey:(NSString*)key; {
+ [self setPlist:plistObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval];
+}
+
+- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; {
+ // Binary plists are used over XML for better performance
+ NSData* plistData = [NSPropertyListSerialization dataFromPropertyList:plistObject
+ format:NSPropertyListBinaryFormat_v1_0
+ errorDescription:NULL];
+
+ [self setData:plistData forKey:key withTimeoutInterval:timeoutInterval];
+}
+
+#pragma mark -
#pragma mark Disk writing operations
- (void)performDiskWriteOperation:(NSInvocation *)invoction {

1 comment on commit dda4c8b

@lukeredpath

One thing I didn't bother with was error handling but it may be worth looking at doing something (you can pass a pointer to an NSString into errorDescription).

Please sign in to comment.
Something went wrong with that request. Please try again.