add code to allow refreshing of existing items in cache

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/performance/trunk@40424 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2017-03-28 10:58:51 +00:00
parent c9ae687c14
commit 06bf80e7ac
2 changed files with 51 additions and 0 deletions

View file

@ -140,6 +140,17 @@
*/
- (void) purge;
/**
* Similar to -setObject:forKey:lifetime: but, if there is an existing
* object in the cache which -isEqual: to anObject (or is anObject is nil),
* the existing object is retained in the cache (though its lifetime is
* updated/refreshed).<br />
* The value of the object in the cache is returned.
*/
- (id) refreshObject: (id)anObject
forKey: (id)aKey
lifetime: (unsigned)lifetime;
/**
* Sets the delegate for the receiver.<br />
* The delegate object is not retained.<br />

View file

@ -537,6 +537,46 @@ static void removeItem(GSCacheItem *item, GSCacheItem **first)
}
}
- (id) refreshObject: (id)anObject
forKey: (id)aKey
lifetime: (unsigned)lifetime
{
id object;
GSCacheItem *item;
[my->lock lock];
item = (GSCacheItem*)NSMapGet(my->contents, aKey);
if (item == nil)
{
if (nil != anObject)
{
[self setObject: anObject
forKey: aKey
lifetime: lifetime];
}
[my->lock unlock];
return anObject;
}
if (nil != anObject && NO == [anObject isEqual: item->object])
{
object = [anObject retain];
[item->object release];
item->object = object;
}
if (lifetime > 0)
{
unsigned tick = GSTickerTimeTick();
item->when = tick + lifetime;
item->warn = tick + lifetime / 2;
}
item->life = lifetime;
object = [[item->object retain] autorelease];
[my->lock unlock];
return object;
}
- (void) setDelegate: (id)anObject
{
[my->lock lock];