2008-01-16 19:47:20 +01:00
|
|
|
/* SOGoCache.m - this file is part of SOGo
|
|
|
|
*
|
2008-09-27 03:13:41 +02:00
|
|
|
* Copyright (C) 2008 Inverse inc.
|
2008-01-16 19:47:20 +01:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
2008-11-11 01:17:35 +01:00
|
|
|
* Ludovic Marcotte <lmarcotte@inverse.ca>
|
2008-01-16 19:47:20 +01:00
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Foundation/NSArray.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <Foundation/NSDistributedNotificationCenter.h>
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSString.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <Foundation/NSTimer.h>
|
|
|
|
#import <Foundation/NSUserDefaults.h>
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
#import <NGObjWeb/SoObject.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2008-01-16 19:47:20 +01:00
|
|
|
|
2008-07-29 18:36:16 +02:00
|
|
|
#import "SOGoObject.h"
|
2008-01-16 19:47:20 +01:00
|
|
|
#import "SOGoUser.h"
|
2008-11-12 04:30:13 +01:00
|
|
|
#import "SOGoUserDefaults.h"
|
2008-07-29 18:36:16 +02:00
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
#import "SOGoCache.h"
|
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
// We define the default value for cleaning up cached
|
2008-11-11 02:52:56 +01:00
|
|
|
// users' preferences. This value should be relatively
|
|
|
|
// high to avoid useless database calls.
|
2008-11-11 01:17:35 +01:00
|
|
|
static NSTimeInterval cleanupInterval = 1800;
|
2008-11-11 02:52:56 +01:00
|
|
|
|
|
|
|
static NSMutableDictionary *cache = nil;
|
|
|
|
static NSMutableDictionary *users = nil;
|
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
static NSMutableDictionary *s_userDefaults = nil;
|
|
|
|
static NSMutableDictionary *s_userSettings = nil;
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
static SOGoCache *sharedCache = nil;
|
|
|
|
|
|
|
|
@implementation SOGoCache
|
|
|
|
|
2008-11-11 02:52:56 +01:00
|
|
|
+ (NSTimeInterval) cleanupInterval
|
|
|
|
{
|
|
|
|
return cleanupInterval;
|
|
|
|
}
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
+ (SOGoCache *) sharedCache
|
|
|
|
{
|
|
|
|
if (!sharedCache)
|
|
|
|
sharedCache = [self new];
|
|
|
|
|
|
|
|
return sharedCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) killCache
|
|
|
|
{
|
2008-11-11 02:52:56 +01:00
|
|
|
[cache removeAllObjects];
|
|
|
|
[users removeAllObjects];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2008-11-11 01:17:35 +01:00
|
|
|
NSString *cleanupSetting;
|
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
cache = [[NSMutableDictionary alloc] init];
|
|
|
|
users = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
s_userDefaults = [[NSMutableDictionary alloc] init];
|
|
|
|
s_userSettings = [[NSMutableDictionary alloc] init];
|
2008-11-14 01:55:00 +01:00
|
|
|
|
|
|
|
// We register ourself for notifications
|
2008-11-11 01:17:35 +01:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(_userDefaultsHaveChanged:)
|
|
|
|
name: @"SOGoUserDefaultsHaveChanged"
|
|
|
|
object: nil];
|
|
|
|
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(_userSettingsHaveChanged:)
|
|
|
|
name: @"SOGoUserSettingsHaveChanged"
|
|
|
|
object: nil];
|
|
|
|
|
|
|
|
// We fire our timer that will cleanup cache entries
|
|
|
|
cleanupSetting = [[NSUserDefaults standardUserDefaults]
|
|
|
|
objectForKey: @"SOGoCacheCleanupInterval"];
|
|
|
|
|
|
|
|
if (cleanupSetting && [cleanupSetting doubleValue] > 0.0)
|
|
|
|
cleanupInterval = [cleanupSetting doubleValue];
|
|
|
|
|
|
|
|
_cleanupTimer = [NSTimer scheduledTimerWithTimeInterval: cleanupInterval
|
|
|
|
target: self
|
|
|
|
selector: @selector(_cleanupSources)
|
|
|
|
userInfo: nil
|
|
|
|
repeats: YES];
|
|
|
|
[self logWithFormat: @"cleanup interval set every %f seconds",
|
|
|
|
cleanupInterval];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2008-11-11 02:52:56 +01:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
removeObserver: self
|
|
|
|
name: @"SOGoUserDefaultsHaveChanged"
|
|
|
|
object: nil];
|
|
|
|
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
removeObserver: self
|
|
|
|
name: @"SOGoUserSettingsHaveChanged"
|
|
|
|
object: nil];
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
[cache release];
|
|
|
|
[users release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) _pathFromObject: (SOGoObject *) container
|
|
|
|
withName: (NSString *) name
|
|
|
|
{
|
|
|
|
NSString *fullPath, *nameInContainer;
|
|
|
|
NSMutableArray *names;
|
|
|
|
id currentObject;
|
|
|
|
|
|
|
|
names = [NSMutableArray new];
|
|
|
|
|
|
|
|
[names addObject: name];
|
|
|
|
currentObject = container;
|
|
|
|
while ((nameInContainer = [currentObject nameInContainer]))
|
|
|
|
{
|
|
|
|
[names addObject: nameInContainer];
|
|
|
|
currentObject = [currentObject container];
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath = [names componentsJoinedByString: @"/"];
|
|
|
|
[names release];
|
|
|
|
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerObject: (id) object
|
|
|
|
withName: (NSString *) name
|
|
|
|
inContainer: (SOGoObject *) container
|
|
|
|
{
|
|
|
|
NSString *fullPath;
|
|
|
|
|
2008-01-16 20:52:43 +01:00
|
|
|
if (object && name)
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
2008-01-16 20:52:43 +01:00
|
|
|
[self registerObject: container
|
|
|
|
withName: [container nameInContainer]
|
|
|
|
inContainer: [container container]];
|
2008-01-16 19:47:20 +01:00
|
|
|
fullPath = [self _pathFromObject: container
|
|
|
|
withName: name];
|
2008-01-16 20:52:43 +01:00
|
|
|
if (![cache objectForKey: fullPath])
|
|
|
|
{
|
|
|
|
// NSLog (@"registering '%@'", fullPath);
|
|
|
|
[cache setObject: object forKey: fullPath];
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
// NSLog (@"'%@' already registered", fullPath);
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectNamed: (NSString *) name
|
|
|
|
inContainer: (SOGoObject *) container
|
|
|
|
{
|
|
|
|
NSString *fullPath;
|
|
|
|
|
|
|
|
fullPath = [self _pathFromObject: container
|
|
|
|
withName: name];
|
|
|
|
|
|
|
|
return [cache objectForKey: fullPath];
|
2008-01-16 20:52:43 +01:00
|
|
|
// if (object)
|
|
|
|
// NSLog (@"found cached object '%@'", fullPath);
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerUser: (SOGoUser *) user
|
2008-11-11 02:52:56 +01:00
|
|
|
{
|
|
|
|
[users setObject: user
|
2008-11-11 01:17:35 +01:00
|
|
|
forKey: [user login]];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) userNamed: (NSString *) name
|
|
|
|
{
|
2008-11-11 02:52:56 +01:00
|
|
|
return [users objectForKey: name];
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
+ (NSDictionary *) cachedUserDefaults
|
2008-11-11 01:17:35 +01:00
|
|
|
{
|
2008-11-12 04:30:13 +01:00
|
|
|
return s_userDefaults;
|
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
/* defaults */
|
|
|
|
+ (void) setCachedUserDefaults: (SOGoUserDefaults *) theDefaults
|
|
|
|
user: (NSString *) login
|
|
|
|
{
|
|
|
|
NSDate *cleanupDate;
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
cleanupDate = [[NSDate date] addTimeInterval: [SOGoCache cleanupInterval]];
|
|
|
|
[s_userDefaults setObject: [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
theDefaults, @"dictionary",
|
|
|
|
cleanupDate, @"cleanupDate", nil]
|
|
|
|
forKey: login];
|
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
+ (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
|
|
|
|
{
|
2008-11-20 20:17:59 +01:00
|
|
|
SOGoUser *user;
|
|
|
|
NSString *uid;
|
2008-11-12 04:30:13 +01:00
|
|
|
|
2008-11-20 20:17:59 +01:00
|
|
|
uid = [[theNotification userInfo] objectForKey: @"uid"];
|
|
|
|
|
|
|
|
if ((user = [users objectForKey: uid]))
|
|
|
|
{
|
|
|
|
[[user userDefaults] setValues: [[theNotification userInfo] objectForKey: @"values"]];
|
2008-12-05 02:20:59 +01:00
|
|
|
[SOGoCache setCachedUserDefaults: (SOGoUserDefaults*)[user userDefaults] user: uid];
|
2008-12-02 02:36:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[s_userDefaults removeObjectForKey: uid];
|
2008-11-20 20:17:59 +01:00
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _userSettingsHaveChanged: (NSNotification *) theNotification
|
|
|
|
{
|
2008-11-20 20:17:59 +01:00
|
|
|
SOGoUser *user;
|
|
|
|
NSString *uid;
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2008-11-20 20:17:59 +01:00
|
|
|
uid = [[theNotification userInfo] objectForKey: @"uid"];
|
|
|
|
|
|
|
|
if ((user = [users objectForKey: uid]))
|
|
|
|
{
|
|
|
|
[[user userSettings] setValues: [[theNotification userInfo] objectForKey: @"values"]];
|
2008-12-05 02:20:59 +01:00
|
|
|
[SOGoCache setCachedUserSettings: (SOGoUserDefaults *)[user userSettings] user: uid];
|
2008-12-02 02:36:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[s_userSettings removeObjectForKey: uid];
|
2008-11-20 20:17:59 +01:00
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _cleanupSources
|
|
|
|
{
|
2008-11-11 02:52:56 +01:00
|
|
|
NSDictionary *currentEntry;
|
2008-11-11 01:17:35 +01:00
|
|
|
NSEnumerator *userIDs;
|
|
|
|
NSString *currentID;
|
|
|
|
NSDate *now;
|
2008-11-11 02:52:56 +01:00
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
unsigned int count;
|
|
|
|
|
|
|
|
now = [NSDate date];
|
2008-11-11 02:52:56 +01:00
|
|
|
|
|
|
|
// We cleanup the user defaults
|
2008-11-12 04:30:13 +01:00
|
|
|
userIDs = [[s_userDefaults allKeys] objectEnumerator];
|
2008-11-11 01:17:35 +01:00
|
|
|
count = 0;
|
|
|
|
while ((currentID = [userIDs nextObject]))
|
|
|
|
{
|
2008-11-12 04:30:13 +01:00
|
|
|
currentEntry = [s_userDefaults objectForKey: currentID];
|
2008-11-11 02:52:56 +01:00
|
|
|
|
|
|
|
if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now)
|
2008-11-11 01:17:35 +01:00
|
|
|
{
|
2008-11-12 04:30:13 +01:00
|
|
|
[s_userDefaults removeObjectForKey: currentID];
|
2008-11-11 01:17:35 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2008-11-12 04:30:13 +01:00
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
if (count)
|
2008-11-11 02:52:56 +01:00
|
|
|
[self logWithFormat: @"cleaned %d users records from user defaults cache", count];
|
|
|
|
|
|
|
|
// We cleanup the user settings
|
2008-11-12 04:30:13 +01:00
|
|
|
userIDs = [[s_userSettings allKeys] objectEnumerator];
|
2008-11-11 02:52:56 +01:00
|
|
|
count = 0;
|
|
|
|
while ((currentID = [userIDs nextObject]))
|
|
|
|
{
|
2008-11-12 04:30:13 +01:00
|
|
|
currentEntry = [s_userSettings objectForKey: currentID];
|
2008-11-11 02:52:56 +01:00
|
|
|
|
|
|
|
if ([now earlierDate: [currentEntry objectForKey: @"cleanupDate"]] == now)
|
|
|
|
{
|
2008-11-12 04:30:13 +01:00
|
|
|
[s_userSettings removeObjectForKey: currentID];
|
2008-11-11 02:52:56 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
if (count)
|
2008-11-11 02:52:56 +01:00
|
|
|
[self logWithFormat: @"cleaned %d users records from user settings cache", count];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|