mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-24 23:12:34 +08:00
Update docs and example
This commit is contained in:
@@ -270,11 +270,17 @@ By default there is a global caching period set in `lib/config.js`, some sources
|
||||
- Save to cache:
|
||||
|
||||
```js
|
||||
ctx.cache.set((key: string), (value: string), (time: number)); // time is the caching period in seconds
|
||||
ctx.cache.set((key: string), (value: string), (time: number), (persistent: boolean)); // time is the caching period in seconds. When persistent is set to true, the cache will refresh its expiry time upon every re-request (Under Redis this means the cache will never expire).
|
||||
```
|
||||
|
||||
- Access the cache:
|
||||
|
||||
::: tip tips
|
||||
|
||||
Since Redis caches with `persistent = true` will never expire, parameter `persistent` for `Get()` only has effect on LRU memory cache, it serves no purpose for Redis.
|
||||
|
||||
:::
|
||||
|
||||
```js
|
||||
const value = await ctx.cache.get((key: string));
|
||||
```
|
||||
@@ -285,19 +291,19 @@ Given the update frequency is known, set the appropriate caching period to reuse
|
||||
|
||||
```js
|
||||
const key = 'daily' + story.id; // story.id is the unique identifier of each article
|
||||
ctx.cache.set(key, item.description, 24 * 60 * 60); // set the caching period to 24 hours * 60 minutes * 60 seconds = 86,400 seconds = 1 day
|
||||
ctx.cache.set(key, item.description, 24 * 60 * 60, true); // set the LRU memeroy caching period to 24 hours * 60 minutes * 60 seconds = 86,400 seconds = 1 day
|
||||
```
|
||||
|
||||
When the identical requests come in, reuse the cache:
|
||||
|
||||
```js
|
||||
const key = 'daily' + story.id;
|
||||
const value = await ctx.cache.get(key); // query the cache to find the unique identifier
|
||||
const value = await ctx.cache.get(key, true); // query the LRU memory cache to find the unique identifier
|
||||
if (value) {
|
||||
// return the cached data
|
||||
item.description = value; // assign the cached data
|
||||
} else {
|
||||
// no cached found
|
||||
// no cache found
|
||||
// initiate request to the data source
|
||||
}
|
||||
```
|
||||
|
||||
+10
-4
@@ -267,29 +267,35 @@ sidebar: auto
|
||||
- 添加缓存:
|
||||
|
||||
```js
|
||||
ctx.cache.set((key: string), (value: string), (time: number)); // time 为缓存时间, 单位为秒
|
||||
ctx.cache.set((key: string), (value: string), (time: number), (persistent: boolean)); // time 为缓存时间。单位为秒。persistent 为 true 则为缓存自动延长过期时间 (Redis 下则为永不过期)。
|
||||
```
|
||||
|
||||
- 获取缓存:
|
||||
|
||||
::: tip 提示
|
||||
|
||||
由于 Redis 下 `persistent = true` 的缓存不会过期,`Get()` 下 `persistent` 参数仅适用于 LRU memory 缓存,对于 Redis 不会有任何效果。
|
||||
|
||||
:::
|
||||
|
||||
```js
|
||||
const value = await ctx.cache.get((key: string));
|
||||
```
|
||||
|
||||
例如知乎日报需要获取文章全文:[/lib/routes/zhihu/daily.js](https://github.com/DIYgod/RSSHub/blob/master/lib/routes/zhihu/daily.js), 每篇文章都需要单独请求一次。
|
||||
|
||||
由于已知文章更新频率为一天,把结果缓存一天,可以让后续的请求直接使用已缓存的数据,从而提升性能并节省资源。
|
||||
由于已知文章更新频率为一天,把结果缓存一天,可以让后续的请求直接使用已缓存的数据,并且该缓存被再次访问后会延长过期时间,从而提升性能并节省资源。
|
||||
|
||||
```js
|
||||
const key = 'daily' + story.id; // story.id 为知乎日报返回的文章唯一识别符
|
||||
ctx.cache.set(key, item.description, 24 * 60 * 60); // 设置缓存时间为 24小时 * 60分钟 * 60秒 = 86400秒 = 1天
|
||||
ctx.cache.set(key, item.description, 24 * 60 * 60, true); // 设置 LRU memory 缓存时间为 24小时 * 60分钟 * 60秒 = 86400秒 = 1天
|
||||
```
|
||||
|
||||
当同样的请求被发起时,优先使用未过期的缓存:
|
||||
|
||||
```js
|
||||
const key = 'daily' + story.id;
|
||||
const value = await ctx.cache.get(key); // 从缓存中查询是否已存在该文章唯一识别符
|
||||
const value = await ctx.cache.get(key, true); // 从 LRU memory 缓存中查询是否已存在该文章唯一识别符
|
||||
if (value) {
|
||||
// 查询返回未过期缓存
|
||||
item.description = value; // 直接赋值
|
||||
|
||||
@@ -24,7 +24,7 @@ module.exports = async (ctx) => {
|
||||
link: 'https://news-at.zhihu.com/story/' + story.id,
|
||||
};
|
||||
const key = 'daily' + story.id;
|
||||
const value = await ctx.cache.get(key);
|
||||
const value = await ctx.cache.get(key, true);
|
||||
|
||||
if (value) {
|
||||
item.description = value;
|
||||
@@ -37,7 +37,7 @@ module.exports = async (ctx) => {
|
||||
},
|
||||
});
|
||||
item.description = utils.ProcessImage(storyDetail.data.body.replace(/<div class="meta">([\s\S]*?)<\/div>/g, '<strong>$1</strong>').replace(/<h2.*?>/g, ''));
|
||||
ctx.cache.set(key, item.description, 24 * 60 * 60);
|
||||
ctx.cache.set(key, item.description, 24 * 60 * 60, true);
|
||||
}
|
||||
|
||||
return Promise.resolve(item);
|
||||
|
||||
Reference in New Issue
Block a user