mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-25 01:31:08 +00:00
tweak NSCache changes
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@28582 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
c00f54bafb
commit
6bbe31c18d
3 changed files with 309 additions and 185 deletions
15
ChangeLog
15
ChangeLog
|
@ -1,5 +1,18 @@
|
||||||
2009-08-31 David Chisnall <csdavec@swan.ac.uk>
|
2009-09-01 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
|
* Headers/Foundation/NSCache.h:
|
||||||
|
* Source/NSCache.m:
|
||||||
|
Great additions... tweaked with whitespace changes to conform to
|
||||||
|
coding standards, addition of copyright headers, addition of standard
|
||||||
|
prevention of multiple inclusion, include changes to build independant
|
||||||
|
of any existing installation.
|
||||||
|
|
||||||
|
2009-08-31 David Chisnall <csdavec@swan.ac.uk>
|
||||||
|
|
||||||
|
* Headers/Foundation/NSCache.h:
|
||||||
|
* Headers/Foundation/Foundation.h:
|
||||||
|
* Source/NSCache.m:
|
||||||
|
* Source/GNUmakefile:
|
||||||
Added NSCache implementation.
|
Added NSCache implementation.
|
||||||
|
|
||||||
2009-08-31 Richard Frith-Macdonald <rfm@gnu.org>
|
2009-08-31 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
|
@ -1,104 +1,152 @@
|
||||||
|
/* Interface for NSCache for GNUStep
|
||||||
|
Copyright (C) 2009 Free Software Foundation, Inc.
|
||||||
|
|
||||||
|
Written by: David Chisnall <csdavec@swan.ac.uk>
|
||||||
|
Created: 2009
|
||||||
|
|
||||||
|
This file is part of the GNUstep Base 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 2 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
|
||||||
|
Library 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., 51 Franklin Street, Fifth Floor,
|
||||||
|
Boston, MA 02111 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __NSCache_h_GNUSTEP_BASE_INCLUDE
|
||||||
|
#define __NSCache_h_GNUSTEP_BASE_INCLUDE
|
||||||
|
#import <GNUstepBase/GSVersionMacros.h>
|
||||||
|
|
||||||
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
|
#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
|
||||||
|
|
||||||
#import <Foundation/NSObjCRuntime.h>
|
#import <Foundation/NSObject.h>
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
@class NSString;
|
@class NSString;
|
||||||
@class NSMutableDictionary;
|
@class NSMutableDictionary;
|
||||||
@class NSMutableArray;
|
@class NSMutableArray;
|
||||||
|
|
||||||
#ifndef __has_feature
|
#ifndef __has_feature
|
||||||
#define __has_feature(x) 0
|
#define __has_feature(x) 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@interface NSCache : NSObject
|
@interface NSCache : NSObject
|
||||||
{
|
{
|
||||||
#if !__has_feature(objc_nonfragile_abi) || defined(EXPOSE_GSCACHE_IVARS)
|
#if !__has_feature(objc_nonfragile_abi) || defined(EXPOSE_GSCACHE_IVARS)
|
||||||
@private
|
@private
|
||||||
/** The maximum total cost of all cache objects. */
|
/** The maximum total cost of all cache objects. */
|
||||||
NSUInteger _costLimit;
|
NSUInteger _costLimit;
|
||||||
/** Total cost of currently-stored objects. */
|
/** Total cost of currently-stored objects. */
|
||||||
NSUInteger _totalCost;
|
NSUInteger _totalCost;
|
||||||
/** The maximum number of objects in the cache. */
|
/** The maximum number of objects in the cache. */
|
||||||
NSUInteger _countLimit;
|
NSUInteger _countLimit;
|
||||||
/** The delegate object, notified when objects are about to be evicted. */
|
/** The delegate object, notified when objects are about to be evicted. */
|
||||||
id _delegate;
|
id _delegate;
|
||||||
/** Flag indicating whether discarded objects should be evicted */
|
/** Flag indicating whether discarded objects should be evicted */
|
||||||
BOOL _evictsObjectsWithDiscardedContent;
|
BOOL _evictsObjectsWithDiscardedContent;
|
||||||
/** Name of this cache. */
|
/** Name of this cache. */
|
||||||
NSString *_name;
|
NSString *_name;
|
||||||
/** The mapping from names to objects in this cache. */
|
/** The mapping from names to objects in this cache. */
|
||||||
NSMutableDictionary *_objects;
|
NSMutableDictionary *_objects;
|
||||||
/** LRU ordering of all potentially-evictable objects in this cache. */
|
/** LRU ordering of all potentially-evictable objects in this cache. */
|
||||||
NSMutableArray *_accesses;
|
NSMutableArray *_accesses;
|
||||||
/** Total number of accesses to objects */
|
/** Total number of accesses to objects */
|
||||||
int64_t _totalAccesses;
|
int64_t _totalAccesses;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Returns the maximum number of objects that are supported by this cache.
|
* Returns the maximum number of objects that are supported by this cache.
|
||||||
*/
|
*/
|
||||||
- (NSUInteger)countLimit;
|
- (NSUInteger) countLimit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the cache's delegate.
|
* Returns the cache's delegate.
|
||||||
*/
|
*/
|
||||||
- (id)delegate;
|
- (id) delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether objects stored in this cache which implement the
|
* Returns whether objects stored in this cache which implement the
|
||||||
* NSDiscardableContent protocol are removed from the cache when their contents
|
* NSDiscardableContent protocol are removed from the cache when their contents
|
||||||
* are evicted.
|
* are evicted.
|
||||||
*/
|
*/
|
||||||
- (BOOL)evictsObjectsWithDiscardedContent;
|
- (BOOL) evictsObjectsWithDiscardedContent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the name associated with this cache.
|
* Returns the name associated with this cache.
|
||||||
*/
|
*/
|
||||||
- (NSString*)name;
|
- (NSString*) name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an object associated with the specified key in this cache.
|
* Returns an object associated with the specified key in this cache.
|
||||||
*/
|
*/
|
||||||
- (id)objectForKey: (id)key;
|
- (id) objectForKey: (id)key;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all objects from this cache.
|
* Removes all objects from this cache.
|
||||||
*/
|
*/
|
||||||
- (void)removeAllObjects;
|
- (void) removeAllObjects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the object associated with the given key.
|
* Removes the object associated with the given key.
|
||||||
*/
|
*/
|
||||||
- (void)removeObjectForKey: (id)key;
|
- (void) removeObjectForKey: (id)key;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the maximum number of objects permitted in this cache. This limit is
|
* Sets the maximum number of objects permitted in this cache. This limit is
|
||||||
* advisory; caches may choose to disregard it temporarily or permanently. A
|
* advisory; caches may choose to disregard it temporarily or permanently. A
|
||||||
* limit of 0 is used to indicate no limit; this is the default.
|
* limit of 0 is used to indicate no limit; this is the default.
|
||||||
*/
|
*/
|
||||||
- (void)setCountLimit: (NSUInteger)lim;;
|
- (void) setCountLimit: (NSUInteger)lim;;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the delegate for this cache. The delegate will be notified when an
|
* Sets the delegate for this cache. The delegate will be notified when an
|
||||||
* object is being evicted or removed from the cache.
|
* object is being evicted or removed from the cache.
|
||||||
*/
|
*/
|
||||||
- (void)setDelegate:(id)del;
|
- (void) setDelegate: (id)del;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets whether this cache will evict objects that conform to the
|
* Sets whether this cache will evict objects that conform to the
|
||||||
* NSDiscardableContent protocol, or simply discard their contents.
|
* NSDiscardableContent protocol, or simply discard their contents.
|
||||||
*/
|
*/
|
||||||
- (void)setEvictsObjectsWithDiscardedContent:(BOOL)b;
|
- (void) setEvictsObjectsWithDiscardedContent: (BOOL)b;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the name for this cache.
|
* Sets the name for this cache.
|
||||||
*/
|
*/
|
||||||
- (void)setName: (NSString*)cacheName;
|
- (void) setName: (NSString*)cacheName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an object and its associated cost. The cache will endeavor to keep the
|
* Adds an object and its associated cost. The cache will endeavor to keep the
|
||||||
* total cost below the value set with -setTotalCostLimit: by discarding the
|
* total cost below the value set with -setTotalCostLimit: by discarding the
|
||||||
* contents of objects which implement the NSDiscardableContent protocol.
|
* contents of objects which implement the NSDiscardableContent protocol.
|
||||||
*/
|
*/
|
||||||
- (void)setObject: (id)obj forKey: (id)key cost: (NSUInteger)num;
|
- (void) setObject: (id)obj forKey: (id)key cost: (NSUInteger)num;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an object to the cache without associating a cost with it.
|
* Adds an object to the cache without associating a cost with it.
|
||||||
*/
|
*/
|
||||||
- (void)setObject: (id)obj forKey: (id)key;
|
- (void) setObject: (id)obj forKey: (id)key;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the maximum total cost for objects stored in this cache. This limit is
|
* Sets the maximum total cost for objects stored in this cache. This limit is
|
||||||
* advisory; caches may choose to disregard it temporarily or permanently. A
|
* advisory; caches may choose to disregard it temporarily or permanently. A
|
||||||
* limit of 0 is used to indicate no limit; this is the default.
|
* limit of 0 is used to indicate no limit; this is the default.
|
||||||
*/
|
*/
|
||||||
- (void)setTotalCostLimit: (NSUInteger)lim;
|
- (void) setTotalCostLimit: (NSUInteger)lim;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Protocol implemented by NSCache delegate objects.
|
* Protocol implemented by NSCache delegate objects.
|
||||||
*/
|
*/
|
||||||
|
@ -107,6 +155,13 @@
|
||||||
* Delegate method, called just before the cache removes an object, either as
|
* Delegate method, called just before the cache removes an object, either as
|
||||||
* the result of user action or due to the cache becoming full.
|
* the result of user action or due to the cache becoming full.
|
||||||
*/
|
*/
|
||||||
- (void)cache: (NSCache*)cache willEvictObject: (id)obj;
|
- (void) cache: (NSCache*)cache willEvictObject: (id)obj;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif //OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
|
#endif //OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST)
|
||||||
|
|
||||||
|
#endif /* __NSCache_h_GNUSTEP_BASE_INCLUDE */
|
||||||
|
|
354
Source/NSCache.m
354
Source/NSCache.m
|
@ -1,5 +1,36 @@
|
||||||
|
/* Implementation for NSCache for GNUStep
|
||||||
|
Copyright (C) 2009 Free Software Foundation, Inc.
|
||||||
|
|
||||||
|
Written by: David Chisnall <csdavec@swan.ac.uk>
|
||||||
|
Created: 2009
|
||||||
|
|
||||||
|
This file is part of the GNUstep Base 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 2 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
|
||||||
|
Library 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., 51 Franklin Street, Fifth Floor,
|
||||||
|
Boston, MA 02111 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#import "config.h"
|
||||||
|
|
||||||
#define EXPOSE_GSCACHE_IVARS 1
|
#define EXPOSE_GSCACHE_IVARS 1
|
||||||
#import <Foundation/Foundation.h>
|
|
||||||
|
#import "Foundation/NSArray.h"
|
||||||
|
#import "Foundation/NSCache.h"
|
||||||
|
#import "Foundation/NSDictionary.h"
|
||||||
|
#import "Foundation/NSEnumerator.h"
|
||||||
|
#import "Foundation/NSString.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* _GSCachedObject is effectively used as a structure containing the various
|
* _GSCachedObject is effectively used as a structure containing the various
|
||||||
|
@ -9,134 +40,157 @@
|
||||||
*/
|
*/
|
||||||
@interface _GSCachedObject : NSObject
|
@interface _GSCachedObject : NSObject
|
||||||
{
|
{
|
||||||
@public
|
@public
|
||||||
id object;
|
id object;
|
||||||
NSString *key;
|
NSString *key;
|
||||||
int accessCount;
|
int accessCount;
|
||||||
NSUInteger cost;
|
NSUInteger cost;
|
||||||
BOOL isEvictable;
|
BOOL isEvictable;
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface NSCache (EvictionPolicy)
|
@interface NSCache (EvictionPolicy)
|
||||||
/** The method controlling eviction policy in an NSCache. */
|
/** The method controlling eviction policy in an NSCache. */
|
||||||
- (void)_evictObjectsToMakeSpaceForObjectWithCost: (NSUInteger)cost;
|
- (void) _evictObjectsToMakeSpaceForObjectWithCost: (NSUInteger)cost;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation NSCache
|
@implementation NSCache
|
||||||
- (id)init
|
- (id) init
|
||||||
{
|
{
|
||||||
if (nil == (self = [super init])) { return nil; }
|
if (nil == (self = [super init]))
|
||||||
_objects = [NSMutableDictionary new];
|
{
|
||||||
_accesses = [NSMutableArray new];
|
return nil;
|
||||||
return self;
|
}
|
||||||
|
_objects = [NSMutableDictionary new];
|
||||||
|
_accesses = [NSMutableArray new];
|
||||||
|
return self;
|
||||||
}
|
}
|
||||||
- (NSUInteger)countLimit
|
|
||||||
|
- (NSUInteger) countLimit
|
||||||
{
|
{
|
||||||
return _countLimit;
|
return _countLimit;
|
||||||
}
|
}
|
||||||
- (id)delegate
|
|
||||||
|
- (id) delegate
|
||||||
{
|
{
|
||||||
return _delegate;
|
return _delegate;
|
||||||
}
|
}
|
||||||
- (BOOL)evictsObjectsWithDiscardedContent
|
|
||||||
|
- (BOOL) evictsObjectsWithDiscardedContent
|
||||||
{
|
{
|
||||||
return _evictsObjectsWithDiscardedContent;
|
return _evictsObjectsWithDiscardedContent;
|
||||||
}
|
}
|
||||||
- (NSString*)name
|
|
||||||
|
- (NSString*) name
|
||||||
{
|
{
|
||||||
return _name;
|
return _name;
|
||||||
}
|
}
|
||||||
- (id)objectForKey: (id)key
|
|
||||||
|
- (id) objectForKey: (id)key
|
||||||
{
|
{
|
||||||
_GSCachedObject *obj = [_objects objectForKey: key];
|
_GSCachedObject *obj = [_objects objectForKey: key];
|
||||||
if (nil == obj)
|
|
||||||
{
|
if (nil == obj)
|
||||||
return nil;
|
{
|
||||||
}
|
return nil;
|
||||||
if (obj->isEvictable)
|
}
|
||||||
{
|
if (obj->isEvictable)
|
||||||
// Move the object to the end of the access list.
|
{
|
||||||
[_accesses removeObjectIdenticalTo: obj];
|
// Move the object to the end of the access list.
|
||||||
[_accesses addObject: obj];
|
[_accesses removeObjectIdenticalTo: obj];
|
||||||
}
|
[_accesses addObject: obj];
|
||||||
obj->accessCount++;
|
}
|
||||||
_totalAccesses++;
|
obj->accessCount++;
|
||||||
return obj->object;
|
_totalAccesses++;
|
||||||
|
return obj->object;
|
||||||
}
|
}
|
||||||
- (void)removeAllObjects
|
|
||||||
|
- (void) removeAllObjects
|
||||||
{
|
{
|
||||||
NSEnumerator *e = [_objects objectEnumerator];
|
NSEnumerator *e = [_objects objectEnumerator];
|
||||||
_GSCachedObject *obj;
|
_GSCachedObject *obj;
|
||||||
while (nil != (obj = [e nextObject]))
|
|
||||||
{
|
while (nil != (obj = [e nextObject]))
|
||||||
[_delegate cache: self willEvictObject: obj->object];
|
{
|
||||||
}
|
[_delegate cache: self willEvictObject: obj->object];
|
||||||
[_objects removeAllObjects];
|
}
|
||||||
[_accesses removeAllObjects];
|
[_objects removeAllObjects];
|
||||||
_totalAccesses = 0;
|
[_accesses removeAllObjects];
|
||||||
|
_totalAccesses = 0;
|
||||||
}
|
}
|
||||||
- (void)removeObjectForKey: (id)key
|
|
||||||
|
- (void) removeObjectForKey: (id)key
|
||||||
{
|
{
|
||||||
_GSCachedObject *obj = [_objects objectForKey: key];
|
_GSCachedObject *obj = [_objects objectForKey: key];
|
||||||
if (nil != obj)
|
|
||||||
{
|
if (nil != obj)
|
||||||
[_delegate cache: self willEvictObject: obj->object];
|
{
|
||||||
_totalAccesses -= obj->accessCount;
|
[_delegate cache: self willEvictObject: obj->object];
|
||||||
[_objects removeObjectForKey: key];
|
_totalAccesses -= obj->accessCount;
|
||||||
[_accesses removeObjectIdenticalTo: obj];
|
[_objects removeObjectForKey: key];
|
||||||
}
|
[_accesses removeObjectIdenticalTo: obj];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
- (void)setCountLimit: (NSUInteger)lim
|
|
||||||
|
- (void) setCountLimit: (NSUInteger)lim
|
||||||
{
|
{
|
||||||
_countLimit = lim;
|
_countLimit = lim;
|
||||||
}
|
}
|
||||||
- (void)setDelegate:(id)del
|
|
||||||
|
- (void) setDelegate:(id)del
|
||||||
{
|
{
|
||||||
_delegate = del;
|
_delegate = del;
|
||||||
}
|
}
|
||||||
- (void)setEvictsObjectsWithDiscardedContent:(BOOL)b
|
|
||||||
|
- (void) setEvictsObjectsWithDiscardedContent:(BOOL)b
|
||||||
{
|
{
|
||||||
_evictsObjectsWithDiscardedContent = b;
|
_evictsObjectsWithDiscardedContent = b;
|
||||||
}
|
}
|
||||||
- (void)setName: (NSString*)cacheName
|
|
||||||
|
- (void) setName: (NSString*)cacheName
|
||||||
{
|
{
|
||||||
ASSIGN(_name, cacheName);
|
ASSIGN(_name, cacheName);
|
||||||
}
|
}
|
||||||
- (void)setObject: (id)obj forKey: (id)key cost: (NSUInteger)num
|
|
||||||
|
- (void) setObject: (id)obj forKey: (id)key cost: (NSUInteger)num
|
||||||
{
|
{
|
||||||
_GSCachedObject *old = [_objects objectForKey: key];
|
_GSCachedObject *old = [_objects objectForKey: key];
|
||||||
if (nil != old)
|
|
||||||
{
|
if (nil != old)
|
||||||
[self removeObjectForKey: old->key];
|
{
|
||||||
}
|
[self removeObjectForKey: old->key];
|
||||||
[self _evictObjectsToMakeSpaceForObjectWithCost: num];
|
}
|
||||||
_GSCachedObject *new = [_GSCachedObject new];
|
[self _evictObjectsToMakeSpaceForObjectWithCost: num];
|
||||||
// Retained here, released when obj is dealloc'd
|
_GSCachedObject *new = [_GSCachedObject new];
|
||||||
new->object = RETAIN(obj);
|
// Retained here, released when obj is dealloc'd
|
||||||
new->key = RETAIN(key);
|
new->object = RETAIN(obj);
|
||||||
new->cost = num;
|
new->key = RETAIN(key);
|
||||||
if ([obj conformsToProtocol: @protocol(NSDiscardableContent)])
|
new->cost = num;
|
||||||
{
|
if ([obj conformsToProtocol: @protocol(NSDiscardableContent)])
|
||||||
new->isEvictable = YES;
|
{
|
||||||
[_accesses addObject: new];
|
new->isEvictable = YES;
|
||||||
}
|
[_accesses addObject: new];
|
||||||
[_objects setObject: new forKey: key];
|
}
|
||||||
RELEASE(obj);
|
[_objects setObject: new forKey: key];
|
||||||
_totalCost += num;
|
RELEASE(obj);
|
||||||
|
_totalCost += num;
|
||||||
}
|
}
|
||||||
- (void)setObject: (id)obj forKey: (id)key
|
|
||||||
|
- (void) setObject: (id)obj forKey: (id)key
|
||||||
{
|
{
|
||||||
[self setObject: obj forKey: key cost: 0];
|
[self setObject: obj forKey: key cost: 0];
|
||||||
}
|
}
|
||||||
- (void)setTotalCostLimit: (NSUInteger)lim
|
|
||||||
|
- (void) setTotalCostLimit: (NSUInteger)lim
|
||||||
{
|
{
|
||||||
_costLimit = lim;
|
_costLimit = lim;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSUInteger)totalCostLimit
|
- (NSUInteger)totalCostLimit
|
||||||
{
|
{
|
||||||
return _costLimit;
|
return _costLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is the one that handles the eviction policy. This
|
* This method is the one that handles the eviction policy. This
|
||||||
* implementation uses a relatively simple LRU/LFU hybrid. The NSCache
|
* implementation uses a relatively simple LRU/LFU hybrid. The NSCache
|
||||||
|
@ -146,82 +200,84 @@
|
||||||
*/
|
*/
|
||||||
- (void)_evictObjectsToMakeSpaceForObjectWithCost: (NSUInteger)cost
|
- (void)_evictObjectsToMakeSpaceForObjectWithCost: (NSUInteger)cost
|
||||||
{
|
{
|
||||||
NSUInteger spaceNeeded = 0;
|
NSUInteger spaceNeeded = 0;
|
||||||
NSUInteger count = [_objects count];
|
NSUInteger count = [_objects count];
|
||||||
|
|
||||||
if (_costLimit > 0 && _totalCost + cost > _costLimit)
|
if (_costLimit > 0 && _totalCost + cost > _costLimit)
|
||||||
|
{
|
||||||
|
spaceNeeded = _totalCost + cost - _costLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only evict if we need the space.
|
||||||
|
if (count > 0 && (spaceNeeded > 0 || count >= _countLimit))
|
||||||
|
{
|
||||||
|
NSMutableArray *evictedKeys = nil;
|
||||||
|
// Round up slightly.
|
||||||
|
NSUInteger averageAccesses = (_totalAccesses / count * 0.2) + 1;
|
||||||
|
NSEnumerator *e = [_accesses objectEnumerator];
|
||||||
|
_GSCachedObject *obj;
|
||||||
|
|
||||||
|
if (_evictsObjectsWithDiscardedContent)
|
||||||
{
|
{
|
||||||
spaceNeeded = _totalCost + cost - _costLimit;
|
evictedKeys = [[NSMutableArray alloc] init];
|
||||||
}
|
}
|
||||||
|
while (nil != (obj = [e nextObject]))
|
||||||
// Only evict if we need the space.
|
|
||||||
if (count > 0 && (spaceNeeded > 0 || count >= _countLimit))
|
|
||||||
{
|
{
|
||||||
NSMutableArray *evictedKeys = nil;
|
// Don't evict frequently accessed objects.
|
||||||
// Round up slightly.
|
if (obj->accessCount < averageAccesses && obj->isEvictable)
|
||||||
NSUInteger averageAccesses = (_totalAccesses / count * 0.2) + 1;
|
{
|
||||||
NSEnumerator *e = [_accesses objectEnumerator];
|
[obj->object discardContentIfPossible];
|
||||||
_GSCachedObject *obj;
|
if ([obj->object isContentDiscarded])
|
||||||
|
{
|
||||||
|
NSUInteger cost = obj->cost;
|
||||||
|
|
||||||
if (_evictsObjectsWithDiscardedContent)
|
// Evicted objects have no cost.
|
||||||
{
|
obj->cost = 0;
|
||||||
evictedKeys = [[NSMutableArray alloc] init];
|
// Don't try evicting this again in future; it's gone already.
|
||||||
}
|
obj->isEvictable = NO;
|
||||||
while (nil != (obj = [e nextObject]))
|
// Remove this object as well as its contents if required
|
||||||
{
|
if (_evictsObjectsWithDiscardedContent)
|
||||||
// Don't evict frequently accessed objects.
|
{
|
||||||
if (obj->accessCount < averageAccesses && obj->isEvictable)
|
[evictedKeys addObject: obj->key];
|
||||||
{
|
}
|
||||||
[obj->object discardContentIfPossible];
|
_totalCost -= cost;
|
||||||
if ([obj->object isContentDiscarded])
|
// If we've freed enough space, give up
|
||||||
{
|
if (cost > spaceNeeded)
|
||||||
NSUInteger cost = obj->cost;
|
{
|
||||||
// Evicted objects have no cost.
|
break;
|
||||||
obj->cost = 0;
|
}
|
||||||
// Don't try evicting this again in future; it's gone already.
|
spaceNeeded -= cost;
|
||||||
obj->isEvictable = NO;
|
|
||||||
// Remove this object as well as its contents if required
|
|
||||||
if (_evictsObjectsWithDiscardedContent)
|
|
||||||
{
|
|
||||||
[evictedKeys addObject: obj->key];
|
|
||||||
}
|
|
||||||
_totalCost -= cost;
|
|
||||||
// If we've freed enough space, give up
|
|
||||||
if (cost > spaceNeeded)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
spaceNeeded -= cost;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Evict all of the objects whose content we have discarded if required
|
|
||||||
if (_evictsObjectsWithDiscardedContent)
|
|
||||||
{
|
|
||||||
NSString *key;
|
|
||||||
e = [evictedKeys objectEnumerator];
|
|
||||||
while (nil != (key = [e nextObject]))
|
|
||||||
{
|
|
||||||
[self removeObjectForKey: key];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// Evict all of the objects whose content we have discarded if required
|
||||||
|
if (_evictsObjectsWithDiscardedContent)
|
||||||
|
{
|
||||||
|
NSString *key;
|
||||||
|
|
||||||
|
e = [evictedKeys objectEnumerator];
|
||||||
|
while (nil != (key = [e nextObject]))
|
||||||
|
{
|
||||||
|
[self removeObjectForKey: key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)dealloc
|
- (void) dealloc
|
||||||
{
|
{
|
||||||
[_name release];
|
[_name release];
|
||||||
[_objects release];
|
[_objects release];
|
||||||
[_accesses release];
|
[_accesses release];
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation _GSCachedObject
|
@implementation _GSCachedObject
|
||||||
- (void)dealloc
|
- (void)dealloc
|
||||||
{
|
{
|
||||||
[object release];
|
[object release];
|
||||||
[key release];
|
[key release];
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Reference in a new issue