mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-07-12 20:05:08 +00:00
Many improvements to the previous commit.
Monotone-Parent: b338f502fe70efea6d0d06d3f5a745163d1dbaf7 Monotone-Revision: e74038b8c915f22e294a5efbc419eef439f81d21 Monotone-Author: ludovic@Sophos.ca Monotone-Date: 2008-11-11T01:52:56 Monotone-Branch: ca.inverse.sogo
This commit is contained in:
@@ -35,11 +35,11 @@
|
||||
|
||||
@interface SOGoCache : NSObject
|
||||
{
|
||||
NSMutableDictionary *cache;
|
||||
NSMutableDictionary *users;
|
||||
NSTimer *_cleanupTimer;
|
||||
@private
|
||||
NSTimer *_cleanupTimer;
|
||||
}
|
||||
|
||||
+ (NSTimeInterval) cleanupInterval;
|
||||
+ (SOGoCache *) sharedCache;
|
||||
+ (void) killCache;
|
||||
|
||||
|
||||
+53
-20
@@ -38,13 +38,22 @@
|
||||
#import "SOGoCache.h"
|
||||
|
||||
// We define the default value for cleaning up cached
|
||||
// users' preferences. This value should be relatively high
|
||||
// to avoid useless database calls.
|
||||
// users' preferences. This value should be relatively
|
||||
// high to avoid useless database calls.
|
||||
static NSTimeInterval cleanupInterval = 1800;
|
||||
|
||||
static NSMutableDictionary *cache = nil;
|
||||
static NSMutableDictionary *users = nil;
|
||||
|
||||
static SOGoCache *sharedCache = nil;
|
||||
|
||||
@implementation SOGoCache
|
||||
|
||||
+ (NSTimeInterval) cleanupInterval
|
||||
{
|
||||
return cleanupInterval;
|
||||
}
|
||||
|
||||
+ (SOGoCache *) sharedCache
|
||||
{
|
||||
if (!sharedCache)
|
||||
@@ -55,8 +64,8 @@ static SOGoCache *sharedCache = nil;
|
||||
|
||||
+ (void) killCache
|
||||
{
|
||||
[sharedCache release];
|
||||
sharedCache = nil;
|
||||
[cache removeAllObjects];
|
||||
[users removeAllObjects];
|
||||
}
|
||||
|
||||
- (id) init
|
||||
@@ -102,6 +111,16 @@ static SOGoCache *sharedCache = nil;
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
[[NSDistributedNotificationCenter defaultCenter]
|
||||
removeObserver: self
|
||||
name: @"SOGoUserDefaultsHaveChanged"
|
||||
object: nil];
|
||||
|
||||
[[NSDistributedNotificationCenter defaultCenter]
|
||||
removeObserver: self
|
||||
name: @"SOGoUserSettingsHaveChanged"
|
||||
object: nil];
|
||||
|
||||
[cache release];
|
||||
[users release];
|
||||
[super dealloc];
|
||||
@@ -167,21 +186,16 @@ static SOGoCache *sharedCache = nil;
|
||||
}
|
||||
|
||||
- (void) registerUser: (SOGoUser *) user
|
||||
{
|
||||
NSDate *cleanupDate;
|
||||
|
||||
cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval];
|
||||
|
||||
[users setObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: user, @"user", cleanupDate, @"cleanupDate", nil]
|
||||
{
|
||||
[users setObject: user
|
||||
forKey: [user login]];
|
||||
}
|
||||
|
||||
- (id) userNamed: (NSString *) name
|
||||
{
|
||||
return [[users objectForKey: name] objectForKey: @"user"];
|
||||
return [users objectForKey: name];
|
||||
}
|
||||
|
||||
|
||||
- (void) _userDefaultsHaveChanged: (NSNotification *) theNotification
|
||||
{
|
||||
NSMutableDictionary *d;
|
||||
@@ -214,29 +228,48 @@ static SOGoCache *sharedCache = nil;
|
||||
|
||||
- (void) _cleanupSources
|
||||
{
|
||||
NSDictionary *currentEntry;
|
||||
NSEnumerator *userIDs;
|
||||
NSString *currentID;
|
||||
NSDictionary *currentUser;
|
||||
NSDate *now;
|
||||
|
||||
unsigned int count;
|
||||
|
||||
now = [NSDate date];
|
||||
|
||||
// We cleanup the user defaults
|
||||
userIDs = [[[SOGoUser userDefaultsCache] allKeys] objectEnumerator];
|
||||
count = 0;
|
||||
userIDs = [[users allKeys] objectEnumerator];
|
||||
|
||||
while ((currentID = [userIDs nextObject]))
|
||||
{
|
||||
currentUser = [users objectForKey: currentID];
|
||||
if ([now earlierDate:
|
||||
[currentUser objectForKey: @"cleanupDate"]] == now)
|
||||
currentEntry = [[SOGoUser userDefaultsCache] objectForKey: currentID];
|
||||
|
||||
if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now)
|
||||
{
|
||||
[users removeObjectForKey: currentID];
|
||||
[SOGoUser setUserDefaultsFromDictionary: nil user: currentID];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count)
|
||||
[self logWithFormat: @"cleaned %d users records from cache", 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 setUserDefaultsFromDictionary: nil user: currentID];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count)
|
||||
[self logWithFormat: @"cleaned %d users records from user settings cache", count];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -112,10 +112,12 @@ extern NSString *SOGoWeekStartFirstFullWeek;
|
||||
|
||||
+ (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary
|
||||
user: (NSString *) login;
|
||||
+ (NSDictionary *) userDefaultsCache;
|
||||
- (NSUserDefaults *) userDefaults;
|
||||
|
||||
+ (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary
|
||||
user: (NSString *) login;
|
||||
+ (NSDictionary *) userSettingsCache;
|
||||
- (NSUserDefaults *) userSettings;
|
||||
|
||||
- (NSString *) language;
|
||||
|
||||
@@ -472,10 +472,26 @@ _timeValue (NSString *key)
|
||||
}
|
||||
|
||||
/* defaults */
|
||||
- (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary
|
||||
+ (void) setUserDefaultsFromDictionary: (NSDictionary *) theDictionary
|
||||
user: (NSString *) login
|
||||
{
|
||||
[userDefaults setObject: theDictionary forKey: 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
|
||||
@@ -484,7 +500,7 @@ _timeValue (NSString *key)
|
||||
|
||||
o = [userDefaults objectForKey: login];
|
||||
if (!o)
|
||||
{
|
||||
{
|
||||
o = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL
|
||||
uid: login
|
||||
fieldName: @"c_defaults"];
|
||||
@@ -498,16 +514,34 @@ _timeValue (NSString *key)
|
||||
if (![[o stringForKey: @"MessageCheck"] length])
|
||||
[o setObject: defaultMessageCheck forKey: @"MessageCheck"];
|
||||
|
||||
[userDefaults setObject: o forKey: login];
|
||||
[SOGoUser setUserDefaultsFromDictionary: o user: login];
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
return o;
|
||||
return [o objectForKey: @"dictionary"];
|
||||
}
|
||||
|
||||
+ (void) setUserSettingsFromDictionary: (NSDictionary *) theDictionary
|
||||
user: (NSString *) login
|
||||
{
|
||||
[userSettings setObject: theDictionary forKey: 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 userDefaults;
|
||||
}
|
||||
|
||||
- (NSUserDefaults *) userSettings
|
||||
@@ -517,14 +551,16 @@ _timeValue (NSString *key)
|
||||
o = [userSettings objectForKey: login];
|
||||
|
||||
if (!o)
|
||||
{
|
||||
{
|
||||
o = [[SOGoUserDefaults alloc] initWithTableURL: SOGoProfileURL
|
||||
uid: login
|
||||
fieldName: @"c_settings"];
|
||||
[userSettings setObject: o forKey: login];
|
||||
[SOGoUser setUserSettingsFromDictionary: o user: login];
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
return o;
|
||||
return [o objectForKey: @"dictionary"];
|
||||
}
|
||||
|
||||
- (NSString *) language
|
||||
|
||||
Reference in New Issue
Block a user