1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
|
/* -*-objc-*- */
/** Implementation of GSCache for GNUStep
Copyright (C) 2005 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: October 2005
This file is part of the Performance Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Date$ $Revision$
*/
#include <inttypes.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSException.h>
#import <Foundation/NSHashTable.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSMapTable.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSString.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSValue.h>
#import "GSCache.h"
#import "GSTicker.h"
#if !defined(GNUSTEP)
#include <objc/objc-class.h>
#if (MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4)
#define class_getInstanceSize(isa) ((struct objc_class *)isa)->instance_size
#endif
#import "NSObject+GSExtensions.h"
#endif
@interface GSCache (Private)
- (void) _useDefaults: (NSNotification*)n;
@end
@interface GSCacheItem : NSObject
{
@public
GSCacheItem *next;
GSCacheItem *prev;
unsigned life;
unsigned warn;
unsigned when;
NSUInteger size;
id key;
id object;
}
+ (GSCacheItem*) newWithObject: (id)anObject forKey: (id)aKey;
@end
@implementation GSCacheItem
+ (GSCacheItem*) newWithObject: (id)anObject forKey: (id)aKey
{
GSCacheItem *i;
i = (GSCacheItem*)NSAllocateObject(self, 0, NSDefaultMallocZone());
i->object = [anObject retain];
i->key = [aKey copy];
return i;
}
- (void) dealloc
{
[key release];
[object release];
[super dealloc];
}
- (NSUInteger) sizeInBytesExcluding: (NSHashTable*)exclude
{
NSUInteger bytes = [super sizeInBytesExcluding: exclude];
if (bytes > 0)
{
bytes += [key sizeInBytesExcluding: exclude];
bytes += [object sizeInBytesExcluding: exclude];
}
return bytes;
}
@end
@implementation GSCache
static NSHashTable *allCaches = 0;
static NSRecursiveLock *allCachesLock = nil;
static int itemOffset = 0;
typedef struct {
id delegate;
void (*refresh)(id, SEL, id, id, unsigned, unsigned);
BOOL (*replace)(id, SEL, id, id, unsigned, unsigned);
unsigned currentObjects;
NSUInteger currentSize;
unsigned lifetime;
unsigned maxObjects;
NSUInteger maxSize;
unsigned hits;
unsigned misses;
NSMapTable *contents;
GSCacheItem *first;
NSString *name;
NSHashTable *exclude;
NSRecursiveLock *lock;
BOOL useDefaults;
} Item;
#define my ((Item*)((void*)self + itemOffset))
/*
* Add item to linked list starting at *first
*/
static void appendItem(GSCacheItem *item, GSCacheItem **first)
{
if (*first == nil)
{
item->next = item->prev = item;
*first = item;
}
else
{
(*first)->prev->next = item;
item->prev = (*first)->prev;
(*first)->prev = item;
item->next = *first;
}
}
/*
* Remove item from linked list starting at *first
*/
static void removeItem(GSCacheItem *item, GSCacheItem **first)
{
if (*first == item)
{
if (item->next == item)
{
*first = nil;
}
else
{
*first = item->next;
}
}
item->next->prev = item->prev;
item->prev->next = item->next;
item->prev = item->next = item;
}
+ (NSArray*) allInstances
{
NSArray *a;
[allCachesLock lock];
a = NSAllHashTableObjects(allCaches);
[allCachesLock unlock];
return a;
}
+ (id) alloc
{
return [self allocWithZone: NSDefaultMallocZone()];
}
+ (id) allocWithZone: (NSZone*)z
{
GSCache *c;
c = (GSCache*)NSAllocateObject(self, sizeof(Item), z);
return c;
}
+ (NSString*) description
{
NSMutableString *ms;
NSHashEnumerator e;
GSCache *c;
ms = [NSMutableString stringWithString: [super description]];
[allCachesLock lock];
e = NSEnumerateHashTable(allCaches);
while ((c = (GSCache*)NSNextHashEnumeratorItem(&e)) != nil)
{
[ms appendFormat: @"\n%@", [c description]];
}
NSEndHashTableEnumeration(&e);
[allCachesLock unlock];
return ms;
}
+ (void) initialize
{
if (allCaches == 0)
{
itemOffset = class_getInstanceSize(self);
allCaches = NSCreateHashTable(NSNonRetainedObjectHashCallBacks, 0);
GSTickerTimeNow();
}
}
- (unsigned) currentObjects
{
return my->currentObjects;
}
- (NSUInteger) currentSize
{
return my->currentSize;
}
- (void) dealloc
{
if (my->useDefaults)
{
[[NSNotificationCenter defaultCenter] removeObserver: self
name: NSUserDefaultsDidChangeNotification object: nil];
}
[allCachesLock lock];
NSHashRemove(allCaches, (void*)self);
[allCachesLock unlock];
if (my->contents != 0)
{
[self shrinkObjects: 0 andSize: 0];
NSFreeMapTable(my->contents);
}
[my->exclude release];
[my->name release];
[my->lock release];
[super dealloc];
}
- (id) delegate
{
return my->delegate;
}
- (NSString*) description
{
NSString *n;
[my->lock lock];
n = my->name;
if (n == nil)
{
n = [super description];
}
n = [NSString stringWithFormat:
@" %@\n"
@" Items: %u(%u)\n"
@" Size: %"PRIuPTR"(%"PRIuPTR")\n"
@" Life: %u\n"
@" Hit: %u\n"
@" Miss: %u\n",
n,
my->currentObjects, my->maxObjects,
my->currentSize, my->maxSize,
my->lifetime,
my->hits,
my->misses];
[my->lock unlock];
return n;
}
- (id) init
{
if (nil != (self = [super init]))
{
my->lock = [NSRecursiveLock new];
my->contents = NSCreateMapTable(NSObjectMapKeyCallBacks,
NSObjectMapValueCallBacks, 0);
[allCachesLock lock];
NSHashInsert(allCaches, (void*)self);
[allCachesLock unlock];
}
return self;
}
- (unsigned) lifetime
{
return my->lifetime;
}
- (unsigned) maxObjects
{
return my->maxObjects;
}
- (NSUInteger) maxSize
{
return my->maxSize;
}
- (NSString*) name
{
NSString *n;
[my->lock lock];
n = [my->name retain];
[my->lock unlock];
return [n autorelease];
}
- (id) objectForKey: (id)aKey
{
id object;
GSCacheItem *item;
unsigned when = GSTickerTimeTick();
[my->lock lock];
item = (GSCacheItem*)NSMapGet(my->contents, aKey);
if (item == nil)
{
my->misses++;
[my->lock unlock];
return nil;
}
if (item->when > 0 && item->when < when)
{
BOOL keep = NO;
if (0 != my->replace)
{
GSCacheItem *orig = [item retain];
[my->lock unlock];
NS_DURING
{
keep = (*(my->replace))(my->delegate,
@selector(shouldKeepItem:withKey:lifetime:after:),
item->object,
aKey,
item->life,
when - item->when);
}
NS_HANDLER
{
[my->lock unlock];
[localException raise];
}
NS_ENDHANDLER
[my->lock lock];
if (keep == YES)
{
GSCacheItem *current;
/* Refetch in case delegate changed it.
*/
current = (GSCacheItem*)NSMapGet(my->contents, aKey);
if (current == nil)
{
/* Delegate must have deleted the item even though
* it returned YES to say we should keep it ...
* we count this as a miss.
*/
my->misses++;
[my->lock unlock];
[orig release];
return nil;
}
else if (orig == current)
{
/* Delegate told us to keep the original item so we
* update its expiry time.
*/
item->when = when + item->life;
item->warn = when + item->life / 2;
}
else
{
/* Delegate replaced the item with another and told
* us to keep that one.
*/
item = current;
}
}
[orig release];
}
if (keep == NO)
{
removeItem(item, &my->first);
my->currentObjects--;
if (my->maxSize > 0)
{
my->currentSize -= item->size;
}
NSMapRemove(my->contents, (void*)item->key);
my->misses++;
[my->lock unlock];
return nil; // Lifetime expired.
}
}
else if (item->warn > 0 && item->warn < when)
{
item->warn = 0; // Don't warn again.
if (0 != my->refresh)
{
GSCacheItem *orig = [item retain];
GSCacheItem *current;
[my->lock unlock];
NS_DURING
{
(*(my->refresh))(my->delegate,
@selector(mayRefreshItem:withKey:lifetime:after:),
item->object,
aKey,
item->life,
when - item->when);
}
NS_HANDLER
{
[my->lock unlock];
[localException raise];
}
NS_ENDHANDLER
[my->lock lock];
/* Refetch in case delegate changed it.
*/
current = (GSCacheItem*)NSMapGet(my->contents, aKey);
if (current == nil)
{
/* Delegate must have deleted the item!
* So we count this as a miss.
*/
my->misses++;
[my->lock unlock];
[orig release];
return nil;
}
else
{
item = current;
}
[orig release];
}
}
// Least recently used ... move to end of list.
removeItem(item, &my->first);
appendItem(item, &my->first);
my->hits++;
object = [item->object retain];
[my->lock unlock];
return [object autorelease];
}
- (void) purge
{
if (my->contents != 0)
{
unsigned when = GSTickerTimeTick();
NSMapEnumerator e;
GSCacheItem *i;
id k;
[my->lock lock];
e = NSEnumerateMapTable(my->contents);
while (NSNextMapEnumeratorPair(&e, (void**)&k, (void**)&i) != 0)
{
if (i->when > 0 && i->when < when)
{
removeItem(i, &my->first);
my->currentObjects--;
if (my->maxSize > 0)
{
my->currentSize -= i->size;
}
NSMapRemove(my->contents, (void*)i->key);
}
}
NSEndMapTableEnumeration(&e);
[my->lock unlock];
}
}
- (oneway void) release
{
/* We lock the table while checking, to prevent
* another thread from grabbing this object while we are
* checking it.
* If we are going to deallocate the object, we first remove
* it from the table so that no other thread will find it
* and try to use it while it is being deallocated.
*/
[allCachesLock lock];
if (NSDecrementExtraRefCountWasZero(self))
{
NSHashRemove(allCaches, (void*)self);
[allCachesLock unlock];
[self dealloc];
}
else
{
[allCachesLock unlock];
}
}
- (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];
my->delegate = anObject;
if ([my->delegate respondsToSelector:
@selector(shouldKeepItem:withKey:lifetime:after:)])
{
my->replace = (BOOL (*)(id,SEL,id,id,unsigned,unsigned))
[my->delegate methodForSelector:
@selector(shouldKeepItem:withKey:lifetime:after:)];
}
else
{
my->replace = 0;
}
if ([my->delegate respondsToSelector:
@selector(mayRefreshItem:withKey:lifetime:after:)])
{
my->refresh = (void (*)(id,SEL,id,id,unsigned,unsigned))
[my->delegate methodForSelector:
@selector(mayRefreshItem:withKey:lifetime:after:)];
}
else
{
my->refresh = 0;
}
[my->lock unlock];
}
- (void) setLifetime: (unsigned)max
{
[my->lock lock];
if (YES == my->useDefaults)
{
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSString *n = (nil == my->name) ? @"" : my->name;
NSString *k = [@"GSCacheLifetime" stringByAppendingString: n];
if (nil != [defs objectForKey: k])
{
max = (unsigned) [defs integerForKey: k];
}
}
my->lifetime = max;
[my->lock unlock];
}
- (void) setMaxObjects: (unsigned)max
{
[my->lock lock];
if (YES == my->useDefaults)
{
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSString *n = (nil == my->name) ? @"" : my->name;
NSString *k = [@"GSCacheMaxObjects" stringByAppendingString: n];
if (nil != [defs objectForKey: k])
{
max = (unsigned) [defs integerForKey: k];
}
}
my->maxObjects = max;
if (my->currentObjects > my->maxObjects)
{
[self shrinkObjects: my->maxObjects
andSize: my->maxSize];
}
[my->lock unlock];
}
- (void) setMaxSize: (NSUInteger)max
{
[my->lock lock];
if (YES == my->useDefaults)
{
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSString *n = (nil == my->name) ? @"" : my->name;
NSString *k = [@"GSCacheMaxSize" stringByAppendingString: n];
if (nil != [defs objectForKey: k])
{
max = (NSUInteger) [defs integerForKey: k];
}
}
if (max > 0 && my->maxSize == 0)
{
NSMapEnumerator e = NSEnumerateMapTable(my->contents);
GSCacheItem *i;
id k;
NSUInteger size = 0;
if (nil == my->exclude)
{
my->exclude
= NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
}
while (NSNextMapEnumeratorPair(&e, (void**)&k, (void**)&i) != 0)
{
if (i->size == 0)
{
i->size = [i->object sizeInBytesExcluding: my->exclude];
[my->exclude removeAllObjects];
}
if (i->size > max)
{
/*
* Item in cache is too big for new size limit ...
* Remove it.
*/
removeItem(i, &my->first);
NSMapRemove(my->contents, (void*)i->key);
my->currentObjects--;
continue;
}
size += i->size;
}
NSEndMapTableEnumeration(&e);
my->currentSize = size;
}
else if (max == 0)
{
my->currentSize = 0;
}
my->maxSize = max;
if (my->currentSize > my->maxSize)
{
[self shrinkObjects: my->maxObjects
andSize: my->maxSize];
}
[my->lock unlock];
}
- (void) setName: (NSString*)name forConfiguration: (BOOL)useDefaults
{
NSString *c;
[my->lock lock];
c = [name copy];
[my->name release];
my->name = c;
useDefaults = (useDefaults ? YES : NO); // Make sure this is a real bool
if (my->useDefaults != useDefaults)
{
if (my->useDefaults)
{
[[NSNotificationCenter defaultCenter] removeObserver: self
name: NSUserDefaultsDidChangeNotification object: nil];
}
my->useDefaults = useDefaults;
if (my->useDefaults)
{
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_useDefaults:)
name: NSUserDefaultsDidChangeNotification
object: nil];
[self _useDefaults: nil];
}
}
[my->lock unlock];
}
- (void) setName: (NSString*)name
{
[self setName: name forConfiguration: NO];
}
- (void) setObject: (id)anObject forKey: (id)aKey
{
[self setObject: anObject forKey: aKey lifetime: my->lifetime];
}
- (void) setObject: (id)anObject
forKey: (id)aKey
lifetime: (unsigned)lifetime
{
GSCacheItem *item;
unsigned maxObjects;
NSUInteger maxSize;
unsigned addObjects = (anObject == nil ? 0 : 1);
NSUInteger addSize = 0;
[my->lock lock];
maxObjects = my->maxObjects;
maxSize = my->maxSize;
item = (GSCacheItem*)NSMapGet(my->contents, aKey);
if (item != nil)
{
removeItem(item, &my->first);
my->currentObjects--;
if (my->maxSize > 0)
{
my->currentSize -= item->size;
}
NSMapRemove(my->contents, (void*)aKey);
}
if (addObjects > 0 && (maxSize > 0 || maxObjects > 0))
{
if (maxSize > 0)
{
if (nil == my->exclude)
{
my->exclude
= NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
}
addSize = [anObject sizeInBytesExcluding: my->exclude];
[my->exclude removeAllObjects];
if (addSize > maxSize)
{
addObjects = 0; // Object too big to cache.
}
}
}
if (addObjects > 0)
{
/*
* Make room for new object.
*/
[self shrinkObjects: maxObjects - addObjects
andSize: maxSize - addSize];
item = [GSCacheItem newWithObject: anObject forKey: aKey];
if (lifetime > 0)
{
unsigned tick = GSTickerTimeTick();
item->when = tick + lifetime;
item->warn = tick + lifetime / 2;
}
item->life = lifetime;
item->size = addSize;
NSMapInsert(my->contents, (void*)item->key, (void*)item);
appendItem(item, &my->first);
my->currentObjects += addObjects;
my->currentSize += addSize;
[item release];
}
[my->lock unlock];
}
- (void) setObject: (id)anObject
forKey: (id)aKey
until: (NSDate*)expires
{
NSTimeInterval i;
i = (expires == nil) ? 0.0 : [expires timeIntervalSinceReferenceDate];
i -= GSTickerTimeNow();
if (i <= 0.0)
{
[self setObject: nil forKey: aKey]; // Already expired
}
else
{
unsigned limit;
if (i > 2415919103.0)
{
limit = 0; // Limit in far future.
}
else
{
limit = (unsigned)i;
}
[self setObject: anObject
forKey: aKey
lifetime: limit];
}
}
- (void) shrinkObjects: (unsigned)objects andSize: (NSUInteger)size
{
NSUInteger newSize;
unsigned newObjects;
[my->lock lock];
newSize = [self currentSize];
newObjects = [self currentObjects];
if (newObjects > objects || (my->maxSize > 0 && newSize > size))
{
[self purge];
newSize = [self currentSize];
newObjects = [self currentObjects];
while (newObjects > objects || (my->maxSize > 0 && newSize > size))
{
GSCacheItem *item;
item = my->first;
removeItem(item, &my->first);
newObjects--;
if (my->maxSize > 0)
{
newSize -= item->size;
}
NSMapRemove(my->contents, (void*)item->key);
}
my->currentObjects = newObjects;
my->currentSize = newSize;
}
[my->lock unlock];
}
- (NSUInteger) sizeInBytesExcluding: (NSHashTable*)exclude
{
NSUInteger size;
[my->lock lock];
size = [super sizeInBytesExcluding: exclude];
if (size > 0)
{
size += sizeof(Item)
+ [my->contents sizeInBytesExcluding: exclude]
+ [my->exclude sizeInBytesExcluding: exclude]
+ [my->name sizeInBytesExcluding: exclude]
+ [my->lock sizeInBytesExcluding: exclude];
}
[my->lock unlock];
return size;
}
@end
@implementation GSCache (Private)
- (void) _useDefaults: (NSNotification*)n
{
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSString *conf = (nil == my->name ? @"" : my->name);
NSString *key;
[my->lock lock];
NS_DURING
{
if (YES == my->useDefaults)
{
my->useDefaults = NO;
key = [@"GSCacheLifetime" stringByAppendingString: conf];
if (nil != [defs objectForKey: key])
{
[self setLifetime: (unsigned)[defs integerForKey: key]];
}
key = [@"GSCacheMaxObjects" stringByAppendingString: conf];
if (nil != [defs objectForKey: key])
{
[self setMaxObjects: (unsigned)[defs integerForKey: key]];
}
key = [@"GSCacheMaxSize" stringByAppendingString: conf];
if (nil != [defs objectForKey: key])
{
[self setMaxSize: (NSUInteger)[defs integerForKey: key]];
}
my->useDefaults = YES;
}
[my->lock unlock];
}
NS_HANDLER
{
my->useDefaults = YES;
[my->lock unlock];
[localException raise];
}
NS_ENDHANDLER
}
@end
|