From 49412759b835519b4db6760557a00bbf915d7bf0 Mon Sep 17 00:00:00 2001 From: Ludovic Marcotte Date: Wed, 12 Nov 2008 03:30:13 +0000 Subject: [PATCH] See changelog Monotone-Parent: 9641487ae73aa57f2e0f1a17a9b0f485143a6423 Monotone-Revision: afb54e0e3680201763787e929aeb5bdb376b0543 Monotone-Author: ludovic@Sophos.ca Monotone-Date: 2008-11-12T03:30:13 Monotone-Branch: ca.inverse.sogo --- ChangeLog | 2 + SoObjects/SOGo/SOGoCache.h | 9 +++ SoObjects/SOGo/SOGoCache.m | 128 +++++++++++++++++++----------- SoObjects/SOGo/SOGoUser.h | 8 -- SoObjects/SOGo/SOGoUser.m | 62 ++------------- SoObjects/SOGo/SOGoUserDefaults.m | 6 +- 6 files changed, 102 insertions(+), 113 deletions(-) diff --git a/ChangeLog b/ChangeLog index 04a292b8e..11128739b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ * Fixed a couple of issues in the new pref caching subsystem. + * Cleaned up the caching code and activated + distributed notifications by default. 2008-10-29 Francis Lachapelle diff --git a/SoObjects/SOGo/SOGoCache.h b/SoObjects/SOGo/SOGoCache.h index 0403078d5..c195adab4 100644 --- a/SoObjects/SOGo/SOGoCache.h +++ b/SoObjects/SOGo/SOGoCache.h @@ -29,9 +29,11 @@ @class NSMutableDictionary; @class NSString; @class NSTimer; +@class NSUserDefaults; @class SOGoObject; @class SOGoUser; +@class SOGoUserDefaults; @interface SOGoCache : NSObject { @@ -52,6 +54,13 @@ - (void) registerUser: (SOGoUser *) user; - (id) userNamed: (NSString *) name; ++ (void) setCachedUserDefaults: (SOGoUserDefaults *) theDefaults + user: (NSString *) login; ++ (NSDictionary *) cachedUserDefaults; ++ (void) setCachedUserSettings: (SOGoUserDefaults *) theSettings + user: (NSString *) login; ++ (NSDictionary *) cachedUserSettings; + @end #endif /* SOGOCACHE_H */ diff --git a/SoObjects/SOGo/SOGoCache.m b/SoObjects/SOGo/SOGoCache.m index a091130cd..e10da72e5 100644 --- a/SoObjects/SOGo/SOGoCache.m +++ b/SoObjects/SOGo/SOGoCache.m @@ -34,6 +34,7 @@ #import "SOGoObject.h" #import "SOGoUser.h" +#import "SOGoUserDefaults.h" #import "SOGoCache.h" @@ -45,6 +46,9 @@ static NSTimeInterval cleanupInterval = 1800; static NSMutableDictionary *cache = nil; static NSMutableDictionary *users = nil; +static NSMutableDictionary *s_userDefaults = nil; +static NSMutableDictionary *s_userSettings = nil; + static SOGoCache *sharedCache = nil; @implementation SOGoCache @@ -74,11 +78,13 @@ static SOGoCache *sharedCache = nil; { NSString *cleanupSetting; - cache = [NSMutableDictionary new]; - users = [NSMutableDictionary new]; + cache = [[NSMutableDictionary alloc] init]; + users = [[NSMutableDictionary alloc] init]; + + s_userDefaults = [[NSMutableDictionary alloc] init]; + s_userSettings = [[NSMutableDictionary alloc] init]; // We register ourself for notifications -#if 0 [[NSDistributedNotificationCenter defaultCenter] addObserver: self selector: @selector(_userDefaultsHaveChanged:) @@ -90,7 +96,6 @@ static SOGoCache *sharedCache = nil; selector: @selector(_userSettingsHaveChanged:) name: @"SOGoUserSettingsHaveChanged" object: nil]; -#endif // We fire our timer that will cleanup cache entries cleanupSetting = [[NSUserDefaults standardUserDefaults] @@ -113,7 +118,6 @@ static SOGoCache *sharedCache = nil; - (void) dealloc { -#if 0 [[NSDistributedNotificationCenter defaultCenter] removeObserver: self name: @"SOGoUserDefaultsHaveChanged" @@ -123,7 +127,6 @@ static SOGoCache *sharedCache = nil; removeObserver: self name: @"SOGoUserSettingsHaveChanged" object: nil]; -#endif [cache release]; [users release]; @@ -200,37 +203,68 @@ static SOGoCache *sharedCache = nil; return [users objectForKey: name]; } ++ (NSDictionary *) cachedUserDefaults +{ + return s_userDefaults; +} + +/* defaults */ ++ (void) setCachedUserDefaults: (SOGoUserDefaults *) theDefaults + user: (NSString *) login +{ + NSDate *cleanupDate; + + cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]; + [s_userDefaults setObject: [NSDictionary dictionaryWithObjectsAndKeys: + theDefaults, @"dictionary", + cleanupDate, @"cleanupDate", nil] + forKey: login]; +} + ++ (NSDictionary *) cachedUserSettings +{ + return s_userSettings; +} + ++ (void) setCachedUserSettings: (SOGoUserDefaults *) theSettings + user: (NSString *) login +{ + NSDate *cleanupDate; + + cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]; + [s_userSettings setObject: [NSDictionary dictionaryWithObjectsAndKeys: + theSettings, @"dictionary", + cleanupDate, @"cleanupDate", nil] + forKey: login]; +} + - (void) _userDefaultsHaveChanged: (NSNotification *) theNotification { - NSMutableDictionary *d; - NSString *user; + SOGoUserDefaults *d; - d = [[NSMutableDictionary alloc] init]; - [d addEntriesFromDictionary: [theNotification userInfo]]; + d = [[SOGoUserDefaults alloc] initWithTableURL: [[theNotification userInfo] objectForKey: @"url"] + uid: [[theNotification userInfo] objectForKey: @"uid"] + fieldName: [[theNotification userInfo] objectForKey: @"fieldName"]]; + [d setValues: [[theNotification userInfo] objectForKey: @"values"]]; - user = [d objectForKey: @"uid"]; - [user retain]; - [d removeObjectForKey: @"uid"]; - - [SOGoUser setUserDefaultsFromDictionary: d user: user]; - [user release]; + + [SOGoCache setCachedUserDefaults: d + user: [[theNotification userInfo] objectForKey: @"uid"]]; [d release]; } - (void) _userSettingsHaveChanged: (NSNotification *) theNotification { - NSMutableDictionary *d; - NSString *user; + SOGoUserDefaults *d; - d = [[NSMutableDictionary alloc] init]; - [d addEntriesFromDictionary: [theNotification userInfo]]; + d = [[SOGoUserDefaults alloc] initWithTableURL: [[theNotification userInfo] objectForKey: @"url"] + uid: [[theNotification userInfo] objectForKey: @"uid"] + fieldName: [[theNotification userInfo] objectForKey: @"fieldName"]]; + [d setValues: [[theNotification userInfo] objectForKey: @"values"]]; - user = [d objectForKey: @"uid"]; - [user retain]; - [d removeObjectForKey: @"uid"]; - - [SOGoUser setUserSettingsFromDictionary: d user: user]; - [user release]; + + [SOGoCache setCachedUserSettings: d + user: [[theNotification userInfo] objectForKey: @"uid"]]; [d release]; } @@ -246,37 +280,37 @@ static SOGoCache *sharedCache = nil; now = [NSDate date]; // We cleanup the user defaults - userIDs = [[[SOGoUser userDefaultsCache] allKeys] objectEnumerator]; + userIDs = [[s_userDefaults allKeys] objectEnumerator]; count = 0; while ((currentID = [userIDs nextObject])) { - currentEntry = [[SOGoUser userDefaultsCache] objectForKey: currentID]; + currentEntry = [s_userDefaults objectForKey: currentID]; if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now) { - [SOGoUser setUserDefaultsFromDictionary: nil user: currentID]; - count++; - } - } - - if (count) - [self logWithFormat: @"cleaned %d users records from user defaults cache", count]; - - // We cleanup the user settings - userIDs = [[[SOGoUser userSettingsCache] allKeys] objectEnumerator]; - count = 0; - while ((currentID = [userIDs nextObject])) - { - currentEntry = [[SOGoUser userSettingsCache] objectForKey: currentID]; - - if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now) - { - [SOGoUser setUserSettingsFromDictionary: nil user: currentID]; + [s_userDefaults removeObjectForKey: currentID]; count++; } } - if (count) + if (count) + [self logWithFormat: @"cleaned %d users records from user defaults cache", count]; + + // We cleanup the user settings + userIDs = [[s_userSettings allKeys] objectEnumerator]; + count = 0; + while ((currentID = [userIDs nextObject])) + { + currentEntry = [s_userSettings objectForKey: currentID]; + + if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now) + { + [s_userSettings removeObjectForKey: currentID]; + count++; + } + } + + if (count) [self logWithFormat: @"cleaned %d users records from user settings cache", count]; } diff --git a/SoObjects/SOGo/SOGoUser.h b/SoObjects/SOGo/SOGoUser.h index b180af1ca..cbefa92be 100644 --- a/SoObjects/SOGo/SOGoUser.h +++ b/SoObjects/SOGo/SOGoUser.h @@ -109,15 +109,7 @@ extern NSString *SOGoWeekStartFirstFullWeek; // - (NSDictionary *) additionalIMAP4AccountsAndEMails; /* defaults */ - -+ (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary - user: (NSString *) login; -+ (NSDictionary *) userDefaultsCache; - (NSUserDefaults *) userDefaults; - -+ (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary - user: (NSString *) login; -+ (NSDictionary *) userSettingsCache; - (NSUserDefaults *) userSettings; - (NSString *) language; diff --git a/SoObjects/SOGo/SOGoUser.m b/SoObjects/SOGo/SOGoUser.m index 1e0f0376a..dd0c84843 100644 --- a/SoObjects/SOGo/SOGoUser.m +++ b/SoObjects/SOGo/SOGoUser.m @@ -46,9 +46,6 @@ #import "SOGoUser.h" -static NSMutableDictionary *userDefaults; -static NSMutableDictionary *userSettings; - static NSTimeZone *serverTimeZone = nil; static NSString *fallbackIMAP4Server = nil; static BOOL fallbackIsConfigured = NO; @@ -195,9 +192,6 @@ _timeValue (NSString *key) // acceptAnyUser = ([[ud stringForKey: @"SOGoAuthentificationMethod"] // isEqualToString: @"bypass"]); - - userDefaults = [[NSMutableDictionary alloc] init]; - userSettings = [[NSMutableDictionary alloc] init]; } + (NSString *) language @@ -471,34 +465,12 @@ _timeValue (NSString *key) return dateFormatter; } -/* defaults */ -+ (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary - user: (NSString *) login -{ - if (!theDictionary) - [userDefaults removeObjectForKey: login]; - else - { - NSDate *cleanupDate; - - cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]; - [userDefaults setObject: [NSDictionary dictionaryWithObjectsAndKeys: - theDictionary, @"dictionary", - cleanupDate, @"cleanupDate", nil] - forKey: login]; - } -} - -+ (NSDictionary *) userDefaultsCache -{ - return userDefaults; -} - - (NSUserDefaults *) userDefaults { id o; - o = [userDefaults objectForKey: login]; + o = [[SOGoCache cachedUserDefaults] objectForKey: login]; + if (!o) { o = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL @@ -514,7 +486,8 @@ _timeValue (NSString *key) if (![[o stringForKey: @"MessageCheck"] length]) [o setObject: defaultMessageCheck forKey: @"MessageCheck"]; - [SOGoUser setUserDefaultsFromDictionary: o user: login]; + [SOGoCache setCachedUserDefaults: o user: login]; + [o release]; return o; } @@ -522,40 +495,19 @@ _timeValue (NSString *key) return [o objectForKey: @"dictionary"]; } -+ (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary - user: (NSString *) login -{ - if (!theDictionary) - [userSettings removeObjectForKey: login]; - else - { - NSDate *cleanupDate; - - cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]]; - [userSettings setObject: [NSDictionary dictionaryWithObjectsAndKeys: - theDictionary, @"dictionary", - cleanupDate, @"cleanupDate", nil] - forKey: login]; - } -} - -+ (NSDictionary *) userSettingsCache -{ - return userSettings; -} - - (NSUserDefaults *) userSettings { id o; - o = [userSettings objectForKey: login]; + o = [[SOGoCache cachedUserSettings] objectForKey: login]; if (!o) { o = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL uid: login fieldName: @"c_settings"]; - [SOGoUser setUserSettingsFromDictionary: o user: login]; + [SOGoCache setCachedUserSettings: o user: login]; + [o release]; return o; } diff --git a/SoObjects/SOGo/SOGoUserDefaults.m b/SoObjects/SOGo/SOGoUserDefaults.m index f9dd3008c..e23133532 100644 --- a/SoObjects/SOGo/SOGoUserDefaults.m +++ b/SoObjects/SOGo/SOGoUserDefaults.m @@ -272,16 +272,16 @@ static NSString *uidColumnName = @"c_uid"; NSMutableDictionary *d; d = [[NSMutableDictionary alloc] init]; - [d addEntriesFromDictionary: values]; + [d setObject: values forKey: @"values"]; [d setObject: uid forKey: @"uid"]; + [d setObject: fieldName forKey: @"fieldName"]; + [d setObject: url forKey: @"url"]; #warning reenable when the code to use the SOGoCache is finished -#if 0 [[NSDistributedNotificationCenter defaultCenter] postNotificationName: ([fieldName isEqualToString: @"c_defaults"] ? @"SOGoUserDefaultsHaveChanged" : @"SOGoUserSettingsHaveChanged") object: nil userInfo: d]; -#endif [d release]; if ([[channel adaptorContext] hasOpenTransaction])