# Key Generator 和 Caching Key Generator

相同点，都提供方法：

```
generate_key
```

不同点：

Key Generator 生成的是**初级**密钥。

```ruby
key_generator = ActiveSupport::KeyGenerator.new 'secret'
key_generator.generate_key 'salt'
```

Caching Key Generator 再次封装 Key Generator 和 Thread Safe，生成的是**高级**密钥。

```ruby
key_generator = ActiveSupport::KeyGenerator.new 'secret'

# 参数是 KeyGenerator 实例对象
caching_key_generator = ActiveSupport::CachingKeyGenerator.new(key_generator)
caching_key_generator.generate_key 'salt'
```

> Note: 注意和【Message Encryptor 和 Message Verifier】章节的联系。


---

# Agent Instructions: 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:

```
GET https://kelby.gitbook.io/rails-beginner-s-guide/activesupport/keygenerator__cachingkeygenerator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
