From 4993a3b043a1b973fe512f5d411706ddc54be02c Mon Sep 17 00:00:00 2001 From: Manuel Guesdon Date: Mon, 1 Mar 2004 16:52:50 +0000 Subject: [PATCH] Added git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gsweb/trunk@18747 72102866-910b-0410-8b05-ffd578937521 --- GSWExtensionsGSW.framework/GSWCacheElement.h | 51 ++ GSWExtensionsGSW.framework/GSWCacheElement.m | 511 +++++++++++++++++++ 2 files changed, 562 insertions(+) create mode 100644 GSWExtensionsGSW.framework/GSWCacheElement.h create mode 100644 GSWExtensionsGSW.framework/GSWCacheElement.m diff --git a/GSWExtensionsGSW.framework/GSWCacheElement.h b/GSWExtensionsGSW.framework/GSWCacheElement.h new file mode 100644 index 0000000..cd707aa --- /dev/null +++ b/GSWExtensionsGSW.framework/GSWCacheElement.h @@ -0,0 +1,51 @@ +/** GSWCacheElement.h - GSWeb: Class GSWCacheElement + + Copyright (C) 2004 Free Software Foundation, Inc. + + Written by: Manuel Guesdon + Date: Jan 2004 + + $Revision$ + $Date$ + $Id$ + + This file is part of the GNUstep Web Library. + + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +**/ + +#ifndef _GSWCacheElement_h__ + #define _GSWCacheElement_h__ + +//==================================================================== +@interface GSWCacheElement : GSWDynamicElement +{ + GSWHTMLStaticGroup* _childrenGroup; + int _cacheIndex; + GSWAssociation* _cachedObject; + GSWAssociation* _cache; + GSWAssociation* _duration; + NSArray* _keys; + GSWAssociation* _uniqID; + GSWAssociation* _disabled; + GSWAssociation* _enabled; +}; + +@end + + +#endif //_GSWCacheElement_h__ diff --git a/GSWExtensionsGSW.framework/GSWCacheElement.m b/GSWExtensionsGSW.framework/GSWCacheElement.m new file mode 100644 index 0000000..2c2fad9 --- /dev/null +++ b/GSWExtensionsGSW.framework/GSWCacheElement.m @@ -0,0 +1,511 @@ +/** GSWCacheElement.m - GSWeb: Class GSWCacheElement + + Copyright (C) 2004 Free Software Foundation, Inc. + + Written by: Manuel Guesdon + Date: Jan 2004 + + $Revision$ + $Date$ + $Id$ + + This file is part of the GNUstep Web Library. + + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +**/ + +#include "config.h" + +RCS_ID("$Id$") + +#include "GSWExtGSWWOCompatibility.h" +#include "GSWCacheElement.h" + +/** +GSWCacheElement is an object childrens +There's 2 modes: cachedObject or cache. +Bindings + + cachedObject Evaluated with valueInComponent to get cached data , evaluated with setValue:inComponent: te cache data. + + cache Evaluated to get a GSWCache object used to store and retrieve data using key, key0,keyN... + duration Evaluated when puting data in a GSWCache to set cache duration. Not allowed when using 'cachedObject' binding) + uniqID A string unique used to replace contextual data (session id, context and element id when storing data in cache. + it should be unique in children caches. + disabled If evaluated to yes, caching is disabled + + enabled If evaluated to no, caching is disabled + + key,key0,keyN... (key|key0), key1, key2,... are keys which will be used to put data in a GSWCache. You need at list one key (called + key or key0). +**/ + +//==================================================================== +@implementation GSWCacheElement + +//-------------------------------------------------------------------- +-(id)initWithName:(NSString*)aName + associations:(NSDictionary*)associations + contentElements:(NSArray*)elements +{ + LOGObjectFnStart(); + if ((self=[super initWithName:aName + associations:nil + template:nil])) + { + if (elements) + _childrenGroup=[[GSWHTMLStaticGroup alloc]initWithContentElements:elements]; + + _cachedObject = [[associations objectForKey:@"cachedObject" + withDefaultObject:[_cachedObject autorelease]] retain]; + NSDebugMLLog(@"gswdync",@"cachedObject=%@",_cachedObject); + + _cache = [[associations objectForKey:@"cache" + withDefaultObject:[_cache autorelease]] retain]; + NSDebugMLLog(@"gswdync",@"cache=%@",_cache); + + _duration = [[associations objectForKey:@"duration" + withDefaultObject:[_duration autorelease]] retain]; + NSDebugMLLog(@"gswdync",@"duration=%@",_duration); + + _uniqID = [[associations objectForKey:@"uniqID" + withDefaultObject:[_uniqID autorelease]] retain]; + NSDebugMLLog(@"gswdync",@"uniqID=%@",_uniqID); + + _disabled = [[associations objectForKey:disabled__Key + withDefaultObject:[_disabled autorelease]] retain]; + NSDebugMLLog(@"gswdync",@"disabled=%@",_disabled); + + _enabled = [[associations objectForKey:enabled__Key + withDefaultObject:[_enabled autorelease]] retain]; + NSDebugMLLog(@"gswdync",@"enabled=%@",_enabled); + + if (_disabled && _enabled) + { + ExceptionRaise(@"GSWCacheElement",@"You can't specify 'disabled' and 'enabled' together. componentAssociations:%@", + associations); + } + else if (_cachedObject && _cache) + { + ExceptionRaise(@"GSWCacheElement",@"You can't specify 'cachedObject' and 'cache' together. componentAssociations:%@", + associations); + } + else + { + if (_cachedObject) + { + if ([_cachedObject isValueConstant]) + { + ExceptionRaise0(@"GSWCacheElement", + @"'cachedObject' parameter can't be a constant"); + }; + } + else if (_cache) + { + // Get keys associations + int keyIndex=0; + NSMutableArray* keysArray=(NSMutableArray*)[NSMutableArray array]; + GSWAssociation* aKeyAssociation=nil; + do + { + if (keyIndex==0) + { + aKeyAssociation=[associations objectForKey:@"key"]; + if (!aKeyAssociation) + { + aKeyAssociation=[associations objectForKey:@"key0"]; + } + } + else + { + aKeyAssociation=[associations objectForKey:[NSString stringWithFormat:@"key%d",keyIndex]]; + }; + if (aKeyAssociation) + [keysArray addObject:aKeyAssociation]; + keyIndex++; + } + while(aKeyAssociation); + + if ([keysArray count]>0) + ASSIGN(_keys,([NSArray arrayWithArray:keysArray])); + else + { + ExceptionRaise(@"GSWCacheElement",@"You have to define keys (like key=...; key1=...; ... componentAssociations:%@", + associations); + }; + } + else + { + ExceptionRaise(@"GSWCacheElement",@"You have to define 'cachedObject' or 'cache' together. componentAssociations:%@", + associations); + }; + }; + }; + LOGObjectFnStop(); + return self; +}; + +//-------------------------------------------------------------------- +-(id)initWithName:(NSString*)aName + associations:(NSDictionary*)someAssociations + template:(GSWElement*)templateElement +{ + LOGObjectFnStart(); + if ((self=[self initWithName:aName + associations:someAssociations + contentElements:templateElement ? [NSArray arrayWithObject:templateElement] : nil])) + { + }; + LOGObjectFnStop(); + return self; +}; + +//-------------------------------------------------------------------- +-(void)dealloc +{ + DESTROY(_childrenGroup); + DESTROY(_cachedObject); + DESTROY(_cache); + DESTROY(_duration); + DESTROY(_keys); + DESTROY(_uniqID); + DESTROY(_disabled); + DESTROY(_enabled); + [super dealloc]; +} + +//-------------------------------------------------------------------- +-(void)setDefinitionName:(NSString*)definitionName +{ + [super setDefinitionName:definitionName]; + if (definitionName && _childrenGroup) + [_childrenGroup setDefinitionName:[NSString stringWithFormat:@"%@-StaticGroup",definitionName]]; +}; + +//-------------------------------------------------------------------- +-(NSString*)description +{ + return [NSString stringWithFormat:@"<%s %p>", + object_get_class_name(self), + (void*)self]; +}; + +@end + +//==================================================================== +@implementation GSWCacheElement (GSWCacheElementA) + +//-------------------------------------------------------------------- +-(void)appendToResponse:(GSWResponse*)aResponse + inContext:(GSWContext*)aContext +{ + GSWComponent* component=nil; + BOOL isDisabled=NO; +#ifndef NDEBBUG + int elementsNb=[(GSWElementIDString*)[aContext elementID]elementsNb]; +#endif + + LOGObjectFnStart(); + + component=[aContext component]; + + NS_DURING + { + BOOL cacheUsed=NO; + NSString* contextAndElementID=nil; + NSString* elementID=nil; + NSString* uniqID=nil; + NSString* sessionID=nil; + NSString* contextAndElementIDCacheKey=nil; + NSString* elementIDCacheKey=nil; + GSWCache* cache=nil; + int keysCount=[_keys count]; + id keys[keysCount]; + int i=0; + GSWStartElement(aContext); + GSWSaveAppendToResponseElementID(aContext); + + NSLog(@"GSWCacheElement Start Date=%@",[NSDate date]); + + if (_disabled) + isDisabled=[self evaluateCondition:_disabled + inContext:aContext]; + else if (_enabled) + isDisabled=![self evaluateCondition:_enabled + inContext:aContext]; + + uniqID=[_uniqID valueInComponent:component]; + + // Append an element to elementID So all children elementIDs will start with the same prefix + [aContext appendElementIDComponent:[NSString stringWithFormat:@"CacheElement-%@",uniqID]]; + + contextAndElementID=[aContext contextAndElementID]; + NSDebugMLog(@"contextAndElementID=%@",contextAndElementID); + + elementID=[[[aContext elementID] copy]autorelease]; // because elementID is mutable (and varying) + NSDebugMLog(@"elementID=%@",elementID); + + sessionID=[[aContext session] sessionID]; + NSDebugMLog(@"sessionID=%@",sessionID); + + NSDebugMLLog(@"gswdync",@"isDisabled=%d",isDisabled); + + if (!isDisabled) + { + id cachedObject=nil; + if (_cachedObject) + cachedObject=[_cachedObject valueInComponent:component]; + else + { + cache=[_cache valueInComponent:component]; + for(i=0;i