<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCache",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCache"
  },
  "title" : "NSCache"
}
-->

# NSCache

A mutable collection you use to temporarily store transient key-value pairs that are subject to eviction when resources are low.

```
class NSCache<KeyType, ObjectType> where KeyType : AnyObject, ObjectType : AnyObject
```

## Overview

Cache objects differ from other mutable collections in a few ways:

- The [`NSCache`](/documentation/Foundation/NSCache) class incorporates various auto-eviction policies, which ensure that a cache doesn’t use too much of the system’s memory. If memory is needed by other applications, these policies remove some items from the cache, minimizing its memory footprint.
- You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.
- Unlike an [`NSMutableDictionary`](/documentation/Foundation/NSMutableDictionary) object, a cache does not copy the key objects that are put into it.

You typically use [`NSCache`](/documentation/Foundation/NSCache) objects to temporarily store objects with transient data that are expensive to create. Reusing these objects can provide performance benefits, because their values do not have to be recalculated. However, the objects are not critical to the application and can be discarded if memory is tight. If discarded, their values will have to be recomputed again when needed.

Objects that have subcomponents that can be discarded when not being used can adopt the [`NSDiscardableContent`](/documentation/Foundation/NSDiscardableContent) protocol to improve cache eviction behavior. By default, [`NSDiscardableContent`](/documentation/Foundation/NSDiscardableContent) objects in a cache are automatically removed if their content is discarded, although this automatic removal policy can be changed. If an [`NSDiscardableContent`](/documentation/Foundation/NSDiscardableContent) object is put into the cache, the cache calls [`discardContentIfPossible()`](/documentation/Foundation/NSDiscardableContent/discardContentIfPossible()) on it upon its removal.

## Topics

### Managing the Name

[`name`](/documentation/Foundation/NSCache/name)

The name of the cache.

### Managing Cache Size

[`countLimit`](/documentation/Foundation/NSCache/countLimit)

The maximum number of objects the cache should hold.

[`totalCostLimit`](/documentation/Foundation/NSCache/totalCostLimit)

The maximum total cost that the cache can hold before it starts evicting objects.

### Managing Discardable Content

[`evictsObjectsWithDiscardedContent`](/documentation/Foundation/NSCache/evictsObjectsWithDiscardedContent)

Whether the cache will automatically evict discardable-content objects whose content has been discarded.

[`NSDiscardableContent`](/documentation/Foundation/NSDiscardableContent)

You implement this protocol when a class’s objects have subcomponents that can be discarded when not being used, thereby giving an application a smaller memory footprint.

### Managing the Delegate

[`delegate`](/documentation/Foundation/NSCache/delegate)

The cache’s delegate.

[`NSCacheDelegate`](/documentation/Foundation/NSCacheDelegate)

The delegate of an [`NSCache`](/documentation/Foundation/NSCache) object implements this protocol to perform specialized actions when an object is about to be evicted or removed from the cache.

### Getting a Cached Value

[`object(forKey:)`](/documentation/Foundation/NSCache/object(forKey:))

Returns the value associated with a given key.

### Adding and Removing Cached Values

[`setObject(_:forKey:)`](/documentation/Foundation/NSCache/setObject(_:forKey:))

Sets the value of the specified key in the cache.

[`setObject(_:forKey:cost:)`](/documentation/Foundation/NSCache/setObject(_:forKey:cost:))

Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost.

[`removeObject(forKey:)`](/documentation/Foundation/NSCache/removeObject(forKey:))

Removes the value of the specified key in the cache.

[`removeAllObjects()`](/documentation/Foundation/NSCache/removeAllObjects())

Empties the cache.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)