Next Page > Hide TOC

NSURLCache Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in Mac OS X v10.2 with Safari 1.0 installed.
Available in Mac OS X v10.2.7 and later.
Companion guide
Declared in
NSURLCache.h
Related sample code

Overview

NSURLCache implements the caching of responses to URL load requests by mapping NSURLRequest objects to NSCachedURLResponse objects. It is a composite of an in-memory and an on-disk cache.

Methods are provided to manipulate the sizes of each of these caches as well as to control the path on disk to use for persistent storage of cache data.

Tasks

Getting and Setting Shared Cache

Creating a New Cache Object

Getting and Storing Cached Objects

Removing Cached Objects

Getting and Setting On-disk Cache Properties

Getting and Setting In-memory Cache Properties

Class Methods

setSharedURLCache:

Sets the shared NSURLCache instance to a specified cache object.

+ (void)setSharedURLCache:(NSURLCache *)cache

Parameters
cache

The cache object to use as the shared cache object.

Discussion

Applications that have special caching requirements or constraints should use this method to specify an NSURLCache instance with customized cache settings.

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

sharedURLCache

Returns the shared NSURLCache instance.

+ (NSURLCache *)sharedURLCache

Return Value

The shared NSURLCache instance.

Discussion

The disk path is set to: <user_home_directory>/Library/Caches/<current_process_name>. The user’s home directory is determined by calling NSHomeDirectory and the current process name is determined using [[NSProcessInfo processInfo] processName].

Applications that do not have special caching requirements or constraints should find the default shared cache instance acceptable. Applications with more specific needs can create a custom NSURLCache object and set it as the shared cache instance using setSharedURLCache:.

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

Instance Methods

cachedResponseForRequest:

Returns the cached URL response in the cache for the specified URL request.

- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request

Parameters
request

The URL request whose cached response is desired.

Return Value

The cached URL response for request, or nil if no response has been cached.

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

currentDiskUsage

Returns the current size of the receiver’s on-disk cache, in bytes.

- (NSUInteger)currentDiskUsage

Return Value

The current size of the receiver’s on-disk cache, in bytes.

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

currentMemoryUsage

Returns the current size of the receiver’s in-memory cache, in bytes.

- (NSUInteger)currentMemoryUsage

Return Value

The current size of the receiver’s in-memory cache, in bytes.

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

diskCapacity

Returns the capacity of the receiver’s on-disk cache, in bytes.

- (NSUInteger)diskCapacity

Return Value

The capacity of the receiver’s on-disk cache, in bytes.

Availability
See Also
Declared In
NSURLCache.h

initWithMemoryCapacity:diskCapacity:diskPath:

Initializes an NSURLCache object with the specified values.

- (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path

Parameters
memoryCapacity

The memory capacity of the cache, in bytes.

diskCapacity

The disk capacity of the cache, in bytes.

path

The location at which to store the on-disk cache.

Return Value

The initialized NSURLCache object.

Discussion

The returned NSURLCache is backed by disk, so developers can be more liberal with space when choosing the capacity for this kind of cache. A disk cache measured in the tens of megabytes should be acceptable in most cases.

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

memoryCapacity

Returns the capacity of the receiver’s in-memory cache, in bytes.

- (NSUInteger)memoryCapacity

Return Value

The capacity of the receiver’s in-memory cache, in bytes.

Availability
See Also
Declared In
NSURLCache.h

removeAllCachedResponses

Clears the receiver’s cache, removing all stored cached URL responses.

- (void)removeAllCachedResponses

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

removeCachedResponseForRequest:

Removes the cached URL response for a specified URL request.

- (void)removeCachedResponseForRequest:(NSURLRequest *)request

Parameters
request

The URL request whose cached URL response should be removed. If there is no corresponding cached URL response, no action is taken.

Availability
See Also
Declared In
NSURLCache.h

setDiskCapacity:

Sets the receiver’s on-disk cache capacity

- (void)setDiskCapacity:(NSUInteger)diskCapacity

Parameters
diskCapacity

The new on-disk cache capacity, in bytes. The on-disk cache will truncate its contents to diskCapacity, if necessary.

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

setMemoryCapacity:

Sets the receiver’s in-memory cache capacity.

- (void)setMemoryCapacity:(NSUInteger)memoryCapacity

Parameters
memoryCapacity

The new in-memory cache capacity, in bytes. The in-memory cache will truncate its contents to memoryCapacity, if necessary.

Availability
See Also
Related Sample Code
Declared In
NSURLCache.h

storeCachedResponse:forRequest:

Stores a cached URL response for a specified request

- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request

Parameters
cachedResponse

The cached URL response to store.

request

The request for which the cached URL response is being stored.

Availability
See Also
Declared In
NSURLCache.h

Next Page > Hide TOC


© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-03-11)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.