/* SOGoCache.m - this file is part of SOGo * * Copyright (C) 2008 Inverse inc. * * Author: Wolfgang Sourdeau * Ludovic Marcotte * * 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 #import #import #import #import #import #import #import #import #import "SOGoObject.h" #import "SOGoUser.h" #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. static NSTimeInterval cleanupInterval = 1800; static SOGoCache *sharedCache = nil; @implementation SOGoCache + (SOGoCache *) sharedCache { if (!sharedCache) sharedCache = [self new]; return sharedCache; } + (void) killCache { [sharedCache release]; sharedCache = nil; } - (id) init { if ((self = [super init])) { NSString *cleanupSetting; cache = [NSMutableDictionary new]; users = [NSMutableDictionary new]; // We register ourself for notifications [[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]; } return self; } - (void) dealloc { [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; if (object && name) { [self registerObject: container withName: [container nameInContainer] inContainer: [container container]]; fullPath = [self _pathFromObject: container withName: name]; if (![cache objectForKey: fullPath]) { // NSLog (@"registering '%@'", fullPath); [cache setObject: object forKey: fullPath]; } // else // NSLog (@"'%@' already registered", fullPath); } } - (id) objectNamed: (NSString *) name inContainer: (SOGoObject *) container { NSString *fullPath; fullPath = [self _pathFromObject: container withName: name]; return [cache objectForKey: fullPath]; // if (object) // NSLog (@"found cached object '%@'", fullPath); } - (void) registerUser: (SOGoUser *) user { NSDate *cleanupDate; cleanupDate = [[NSDate date] addTimeInterval: cleanupInterval]; [users setObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: user, @"user", cleanupDate, @"cleanupDate", nil] forKey: [user login]]; } - (id) userNamed: (NSString *) name { return [[users objectForKey: name] objectForKey: @"user"]; } - (void) _userDefaultsHaveChanged: (NSNotification *) theNotification { NSMutableDictionary *d; NSString *user; d = [[NSMutableDictionary alloc] init]; [d addEntriesFromDictionary: [theNotification userInfo]]; user = [d objectForKey: @"uid"]; [d removeObjectForKey: @"uid"]; [SOGoUser setUserDefaultsFromDictionary: d user: user]; [d release]; } - (void) _userSettingsHaveChanged: (NSNotification *) theNotification { NSMutableDictionary *d; NSString *user; d = [[NSMutableDictionary alloc] init]; [d addEntriesFromDictionary: [theNotification userInfo]]; user = [d objectForKey: @"uid"]; [d removeObjectForKey: @"uid"]; [SOGoUser setUserSettingsFromDictionary: d user: user]; [d release]; } - (void) _cleanupSources { NSEnumerator *userIDs; NSString *currentID; NSDictionary *currentUser; NSDate *now; unsigned int count; now = [NSDate date]; count = 0; userIDs = [[users allKeys] objectEnumerator]; while ((currentID = [userIDs nextObject])) { currentUser = [users objectForKey: currentID]; if ([now earlierDate: [currentUser objectForKey: @"cleanupDate"]] == now) { [users removeObjectForKey: currentID]; count++; } } if (count) [self logWithFormat: @"cleaned %d users records from cache", count]; } @end