mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-08-19 21:53:19 +00:00
Monotone-Parent: ff27cb5c0c118c485e7cf9d8e62d4428edafdb32
Monotone-Revision: 8373ee4a182606382fcb3aea7864b142db4aaa95 Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2008-12-12T18:55:12 Monotone-Branch: ca.inverse.sogo
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#import <Foundation/NSDictionary.h>
|
||||
#import <Foundation/NSDistributedNotificationCenter.h>
|
||||
#import <Foundation/NSEnumerator.h>
|
||||
#import <Foundation/NSLock.h>
|
||||
#import <Foundation/NSString.h>
|
||||
#import <Foundation/NSTimer.h>
|
||||
#import <Foundation/NSUserDefaults.h>
|
||||
@@ -51,8 +52,19 @@ static NSMutableDictionary *s_userSettings = nil;
|
||||
|
||||
static SOGoCache *sharedCache = nil;
|
||||
|
||||
#if defined(THREADSAFE)
|
||||
static NSLock *lock;
|
||||
#endif
|
||||
|
||||
@implementation SOGoCache
|
||||
|
||||
#if defined(THREADSAFE)
|
||||
+ (void) initialize
|
||||
{
|
||||
lock = [NSLock new];
|
||||
}
|
||||
#endif
|
||||
|
||||
+ (NSTimeInterval) cleanupInterval
|
||||
{
|
||||
return cleanupInterval;
|
||||
@@ -60,16 +72,28 @@ static SOGoCache *sharedCache = nil;
|
||||
|
||||
+ (SOGoCache *) sharedCache
|
||||
{
|
||||
#if defined(THREADSAFE)
|
||||
[lock lock];
|
||||
#endif
|
||||
if (!sharedCache)
|
||||
sharedCache = [self new];
|
||||
#if defined(THREADSAFE)
|
||||
[lock unlock];
|
||||
#endif
|
||||
|
||||
return sharedCache;
|
||||
}
|
||||
|
||||
+ (void) killCache
|
||||
{
|
||||
#if defined(THREADSAFE)
|
||||
[lock lock];
|
||||
#endif
|
||||
[cache removeAllObjects];
|
||||
[users removeAllObjects];
|
||||
#if defined(THREADSAFE)
|
||||
[lock unlock];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (id) init
|
||||
@@ -134,7 +158,7 @@ static SOGoCache *sharedCache = nil;
|
||||
}
|
||||
|
||||
- (NSString *) _pathFromObject: (SOGoObject *) container
|
||||
withName: (NSString *) name
|
||||
withName: (NSString *) name
|
||||
{
|
||||
NSString *fullPath, *nameInContainer;
|
||||
NSMutableArray *names;
|
||||
@@ -169,13 +193,19 @@ static SOGoCache *sharedCache = nil;
|
||||
inContainer: [container container]];
|
||||
fullPath = [self _pathFromObject: container
|
||||
withName: name];
|
||||
#if defined(THREADSAFE)
|
||||
[lock lock];
|
||||
#endif
|
||||
if (![cache objectForKey: fullPath])
|
||||
{
|
||||
// NSLog (@"registering '%@'", fullPath);
|
||||
// NSLog (@"registering '%@'", fullPath);
|
||||
[cache setObject: object forKey: fullPath];
|
||||
}
|
||||
// else
|
||||
// NSLog (@"'%@' already registered", fullPath);
|
||||
#if defined(THREADSAFE)
|
||||
[lock unlock];
|
||||
#endif
|
||||
// else
|
||||
// NSLog (@"'%@' already registered", fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,14 +218,20 @@ static SOGoCache *sharedCache = nil;
|
||||
withName: name];
|
||||
|
||||
return [cache objectForKey: fullPath];
|
||||
// if (object)
|
||||
// NSLog (@"found cached object '%@'", fullPath);
|
||||
// if (object)
|
||||
// NSLog (@"found cached object '%@'", fullPath);
|
||||
}
|
||||
|
||||
- (void) registerUser: (SOGoUser *) user
|
||||
{
|
||||
#if defined(THREADSAFE)
|
||||
[lock lock];
|
||||
#endif
|
||||
[users setObject: user
|
||||
forKey: [user login]];
|
||||
#if defined(THREADSAFE)
|
||||
[lock unlock];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (id) userNamed: (NSString *) name
|
||||
@@ -214,11 +250,17 @@ static SOGoCache *sharedCache = nil;
|
||||
{
|
||||
NSDate *cleanupDate;
|
||||
|
||||
#if defined(THREADSAFE)
|
||||
[lock lock];
|
||||
#endif
|
||||
cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]];
|
||||
[s_userDefaults setObject: [NSDictionary dictionaryWithObjectsAndKeys:
|
||||
theDefaults, @"dictionary",
|
||||
cleanupDate, @"cleanupDate", nil]
|
||||
forKey: login];
|
||||
#if defined(THREADSAFE)
|
||||
[lock unlock];
|
||||
#endif
|
||||
}
|
||||
|
||||
+ (NSDictionary *) cachedUserSettings
|
||||
@@ -231,11 +273,17 @@ static SOGoCache *sharedCache = nil;
|
||||
{
|
||||
NSDate *cleanupDate;
|
||||
|
||||
#if defined(THREADSAFE)
|
||||
[lock lock];
|
||||
#endif
|
||||
cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]];
|
||||
[s_userSettings setObject: [NSDictionary dictionaryWithObjectsAndKeys:
|
||||
theSettings, @"dictionary",
|
||||
cleanupDate, @"cleanupDate", nil]
|
||||
forKey: login];
|
||||
#if defined(THREADSAFE)
|
||||
[lock unlock];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void) _userDefaultsHaveChanged: (NSNotification *) theNotification
|
||||
@@ -245,6 +293,9 @@ static SOGoCache *sharedCache = nil;
|
||||
|
||||
uid = [[theNotification userInfo] objectForKey: @"uid"];
|
||||
|
||||
#if defined(THREADSAFE)
|
||||
[lock lock];
|
||||
#endif
|
||||
if ((user = [users objectForKey: uid]))
|
||||
{
|
||||
[[user userDefaults] setValues: [[theNotification userInfo] objectForKey: @"values"]];
|
||||
@@ -254,6 +305,9 @@ static SOGoCache *sharedCache = nil;
|
||||
{
|
||||
[s_userDefaults removeObjectForKey: uid];
|
||||
}
|
||||
#if defined(THREADSAFE)
|
||||
[lock unlock];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void) _userSettingsHaveChanged: (NSNotification *) theNotification
|
||||
@@ -283,6 +337,10 @@ static SOGoCache *sharedCache = nil;
|
||||
|
||||
unsigned int count;
|
||||
|
||||
#if defined(THREADSAFE)
|
||||
[lock lock];
|
||||
#endif
|
||||
|
||||
now = [NSDate date];
|
||||
|
||||
// We cleanup the user defaults
|
||||
@@ -317,7 +375,12 @@ static SOGoCache *sharedCache = nil;
|
||||
}
|
||||
|
||||
if (count)
|
||||
[self logWithFormat: @"cleaned %d users records from user settings cache", count];
|
||||
[self logWithFormat: @"cleaned %d users records from user settings cache",
|
||||
count];
|
||||
|
||||
#if defined(THREADSAFE)
|
||||
[lock unlock];
|
||||
#endif
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user