2008-01-16 19:47:20 +01:00
|
|
|
/* SOGoCache.m - this file is part of SOGo
|
|
|
|
*
|
2009-02-24 02:54:59 +01:00
|
|
|
* Copyright (C) 2008-2009 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.
|
|
|
|
*/
|
|
|
|
|
2009-02-24 02:54:59 +01:00
|
|
|
/*
|
2009-08-03 17:46:02 +02:00
|
|
|
* [ Cache Structure ]
|
2009-02-24 02:54:59 +01:00
|
|
|
*
|
2009-08-03 17:46:02 +02:00
|
|
|
* users value = instances of SOGoUser > flushed after the completion of every SOGo requests
|
|
|
|
* <uid>+defaults value = NSDictionary instance > user's defaults
|
|
|
|
* <uid>+settings value = NSDictionary instance > user's settings
|
|
|
|
* <uid>+attributes value = NSMutableDictionary instance > user's LDAP attributes
|
2009-02-24 02:54:59 +01:00
|
|
|
*/
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2009-11-19 18:08:47 +01:00
|
|
|
#import <Foundation/NSData.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-12-12 19:55:12 +01:00
|
|
|
#import <Foundation/NSLock.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>
|
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"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoSystemDefaults.h"
|
2008-01-16 19:47:20 +01:00
|
|
|
#import "SOGoUser.h"
|
2009-11-29 05:19:32 +01:00
|
|
|
#import "SOGoUserProfile.h"
|
2008-07-29 18:36:16 +02:00
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
#import "SOGoCache.h"
|
|
|
|
|
|
|
|
@implementation SOGoCache
|
|
|
|
|
|
|
|
+ (SOGoCache *) sharedCache
|
|
|
|
{
|
2009-11-16 16:02:14 +01:00
|
|
|
static SOGoCache *sharedCache = nil;
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
if (!sharedCache)
|
2009-11-19 18:08:47 +01:00
|
|
|
sharedCache = [self new];
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
return sharedCache;
|
|
|
|
}
|
|
|
|
|
2009-11-16 16:02:14 +01:00
|
|
|
- (void) killCache
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
2008-11-11 02:52:56 +01:00
|
|
|
[cache removeAllObjects];
|
2009-08-03 17:46:02 +02:00
|
|
|
|
|
|
|
// This is essential for refetching the cached values in case something has changed
|
|
|
|
// accross various sogod processes
|
|
|
|
[users removeAllObjects];
|
2009-10-13 22:17:29 +02:00
|
|
|
[localCache removeAllObjects];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2009-11-29 05:19:32 +01:00
|
|
|
SOGoSystemDefaults *sd;
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2009-08-03 17:46:02 +02:00
|
|
|
memcached_return error;
|
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
cache = [NSMutableDictionary new];
|
|
|
|
users = [NSMutableDictionary new];
|
2009-08-03 17:46:02 +02:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
// localCache is used to avoid going all the time to the memcached
|
|
|
|
// server during each request. We'll cache the value we got from
|
|
|
|
// memcached for the duration of the current request - which is good
|
|
|
|
// enough for pretty much all cases. We surely don't want to get new
|
|
|
|
// defaults/settings during the _same_ requests, it could produce
|
|
|
|
// relatively strange behaviors
|
|
|
|
localCache = [NSMutableDictionary new];
|
2009-08-03 17:46:02 +02:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
handle = memcached_create(NULL);
|
2009-08-03 17:46:02 +02:00
|
|
|
if (handle)
|
|
|
|
{
|
2009-11-19 18:08:47 +01:00
|
|
|
#warning We could also make the port number configurable and even make use \
|
|
|
|
of NGNetUtilities for that.
|
2009-11-29 05:19:32 +01:00
|
|
|
|
|
|
|
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
|
|
|
// We define the default value for cleaning up cached users'
|
|
|
|
// preferences. This value should be relatively high to avoid
|
|
|
|
// useless database calls.
|
|
|
|
|
|
|
|
cleanupInterval = [sd cacheCleanupInterval];
|
|
|
|
ASSIGN (memcachedServerName, [sd memcachedHost]);
|
|
|
|
|
|
|
|
[self logWithFormat: @"Cache cleanup interval set every %f seconds",
|
|
|
|
cleanupInterval];
|
|
|
|
[self logWithFormat: @"Using host '%@' as server",
|
|
|
|
memcachedServerName];
|
2009-11-19 18:08:47 +01:00
|
|
|
servers
|
|
|
|
= memcached_server_list_append(NULL,
|
|
|
|
[memcachedServerName UTF8String],
|
|
|
|
11211, &error);
|
2009-08-03 17:46:02 +02:00
|
|
|
error = memcached_server_push(handle, servers);
|
|
|
|
}
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2009-08-03 17:46:02 +02:00
|
|
|
memcached_server_free(servers);
|
|
|
|
memcached_free(handle);
|
2009-11-29 05:19:32 +01:00
|
|
|
[memcachedServerName release];
|
2008-01-16 19:47:20 +01:00
|
|
|
[cache release];
|
|
|
|
[users release];
|
2009-10-13 22:17:29 +02:00
|
|
|
[localCache release];
|
2008-01-16 19:47:20 +01:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) _pathFromObject: (SOGoObject *) container
|
2009-02-24 02:54:59 +01:00
|
|
|
withName: (NSString *) name
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
|
|
|
NSString *fullPath, *nameInContainer;
|
|
|
|
NSMutableArray *names;
|
|
|
|
id currentObject;
|
|
|
|
|
2009-06-04 20:56:06 +02:00
|
|
|
if ([name length])
|
2008-01-16 19:47:20 +01:00
|
|
|
{
|
2009-06-04 20:56:06 +02:00
|
|
|
names = [NSMutableArray array];
|
2008-01-16 19:47:20 +01:00
|
|
|
|
2009-06-04 20:56:06 +02:00
|
|
|
[names addObject: name];
|
|
|
|
currentObject = container;
|
|
|
|
while ((nameInContainer = [currentObject nameInContainer]))
|
|
|
|
{
|
|
|
|
[names addObject: nameInContainer];
|
|
|
|
currentObject = [currentObject container];
|
|
|
|
}
|
|
|
|
|
|
|
|
fullPath = [names componentsJoinedByString: @"/"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fullPath = nil;
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
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])
|
|
|
|
{
|
|
|
|
[cache setObject: object forKey: 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-12-12 19:55:12 +01:00
|
|
|
// if (object)
|
|
|
|
// NSLog (@"found cached object '%@'", fullPath);
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) registerUser: (SOGoUser *) user
|
2009-09-11 16:38:43 +02:00
|
|
|
withName: (NSString *) userName
|
2008-11-11 02:52:56 +01:00
|
|
|
{
|
2009-09-11 16:38:43 +02:00
|
|
|
[users setObject: user forKey: userName];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) userNamed: (NSString *) name
|
|
|
|
{
|
2009-08-03 17:46:02 +02:00
|
|
|
return [users objectForKey: name];
|
2008-11-12 04:30:13 +01:00
|
|
|
}
|
|
|
|
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
2009-08-03 17:46:02 +02:00
|
|
|
// For non-blocking cache method, see memcached_behavior_set and MEMCACHED_BEHAVIOR_NO_BLOCK
|
|
|
|
// memcached is thread-safe so no need to lock here.
|
2009-03-03 20:08:02 +01:00
|
|
|
//
|
2009-11-19 18:08:47 +01:00
|
|
|
- (void) _cacheValues: (NSString *) theAttributes
|
|
|
|
ofType: (NSString *) theType
|
|
|
|
forLogin: (NSString *) theLogin
|
2009-03-03 20:08:02 +01:00
|
|
|
{
|
2009-08-03 17:46:02 +02:00
|
|
|
memcached_return error;
|
2009-11-19 18:08:47 +01:00
|
|
|
NSString *keyName;
|
|
|
|
NSData *key, *value;
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
keyName = [NSString stringWithFormat: @"%@+%@", theLogin, theType];
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
key = [keyName dataUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
value = [theAttributes dataUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
error = memcached_set(handle,
|
|
|
|
[key bytes], [key length],
|
|
|
|
[value bytes], [value length],
|
|
|
|
cleanupInterval, 0);
|
2009-11-26 20:32:24 +01:00
|
|
|
[localCache setObject: theAttributes forKey: keyName];
|
2009-11-19 18:08:47 +01:00
|
|
|
|
|
|
|
if (error != MEMCACHED_SUCCESS)
|
|
|
|
[self logWithFormat: @"memcached error: unable to cache values with subtype '%@' for user '%@'", theType, theLogin];
|
|
|
|
//else
|
|
|
|
//[self logWithFormat: @"memcached: cached values (%s) with subtype %@
|
|
|
|
//for user %@", value, theType, theLogin];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"attempting to cache value for key '%@' while"
|
|
|
|
" no handle exists", keyName];
|
2009-02-24 02:54:59 +01:00
|
|
|
}
|
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (NSString *) _valuesOfType: (NSString *) theType
|
|
|
|
forLogin: (NSString *) theLogin
|
2009-02-24 02:54:59 +01:00
|
|
|
{
|
2009-11-19 18:08:47 +01:00
|
|
|
NSString *valueString, *keyName;
|
|
|
|
NSData *key;
|
|
|
|
char *value;
|
|
|
|
size_t vlen;
|
|
|
|
memcached_return rc;
|
|
|
|
unsigned int flags;
|
2009-03-03 20:08:02 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
valueString = nil;
|
2009-03-24 21:36:49 +01:00
|
|
|
|
2010-01-06 01:20:10 +01:00
|
|
|
keyName = [NSString stringWithFormat: @"%@+%@", theLogin, theType];
|
2009-11-19 18:08:47 +01:00
|
|
|
if (handle)
|
2008-12-02 02:36:53 +01:00
|
|
|
{
|
2009-11-19 18:08:47 +01:00
|
|
|
valueString = [localCache objectForKey: keyName];
|
|
|
|
if (!valueString)
|
|
|
|
{
|
|
|
|
key = [keyName dataUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
value = memcached_get (handle, [key bytes], [key length],
|
|
|
|
&vlen, &flags, &rc);
|
|
|
|
if (rc == MEMCACHED_SUCCESS && value)
|
|
|
|
{
|
|
|
|
valueString
|
|
|
|
= [[NSString alloc] initWithBytesNoCopy: value
|
|
|
|
length: vlen
|
|
|
|
encoding: NSUTF8StringEncoding
|
|
|
|
freeWhenDone: YES];
|
|
|
|
[valueString autorelease];
|
|
|
|
// Cache the value in our localCache
|
|
|
|
[localCache setObject: valueString forKey: keyName];
|
|
|
|
}
|
|
|
|
}
|
2009-02-24 02:54:59 +01:00
|
|
|
}
|
2009-11-19 18:08:47 +01:00
|
|
|
else
|
|
|
|
[self errorWithFormat: @"attempting to retrieved cached value for key"
|
|
|
|
@" '%@' while no handle exists", keyName];
|
2008-11-20 20:17:59 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
return valueString;
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (void) setUserAttributes: (NSString *) theAttributes
|
|
|
|
forLogin: (NSString *) login
|
2008-11-11 01:17:35 +01:00
|
|
|
{
|
2009-11-19 18:08:47 +01:00
|
|
|
[self _cacheValues: theAttributes ofType: @"attributes"
|
|
|
|
forLogin: login];
|
|
|
|
}
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (NSString *) userAttributesForLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
return [self _valuesOfType: @"attributes" forLogin: theLogin];
|
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (void) setUserDefaults: (NSString *) theAttributes
|
|
|
|
forLogin: (NSString *) login
|
|
|
|
{
|
|
|
|
[self _cacheValues: theAttributes ofType: @"defaults"
|
|
|
|
forLogin: login];
|
|
|
|
}
|
2008-11-11 02:52:56 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (NSString *) userDefaultsForLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
return [self _valuesOfType: @"defaults" forLogin: theLogin];
|
2009-08-03 17:46:02 +02:00
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (void) setUserSettings: (NSString *) theAttributes
|
|
|
|
forLogin: (NSString *) login
|
2009-08-03 17:46:02 +02:00
|
|
|
{
|
2009-11-19 18:08:47 +01:00
|
|
|
[self _cacheValues: theAttributes ofType: @"settings"
|
|
|
|
forLogin: login];
|
2009-08-03 17:46:02 +02:00
|
|
|
}
|
|
|
|
|
2009-11-19 18:08:47 +01:00
|
|
|
- (NSString *) userSettingsForLogin: (NSString *) theLogin
|
2009-08-03 17:46:02 +02:00
|
|
|
{
|
2009-11-19 18:08:47 +01:00
|
|
|
return [self _valuesOfType: @"settings" forLogin: theLogin];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|