mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-09-29 09:00:31 +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:
+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
|
||||
|
||||
Reference in New Issue
Block a user