2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "EOCacheDataSource.h"
26 #import <EOControl/EOControl.h>
27 #import "EODataSource+NGExtensions.h"
31 @interface EOCacheDataSource(Private)
32 - (void)_registerForSource:(id)_source;
33 - (void)_removeObserverForSource:(id)_source;
37 @implementation EOCacheDataSource
39 - (id)initWithDataSource:(EODataSource *)_ds {
40 if ((self = [super init])) {
41 self->source = [_ds retain];
45 [self _registerForSource:self->source];
51 [self _removeObserverForSource:self->source];
52 [self->timer invalidate];
53 [self->timer release];
54 [self->source release];
55 [self->cache release];
61 - (void)setSource:(EODataSource *)_source {
62 [self _removeObserverForSource:self->source];
63 ASSIGN(self->source, _source);
64 [self _registerForSource:self->source];
68 - (EODataSource *)source {
72 - (void)setTimeout:(NSTimeInterval)_timeout {
73 self->timeout = _timeout;
75 - (NSTimeInterval)timeout {
81 - (NSArray *)fetchObjects {
84 self->_isFetching = YES;
87 if (self->time < [[NSDate date] timeIntervalSinceReferenceDate]) {
88 [self->cache release]; self->cache = nil;
91 if (self->cache == nil) {
93 if (self->timer != nil) {
94 [self->timer invalidate];
95 [self->timer release]; self->timer = nil;
98 self->cache = [[self->source fetchObjects] retain];
100 if (self->timeout > 0) {
102 [[NSDate date] timeIntervalSinceReferenceDate] + self->timeout;
104 self->timer = [NSTimer scheduledTimerWithTimeInterval:self->timeout
106 selector:@selector(clear)
107 userInfo:nil repeats:NO];
108 self->timer = [self->timer retain];
110 PROFILE_CHECKPOINT("cache miss");
113 PROFILE_CHECKPOINT("cache hit");
116 self->_isFetching = NO;
121 - (EOFetchSpecification *)fetchSpecification {
122 return [self->source fetchSpecification];
125 - (void)setFetchSpecification:(EOFetchSpecification *)_fetchSpec {
126 [self->source setFetchSpecification:_fetchSpec];
129 - (void)insertObject:(id)_obj {
131 [self->source insertObject:_obj];
134 - (void)deleteObject:(id)_obj {
136 [self->source deleteObject:_obj];
140 return [self->source createObject];
143 - (void)updateObject:(id)_obj {
144 [self->source updateObject:_obj];
148 - (EOClassDescription *)classDescriptionForObjects {
149 return [[self source] classDescriptionForObjects];
158 - (NSString *)description {
161 fmt = [NSString stringWithFormat:@"<%@[0x%08X]: source=%@>",
162 NSStringFromClass([self class]), self,
167 @end /* EOCacheDataSource */
169 @implementation EOCacheDataSource(Private)
171 - (void)_registerForSource:(id)_source {
172 static NSNotificationCenter *nc = nil;
174 if (_source != nil) {
176 nc = [[NSNotificationCenter defaultCenter] retain];
178 [nc addObserver:self selector:@selector(_clearCache)
179 name:EODataSourceDidChangeNotification object:_source];
183 - (void)_removeObserverForSource:(id)_source {
184 static NSNotificationCenter *nc = nil;
186 if (_source != nil) {
188 nc = [NSNotificationCenter defaultCenter];
189 [nc removeObserver:self name:EODataSourceDidChangeNotification
195 - (void)_clearCache {
197 NSLog(@"clearing cache (%s)...", self->_isFetching?"fetching":"");
198 if (fgetc(stdin) == 'a')
205 [self->timer invalidate];
206 [self->timer release]; self->timer = nil;
210 [self->cache release]; self->cache = nil;
211 [self postDataSourceChangedNotification];
215 @end /* EOCacheDataSource(Private) */