> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-cms/13.latest/reference/cache/updating-cache.md).

# Getting/Adding/Updating/Inserting Into Cache

*This section describes how you should be getting/adding/updating/inserting items in the cache.*

## Adding and retrieving items in the cache

The recommended way to put data in and get data out is to use one of the many overloaded methods of: `GetCacheItem`. The `GetCacheItem` methods (all except one) are designed to "Get or Add" to the cache. The following retrieves an item from the cache and adds it if it doesn't already exist:

```csharp
MyObject cachedItem = _appCaches.RuntimeCache.GetCacheItem<MyObject>("MyCacheKey", () => new MyObject());
```

where `_appCaches` is injected as type `AppCaches`.

Notice 2 things:

* The `GetCacheItem` method is strongly typed and
* We are supplying a callback method which is used to populate the cache if it doesn't exist.

The example above will retrieve a strongly typed object of `MyObject` from the cache with the key of "MyCacheKey". If the object doesn't exist in the cache, a new instance of MyObject `MyObject` be added to it with the same key.

There are a couple of overloads of `GetCacheItem` allowing you to customize how your object is cached from cache dependencies to expiration times.

To use this generic implementation, add the `Umbraco.Extensions` namespace to your code.

### Retrieving an item from the cache without a callback

One of the overloads of `GetCacheItem` doesn't specify a callback. This allows you to retrieve an item from the cache without populating it if it doesn't exist.

An example of usage:

```csharp
MyObject cachedItem = _appCaches.RuntimeCache.GetCacheItem<MyObject>("MyCacheKey");
```

where `_appCaches` is injected as type `AppCaches`.

### Inserting an item into the cache without retrieval

Sometimes you might want to put something in the cache without retrieving it.\
In this case there is an `InsertCacheItem<T>` method.\
This method will add or update the cache item specified by the key. If the item already exists in the cache, it will be replaced.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/umbraco-cms/13.latest/reference/cache/updating-cache.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
