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>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-11-11 01:17:35 +01:00
|
|
|
#import <Foundation/NSEnumerator.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>
|
|
|
|
#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"
|
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
#import "NSDictionary+BSJSONAdditions.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.
|
2009-02-24 02:54:59 +01:00
|
|
|
static NSTimeInterval cleanupInterval = 300;
|
2008-11-11 02:52:56 +01:00
|
|
|
|
|
|
|
static NSMutableDictionary *cache = nil;
|
|
|
|
static NSMutableDictionary *users = nil;
|
|
|
|
|
2009-10-13 22:17:29 +02: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 caces. We
|
|
|
|
// surely don't want to get new defaults/settings during the _same_ requests, it
|
|
|
|
// could produce relatively strange behaviors
|
|
|
|
static NSMutableDictionary *localCache = nil;
|
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
static NSString *memcachedServerName = @"localhost";
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
static SOGoCache *sharedCache = nil;
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
static NSLock *lock;
|
|
|
|
#endif
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
@implementation SOGoCache
|
|
|
|
|
2008-12-12 19:55:12 +01:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
2009-02-24 02:54:59 +01:00
|
|
|
#if defined(THREADSAFE)
|
2008-12-12 19:55:12 +01:00
|
|
|
lock = [NSLock new];
|
|
|
|
#endif
|
2008-12-15 23:23:40 +01:00
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2008-11-11 02:52:56 +01:00
|
|
|
+ (NSTimeInterval) cleanupInterval
|
|
|
|
{
|
|
|
|
return cleanupInterval;
|
|
|
|
}
|
|
|
|
|
2008-01-16 19:47:20 +01:00
|
|
|
+ (SOGoCache *) sharedCache
|
|
|
|
{
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
if (!sharedCache)
|
2009-08-03 17:46:02 +02:00
|
|
|
sharedCache = [[self alloc] init];
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
|
|
|
|
return sharedCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) killCache
|
|
|
|
{
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
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-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2008-11-11 01:17:35 +01:00
|
|
|
NSString *cleanupSetting;
|
2009-08-03 17:46:02 +02:00
|
|
|
memcached_return error;
|
|
|
|
|
2008-11-12 04:30:13 +01:00
|
|
|
cache = [[NSMutableDictionary alloc] init];
|
|
|
|
users = [[NSMutableDictionary alloc] init];
|
2009-10-13 22:17:29 +02:00
|
|
|
localCache = [[NSMutableDictionary alloc] init];
|
2008-11-12 04:30:13 +01:00
|
|
|
|
2008-11-11 01:17:35 +01:00
|
|
|
// We fire our timer that will cleanup cache entries
|
|
|
|
cleanupSetting = [[NSUserDefaults standardUserDefaults]
|
|
|
|
objectForKey: @"SOGoCacheCleanupInterval"];
|
|
|
|
|
|
|
|
if (cleanupSetting && [cleanupSetting doubleValue] > 0.0)
|
|
|
|
cleanupInterval = [cleanupSetting doubleValue];
|
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
[self logWithFormat: @"Cache cleanup interval set every %f seconds for memcached",
|
2008-11-11 01:17:35 +01:00
|
|
|
cleanupInterval];
|
2009-08-03 17:46:02 +02:00
|
|
|
|
|
|
|
handle = memcached_create(NULL);
|
|
|
|
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
servers = memcached_server_list_append(NULL, [memcachedServerName UTF8String], 11211, &error);
|
|
|
|
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);
|
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-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2008-01-16 20:52:43 +01:00
|
|
|
if (![cache objectForKey: fullPath])
|
|
|
|
{
|
|
|
|
[cache setObject: object forKey: fullPath];
|
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
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
|
|
|
{
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock lock];
|
|
|
|
#endif
|
2009-09-11 16:38:43 +02:00
|
|
|
[users setObject: user forKey: userName];
|
2008-12-12 19:55:12 +01:00
|
|
|
#if defined(THREADSAFE)
|
|
|
|
[lock unlock];
|
|
|
|
#endif
|
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-08-03 17:46:02 +02:00
|
|
|
- (void) cacheValues: (NSDictionary *) theAttributes
|
|
|
|
ofType: (NSString *) theType
|
2009-03-03 20:08:02 +01:00
|
|
|
forLogin: (NSString *) theLogin
|
|
|
|
{
|
2009-08-03 17:46:02 +02:00
|
|
|
memcached_return error;
|
|
|
|
const char *key, *value;
|
|
|
|
unsigned int len, vlen;
|
2009-03-03 20:08:02 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
if (!handle)
|
|
|
|
return;
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
key = [[NSString stringWithFormat: @"%@+%@", theLogin, theType] UTF8String];
|
|
|
|
len = strlen(key);
|
2008-11-12 04:30:13 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
value = [[theAttributes jsonStringValue] UTF8String];
|
|
|
|
vlen = strlen(value);
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
error = memcached_set(handle, key, len, value, vlen, cleanupInterval, 0);
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2009-08-03 17:46:02 +02: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];
|
2009-02-24 02:54:59 +01:00
|
|
|
}
|
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
- (NSDictionary *) _valuesOfType: (NSString *) theType
|
|
|
|
forLogin: (NSString *) theLogin
|
2009-02-24 02:54:59 +01:00
|
|
|
{
|
2009-08-03 17:46:02 +02:00
|
|
|
NSDictionary *d;
|
2009-10-13 22:17:29 +02:00
|
|
|
NSString *k;
|
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
const char *key;
|
2009-10-13 22:17:29 +02:00
|
|
|
unsigned int len;
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
if (!handle)
|
|
|
|
return nil;
|
2009-03-03 20:08:02 +01:00
|
|
|
|
2009-10-13 22:17:29 +02:00
|
|
|
k = [NSString stringWithFormat: @"%@+%@", theLogin, theType];
|
|
|
|
key = [k UTF8String];
|
2009-08-03 17:46:02 +02:00
|
|
|
len = strlen(key);
|
2009-03-24 21:36:49 +01:00
|
|
|
|
2009-10-13 22:17:29 +02:00
|
|
|
d = [localCache objectForKey: k];
|
2009-03-24 21:36:49 +01:00
|
|
|
|
2009-10-13 22:17:29 +02:00
|
|
|
if (!d)
|
2008-12-02 02:36:53 +01:00
|
|
|
{
|
2009-10-13 22:17:29 +02:00
|
|
|
memcached_return rc;
|
|
|
|
unsigned int flags;
|
|
|
|
size_t vlen;
|
|
|
|
char *s;
|
2009-02-24 02:54:59 +01:00
|
|
|
|
2009-10-13 22:17:29 +02:00
|
|
|
s = memcached_get(handle, key, len, &vlen, &flags, &rc);
|
|
|
|
|
|
|
|
if (rc == MEMCACHED_SUCCESS && s)
|
|
|
|
{
|
|
|
|
NSString *v;
|
|
|
|
|
|
|
|
v = [NSString stringWithUTF8String: s];
|
|
|
|
d = [NSDictionary dictionaryWithJSONString: v];
|
|
|
|
//[self logWithFormat: @"read values (%@) for subtype %@ for user %@", [d description], theType, theLogin];
|
|
|
|
|
|
|
|
// Cache the value in our localCache
|
|
|
|
[localCache setObject: d forKey: k];
|
|
|
|
|
|
|
|
free(s);
|
|
|
|
}
|
2009-02-24 02:54:59 +01:00
|
|
|
}
|
2008-11-20 20:17:59 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
return d;
|
2008-11-11 01:17:35 +01:00
|
|
|
}
|
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
|
|
|
|
- (NSMutableDictionary *) userAttributesForLogin: (NSString *) theLogin
|
2008-11-11 01:17:35 +01:00
|
|
|
{
|
2009-08-03 17:46:02 +02:00
|
|
|
id o;
|
2008-11-11 01:17:35 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
o = [self _valuesOfType: @"attributes" forLogin: theLogin];
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
if (o)
|
|
|
|
return [NSMutableDictionary dictionaryWithDictionary: o];
|
2008-11-11 02:52:56 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2008-12-12 19:55:12 +01:00
|
|
|
|
2009-08-03 17:46:02 +02:00
|
|
|
- (NSDictionary *) userDefaultsForLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
return [self _valuesOfType: @"defaults" forLogin: theLogin];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *) userSettingsForLogin: (NSString *) theLogin
|
|
|
|
{
|
|
|
|
return [self _valuesOfType: @"settings" forLogin: theLogin];
|
2008-01-16 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|