mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-13 00:35:19 +08:00
* chore(deps): bump the docs group in /website with 6 updates Bumps the docs group in /website with 6 updates: | Package | From | To | | --- | --- | --- | | [@docusaurus/core](https://github.com/facebook/docusaurus/tree/HEAD/packages/docusaurus) | `2.4.3` | `3.0.0` | | [@docusaurus/plugin-client-redirects](https://github.com/facebook/docusaurus/tree/HEAD/packages/docusaurus-plugin-client-redirects) | `2.4.3` | `3.0.0` | | [@docusaurus/plugin-pwa](https://github.com/facebook/docusaurus/tree/HEAD/packages/docusaurus-plugin-pwa) | `2.4.3` | `3.0.0` | | [@docusaurus/preset-classic](https://github.com/facebook/docusaurus/tree/HEAD/packages/docusaurus-preset-classic) | `2.4.3` | `3.0.0` | | [@docusaurus/module-type-aliases](https://github.com/facebook/docusaurus/tree/HEAD/packages/docusaurus-module-type-aliases) | `2.4.3` | `3.0.0` | | [@docusaurus/types](https://github.com/facebook/docusaurus/tree/HEAD/packages/docusaurus-types) | `2.4.3` | `3.0.0` | Updates `@docusaurus/core` from 2.4.3 to 3.0.0 - [Release notes](https://github.com/facebook/docusaurus/releases) - [Changelog](https://github.com/facebook/docusaurus/blob/main/CHANGELOG.md) - [Commits](https://github.com/facebook/docusaurus/commits/v3.0.0/packages/docusaurus) Updates `@docusaurus/plugin-client-redirects` from 2.4.3 to 3.0.0 - [Release notes](https://github.com/facebook/docusaurus/releases) - [Changelog](https://github.com/facebook/docusaurus/blob/main/CHANGELOG.md) - [Commits](https://github.com/facebook/docusaurus/commits/v3.0.0/packages/docusaurus-plugin-client-redirects) Updates `@docusaurus/plugin-pwa` from 2.4.3 to 3.0.0 - [Release notes](https://github.com/facebook/docusaurus/releases) - [Changelog](https://github.com/facebook/docusaurus/blob/main/CHANGELOG.md) - [Commits](https://github.com/facebook/docusaurus/commits/v3.0.0/packages/docusaurus-plugin-pwa) Updates `@docusaurus/preset-classic` from 2.4.3 to 3.0.0 - [Release notes](https://github.com/facebook/docusaurus/releases) - [Changelog](https://github.com/facebook/docusaurus/blob/main/CHANGELOG.md) - [Commits](https://github.com/facebook/docusaurus/commits/v3.0.0/packages/docusaurus-preset-classic) Updates `@docusaurus/module-type-aliases` from 2.4.3 to 3.0.0 - [Release notes](https://github.com/facebook/docusaurus/releases) - [Changelog](https://github.com/facebook/docusaurus/blob/main/CHANGELOG.md) - [Commits](https://github.com/facebook/docusaurus/commits/v3.0.0/packages/docusaurus-module-type-aliases) Updates `@docusaurus/types` from 2.4.3 to 3.0.0 - [Release notes](https://github.com/facebook/docusaurus/releases) - [Changelog](https://github.com/facebook/docusaurus/blob/main/CHANGELOG.md) - [Commits](https://github.com/facebook/docusaurus/commits/v3.0.0/packages/docusaurus-types) --- updated-dependencies: - dependency-name: "@docusaurus/core" dependency-type: direct:production update-type: version-update:semver-major dependency-group: docs - dependency-name: "@docusaurus/plugin-client-redirects" dependency-type: direct:production update-type: version-update:semver-major dependency-group: docs - dependency-name: "@docusaurus/plugin-pwa" dependency-type: direct:production update-type: version-update:semver-major dependency-group: docs - dependency-name: "@docusaurus/preset-classic" dependency-type: direct:production update-type: version-update:semver-major dependency-group: docs - dependency-name: "@docusaurus/module-type-aliases" dependency-type: direct:development update-type: version-update:semver-major dependency-group: docs - dependency-name: "@docusaurus/types" dependency-type: direct:development update-type: version-update:semver-major dependency-group: docs ... Signed-off-by: dependabot[bot] <support@github.com> * chore: fix dependencies * docs: fix admonition caution is deprecated in v3 caution(v2 yellow) => warning(v3 yellow) --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
92 lines
4.1 KiB
Markdown
92 lines
4.1 KiB
Markdown
---
|
|
sidebar_position: 3
|
|
---
|
|
|
|
# Using Cache
|
|
|
|
All routes have a cache that expires after a short duration. You can change how long the cache lasts by modifying the `CACHE_EXPIRE` value in the `lib/config.js` file using environment variables. However, for interfaces that have less frequently updated content, it's better to specify a longer cache expiration time using `CACHE_CONTENT_EXPIRE` instead.
|
|
|
|
For example, to retrieve the full text of the first comment for each issue, you can make a request to `${baseUrl}/${user}/${repo}/issues/${id}`, since this data is unavailable through `${baseUrl}/${user}/${repo}/issues`. It's recommended to store this data in the cache to avoid making repeated requests to the server.
|
|
|
|
Here's an example of how you can use the cache to retrieve the data:
|
|
|
|
```js
|
|
const items = await Promise.all(
|
|
list.map((item) =>
|
|
ctx.cache.tryGet(item.link, async () => {
|
|
const { data: response } = await got(item.link);
|
|
const $ = cheerio.load(response);
|
|
|
|
item.description = $('.comment-body').first().html();
|
|
|
|
return item;
|
|
})
|
|
)
|
|
);
|
|
```
|
|
|
|
The above code snippet from [Create Your Own RSSHub Route](/joinus/new-rss/start-code#better-reading-experience) shows how to use the cache to get the full text of the first comment of each issue. `ctx.cache.tryGet()` is used to determine if the data is already available within the cache. If it's not, the code retrieves the data and stores it in the cache.
|
|
|
|
The object returned from the previous statement will be reused, and an extra `description` property will be added to it. The returned cache for each `item.link` will be `{ title, link, pubDate, author, category, description }`. The next time the same path is requested, this processed cache will be used instead of making a request to the server and recomputing the data.
|
|
|
|
:::warning
|
|
|
|
Any assignments to variables that are declared outside of the `tryGet()` function will not be processed under a cache-hit scenario. For example, the following code will not work as expected:
|
|
|
|
```js
|
|
let x = '1';
|
|
const z = await ctx.cache.tryGet('cache:key', async () => {
|
|
x = '2';
|
|
const y = '3';
|
|
return y;
|
|
})
|
|
console.log(x); // cache miss: '2', cache hit: '1'
|
|
console.log(z): // '3'
|
|
```
|
|
|
|
:::
|
|
|
|
## API
|
|
|
|
### ctx.cache.tryGet(key, getValueFunc [, maxAge [, refresh ]])
|
|
|
|
#### Parameters
|
|
|
|
| Name | Type | Description |
|
|
| ---- | ---- | ----------- |
|
|
| key | `string` | *(Required)* The key used to store and retrieve the cache. You can use `:` as a separator to create a hierarchy. |
|
|
| getValueFunc | `function` \| `string` | *(Required)* A function that returns data to be cached when a cache miss occurs.
|
|
| maxAge | `number` | *(Optional)* The maximum age of the cache in seconds. If not specified, `CACHE_CONTENT_EXPIRE` will be used. |
|
|
| refresh | `boolean` | *(Optional)* Whether to renew the cache expiration time when the cache is hit. `true` by default. |
|
|
|
|
#### Defined in
|
|
|
|
[lib/middleware/cache/index.js](https://github.com/DIYgod/RSSHub/blob/master/lib/middleware/cache/index.js#L58)
|
|
|
|
:::tip
|
|
|
|
Below are advanced methods for using cache. You should use `ctx.cache.tryGet()` most of the time.
|
|
|
|
Note that you need to use `JSON.parse()` when retrieving the cache using `ctx.cache.get()`.
|
|
|
|
:::
|
|
|
|
### ctx.cache.get(key [, refresh ])
|
|
|
|
#### Parameters
|
|
|
|
| Name | Type | Description |
|
|
| ---- | ---- | ----------- |
|
|
| key | `string` | *(Required)* The key used to retrieve the cache. You can use `:` as a separator to create a hierarchy. |
|
|
| refresh | `boolean` | *(Optional)* Whether to renew the cache expiration time when the cache is hit. `true` by default. |
|
|
|
|
### ctx.cache.set(key, value [, maxAge ])
|
|
|
|
#### Parameters
|
|
|
|
| Name | Type | Description |
|
|
| ---- | ---- | ----------- |
|
|
| key | `string` | *(Required)* The key used to store the cache. You can use `:` as a separator to create a hierarchy. |
|
|
| value | `function`\| `string` | *(Required)* The value to be cached. |
|
|
| maxAge | `number` | *(Optional)* The maximum age of the cache in seconds. If not specified, `CACHE_CONTENT_EXPIRE` will be used. |
|