> For the complete documentation index, see [llms.txt](https://docs.natsclub.cn/cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.natsclub.cn/cn/nats-gai-nian/jetstream/key-value-store.md).

# 键值对存储

JetSteam, the persistence layer of NATS, doesn't just allow for higher qualities of service and features associated with 'streaming', but it also enables some functionalities not found in messaging systems.

One such feature is the Key/Value store functionality, which allows client applications to create 'buckets' and use them as immediately consistent, persistent [associative arrays](https://en.wikipedia.org/wiki/Associative_array).

You can use KV buckets to perform the typical operations you would expect from an immediately consistent key/value store:

* put: associate a value with a key
* get: retrieve the value associated with a key
* delete: clear any value associated with a key
* purge: clear all the values associated with all keys
* create: associate the value with a key only if there is currently no value associated with that key (i.e. compare to null and set)
* update: compare and set (aka compare and swap) the value for a key
* keys: get a copy of all the keys (with a value or operation associated to it)

You can set limits for your buckets, such as:

* the maximum size of the bucket
* the maximum size for any single value
* a TTL: how long the store will keep values for

Finally, you can even do things that typically can not be done with a Key/Value Store:

* watch: watch for changes happening for a key, which is similar to subscribing (in the publish/subscribe sense) to the key: the watcher receives updates due to put or delete operations on the key pushed to it in real-time as they happen
* watch all: watch for all the changes happening on all the keys in the bucket
* history: retrieve a history of the values (and delete operations) associated with each key over time (by default the history of buckets is set to 1, meaning that only the latest value/operation is stored)


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.natsclub.cn/cn/nats-gai-nian/jetstream/key-value-store.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
