mirror of
https://github.com/gnustep/libs-performance.git
synced 2025-02-21 02:41:01 +00:00
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:
parent
c9ae687c14
commit
06bf80e7ac
2 changed files with 51 additions and 0 deletions
11
GSCache.h
11
GSCache.h
|
@ -140,6 +140,17 @@
|
||||||
*/
|
*/
|
||||||
- (void) purge;
|
- (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 />
|
* Sets the delegate for the receiver.<br />
|
||||||
* The delegate object is not retained.<br />
|
* The delegate object is not retained.<br />
|
||||||
|
|
40
GSCache.m
40
GSCache.m
|
@ -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
|
- (void) setDelegate: (id)anObject
|
||||||
{
|
{
|
||||||
[my->lock lock];
|
[my->lock lock];
|
||||||
|
|
Loading…
Reference in a new issue