From 81e9b4728a40ff97023806e3e9463ae528169c73 Mon Sep 17 00:00:00 2001 From: znek Date: Mon, 1 Nov 2004 00:53:29 +0000 Subject: [PATCH] added SOGoLRUCache and made AgenorUserManager use it git-svn-id: http://svn.opengroupware.org/SOGo/trunk@445 d1b88da0-ebda-0310-925b-ed51d893ca5b --- SOGo/SOGo.xcode/project.pbxproj | 23 +++++++ SOGoLogic/AgenorUserManager.h | 3 + SOGoLogic/AgenorUserManager.m | 12 +++- SOGoLogic/ChangeLog | 11 +++ SOGoLogic/GNUmakefile | 3 + SOGoLogic/SOGoLRUCache.h | 44 ++++++++++++ SOGoLogic/SOGoLRUCache.m | 114 ++++++++++++++++++++++++++++++++ SOGoLogic/Version | 2 +- 8 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 SOGoLogic/SOGoLRUCache.h create mode 100644 SOGoLogic/SOGoLRUCache.m diff --git a/SOGo/SOGo.xcode/project.pbxproj b/SOGo/SOGo.xcode/project.pbxproj index e42edc91..48e0ed94 100644 --- a/SOGo/SOGo.xcode/project.pbxproj +++ b/SOGo/SOGo.xcode/project.pbxproj @@ -569,6 +569,8 @@ AD73BE4B06CF88BF00226A2D, AD071D1206CD2BCB00A9EEF4, AD071D1306CD2BCB00A9EEF4, + AD255F390735B15E00D424E6, + AD255F3A0735B15E00D424E6, ADA6333607140E0D0058C21C, ); isa = PBXGroup; @@ -616,6 +618,27 @@ tabWidth = 8; usesTabs = 1; }; + AD255F390735B15E00D424E6 = { + fileEncoding = 5; + indentWidth = 2; + isa = PBXFileReference; + lastKnownFileType = sourcecode.c.h; + path = SOGoLRUCache.h; + refType = 4; + sourceTree = ""; + tabWidth = 2; + usesTabs = 0; + }; + AD255F3A0735B15E00D424E6 = { + fileEncoding = 5; + indentWidth = 2; + isa = PBXFileReference; + lastKnownFileType = sourcecode.c.objc; + path = SOGoLRUCache.m; + refType = 4; + sourceTree = ""; + tabWidth = 2; + }; AD2C74A1071A9FF70087E027 = { fileEncoding = 5; isa = PBXFileReference; diff --git a/SOGoLogic/AgenorUserManager.h b/SOGoLogic/AgenorUserManager.h index 3ca73558..0716a199 100644 --- a/SOGoLogic/AgenorUserManager.h +++ b/SOGoLogic/AgenorUserManager.h @@ -27,9 +27,12 @@ #import +@class SOGoLRUCache; @interface AgenorUserManager : NSObject { + SOGoLRUCache *cnCache; + SOGoLRUCache *serverCache; } + (id)sharedUserManager; diff --git a/SOGoLogic/AgenorUserManager.m b/SOGoLogic/AgenorUserManager.m index 15880827..94d1b91d 100644 --- a/SOGoLogic/AgenorUserManager.m +++ b/SOGoLogic/AgenorUserManager.m @@ -23,7 +23,7 @@ #include "AgenorUserManager.h" #include - +#include "SOGoLRUCache.h" @interface AgenorUserManager (PrivateAPI) - (NGLdapConnection *)ldapConnection; @@ -70,11 +70,15 @@ static NSString *ldapBaseDN = nil; - (id)init { self = [super init]; if(self) { + self->serverCache = [[SOGoLRUCache alloc] initWithCacheSize:10000]; + self->cnCache = [[SOGoLRUCache alloc] initWithCacheSize:10000]; } return self; } - (void)dealloc { + [self->serverCache release]; + [self->cnCache release]; [super dealloc]; } @@ -90,17 +94,19 @@ static NSString *ldapBaseDN = nil; } - (void)_cacheCN:(NSString *)_cn forUID:(NSString *)_uid { + [self->cnCache addObject:_cn forKey:_uid]; } - (NSString *)_cachedCNForUID:(NSString *)_uid { - return nil; + return [self->cnCache objectForKey:_uid]; } - (void)_cacheServer:(NSString *)_server forUID:(NSString *)_uid { + [self->serverCache addObject:_server forKey:_uid]; } - (NSString *)_cachedServerForUID:(NSString *)_uid { - return nil; + return [self->serverCache objectForKey:_uid]; } diff --git a/SOGoLogic/ChangeLog b/SOGoLogic/ChangeLog index 78bb6dd8..76b8d452 100644 --- a/SOGoLogic/ChangeLog +++ b/SOGoLogic/ChangeLog @@ -1,3 +1,14 @@ +2004-11-01 Marcus Mueller + + * v0.9.25 + + * SOGoLRUCache.[hm]: new class providing a generic cache for + objects used in SOGo. The cache is a LRU cache using a use count + to determined which objects are accessed most/least. + + * AgenorUserManager.[hm]: uses SOGoLRUCache to cache CN's and + Servers. + 2004-10-20 Marcus Mueller * AgenorUserManager.[hm]: slightly changed the interface for retrieving diff --git a/SOGoLogic/GNUmakefile b/SOGoLogic/GNUmakefile index 76c923d5..71eae7f3 100644 --- a/SOGoLogic/GNUmakefile +++ b/SOGoLogic/GNUmakefile @@ -1,6 +1,7 @@ # GNUstep Makefile include $(GNUSTEP_MAKEFILES)/common.make +include ../SOGo/Version include ./Version LIBRARY_NAME = libSOGoLogic @@ -13,11 +14,13 @@ libSOGoLogic_HEADER_FILES += \ SOGoAppointment.h \ SOGoAppointmentICalRenderer.h \ AgenorUserManager.h \ + SOGoLRUCache.h \ NSString+iCal.h libSOGoLogic_OBJC_FILES += \ SOGoAppointment.m \ SOGoAppointmentICalRenderer.m \ + SOGoLRUCache.m \ AgenorUserManager.m \ -include GNUmakefile.preamble diff --git a/SOGoLogic/SOGoLRUCache.h b/SOGoLogic/SOGoLRUCache.h new file mode 100644 index 00000000..1d29a504 --- /dev/null +++ b/SOGoLogic/SOGoLRUCache.h @@ -0,0 +1,44 @@ +/* + Copyright (C) 2000-2004 SKYRIX Software AG + + This file is part of OGo + + OGo is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + OGo 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with OGo; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. + */ +// $Id$ + + +#ifndef __SOGoLRUCache_H_ +#define __SOGoLRUCache_H_ + + +#import + + +@interface SOGoLRUCache : NSObject +{ + unsigned size; + NSMutableDictionary *entries; +} + +- (id)initWithCacheSize:(unsigned)_size; + +- (void)addObject:(id)_obj forKey:(id)_key; +- (id)objectForKey:(id)_key; + +@end + +#endif /* __SOGoLRUCache_H_ */ diff --git a/SOGoLogic/SOGoLRUCache.m b/SOGoLogic/SOGoLRUCache.m new file mode 100644 index 00000000..dd11e77c --- /dev/null +++ b/SOGoLogic/SOGoLRUCache.m @@ -0,0 +1,114 @@ +/* + Copyright (C) 2000-2004 SKYRIX Software AG + + This file is part of OGo + + OGo is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + OGo 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 Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with OGo; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. + */ +// $Id$ + + +#import "SOGoLRUCache.h" +#import "common.h" + +@interface SOGoLRUCacheItem : NSObject +{ + id object; + unsigned useCount; +} + +- (id)initWithObject:(id)_obj; +- (id)object; + +- (unsigned)useCount; + +@end + +@implementation SOGoLRUCacheItem + +- (id)initWithObject:(id)_obj { + self = [super init]; + if(self) { + ASSIGN(self->object, _obj); + self->useCount = 1; + } + return self; +} + +- (id)object { + self->useCount++; + return self->object; +} + +- (unsigned)useCount { + return self->useCount; +} + +@end + +@implementation SOGoLRUCache + +- (id)initWithCacheSize:(unsigned)_size { + self = [super init]; + if(self) { + self->size = _size; + self->entries = [[NSMutableDictionary alloc] initWithCapacity:_size]; + } + return self; +} + +- (void)dealloc { + [self->entries release]; + [super dealloc]; +} + +- (void)addObject:(id)_obj forKey:(id)_key { + SOGoLRUCacheItem *item; + + NSAssert(_obj, @"Attempt to insert nil object!"); + + if([self->entries count] >= self->size) { + /* need to find minimum and get rid of it */ + NSEnumerator *keyEnum; + SOGoLRUCacheItem *item; + id key, leastUsedItemKey; + unsigned minimumUseCount = INT_MAX; + + keyEnum = [self->entries keyEnumerator]; + while((key = [keyEnum nextObject])) { + item = [self->entries objectForKey:key]; + if([item useCount] < minimumUseCount) { + minimumUseCount = [item useCount]; + leastUsedItemKey = key; + } + } + [self->entries removeObjectForKey:leastUsedItemKey]; + } + item = [[SOGoLRUCacheItem alloc] initWithObject:_obj]; + [self->entries setObject:item forKey:_key]; + [item release]; +} + +- (id)objectForKey:(id)_key { + SOGoLRUCacheItem *item; + + item = [self->entries objectForKey:_key]; + if(!item) + return nil; + return [item object]; +} + +@end diff --git a/SOGoLogic/Version b/SOGoLogic/Version index bd948570..6238c965 100644 --- a/SOGoLogic/Version +++ b/SOGoLogic/Version @@ -1,6 +1,6 @@ # Version file -SUBMINOR_VERSION:=24 +SUBMINOR_VERSION:=25 # v0.9.23 requires NGiCal v4.3.32 # v0.9.18 requires NGExtensions v4.3.123 -- 2.39.5