2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "EOCacheDataSource.h"
25 #import <EOControl/EOControl.h>
26 #import "EODataSource+NGExtensions.h"
30 @interface EOCacheDataSource(Private)
31 - (void)_registerForSource:(id)_source;
32 - (void)_removeObserverForSource:(id)_source;
36 @implementation EOCacheDataSource
38 - (id)initWithDataSource:(EODataSource *)_ds {
39 if ((self = [super init])) {
40 self->source = [_ds retain];
44 [self _registerForSource:self->source];
50 [self _removeObserverForSource:self->source];
51 [self->timer invalidate];
52 [self->timer release];
53 [self->source release];
54 [self->cache release];
60 - (void)setSource:(EODataSource *)_source {
61 [self _removeObserverForSource:self->source];
62 ASSIGN(self->source, _source);
63 [self _registerForSource:self->source];
67 - (EODataSource *)source {
71 - (void)setTimeout:(NSTimeInterval)_timeout {
72 self->timeout = _timeout;
74 - (NSTimeInterval)timeout {
80 - (NSArray *)fetchObjects {
83 self->_isFetching = YES;
86 if (self->time < [[NSDate date] timeIntervalSinceReferenceDate]) {
87 [self->cache release]; self->cache = nil;
90 if (self->cache == nil) {
92 if (self->timer != nil) {
93 [self->timer invalidate];
94 [self->timer release]; self->timer = nil;
97 self->cache = [[self->source fetchObjects] retain];
99 if (self->timeout > 0) {
101 [[NSDate date] timeIntervalSinceReferenceDate] + self->timeout;
103 self->timer = [NSTimer scheduledTimerWithTimeInterval:self->timeout
105 selector:@selector(clear)
106 userInfo:nil repeats:NO];
107 self->timer = [self->timer retain];
109 PROFILE_CHECKPOINT("cache miss");
112 PROFILE_CHECKPOINT("cache hit");
115 self->_isFetching = NO;
120 - (void)setFetchSpecification:(EOFetchSpecification *)_fetchSpec {
121 [self->source setFetchSpecification:_fetchSpec];
123 - (EOFetchSpecification *)fetchSpecification {
124 return [self->source fetchSpecification];
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%p]: source=%@>",
162 NSStringFromClass([self class]), self,
167 /* private methods */
169 - (void)_registerForSource:(id)_source {
170 static NSNotificationCenter *nc = nil;
172 if (_source != nil) {
174 nc = [[NSNotificationCenter defaultCenter] retain];
176 [nc addObserver:self selector:@selector(_clearCache)
177 name:EODataSourceDidChangeNotification object:_source];
181 - (void)_removeObserverForSource:(id)_source {
182 static NSNotificationCenter *nc = nil;
184 if (_source != nil) {
186 nc = [NSNotificationCenter defaultCenter];
187 [nc removeObserver:self name:EODataSourceDidChangeNotification
193 - (void)_clearCache {
195 NSLog(@"clearing cache (%s)...", self->_isFetching?"fetching":"");
196 if (fgetc(stdin) == 'a')
203 [self->timer invalidate];
204 [self->timer release]; self->timer = nil;
208 [self->cache release]; self->cache = nil;
209 [self postDataSourceChangedNotification];
213 @end /* EOCacheDataSource */