NSURLCache Class Reference

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

Overview

The NSURLCache class implements the caching of responses to URL load requests by mapping NSURLRequest objects to NSCachedURLResponse objects. It provides a composite in-memory and on-disk cache, and lets you manipulate the sizes of both the in-memory and on-disk portions. You can also control the path where cache data is stored persistently.

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

An application that has special caching requirements or constraints should use this method to specify an NSURLCache instance with customized cache settings. The application should do so before any calls to the sharedURLCache method.

Availability
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
Declared In
NSURLCache.h

sharedURLCache

Returns the shared NSURLCache instance.

+ (NSURLCache *)sharedURLCache
Return Value

The shared NSURLCache instance.

Discussion

Applications that do not have special caching requirements or constraints should find the default shared cache instance acceptable. An application with more specific needs can create a custom NSURLCache object and set it as the shared cache instance using setSharedURLCache:. The application should do so before any calls to this method.

Availability
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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

In OS X, path is the location at which to store the on-disk cache.

In iOS, path is the name of a subdirectory of the application’s default cache directory in which to store the on-disk cache (the subdirectory is created if it does not exist).

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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
Declared In
NSURLCache.h

removeAllCachedResponses

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

- (void)removeAllCachedResponses
Availability
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
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
  • Available in OS X v10.2 with Safari 1.0 installed.
  • Available in OS X v10.2.7 and later.
Declared In
NSURLCache.h