From 4227c30d77aa5dc32f06569d8cb46dfb78ca723d Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 28 Jan 2019 21:18:31 +0000 Subject: [PATCH] Update docs and example --- docs/en/joinus/README.md | 14 ++++++++++---- docs/joinus/README.md | 14 ++++++++++---- lib/routes/zhihu/daily.js | 4 ++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/en/joinus/README.md b/docs/en/joinus/README.md index 090aa087de..8ce369fd3c 100644 --- a/docs/en/joinus/README.md +++ b/docs/en/joinus/README.md @@ -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 } ``` diff --git a/docs/joinus/README.md b/docs/joinus/README.md index bd529d3587..4a57021cca 100644 --- a/docs/joinus/README.md +++ b/docs/joinus/README.md @@ -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; // 直接赋值 diff --git a/lib/routes/zhihu/daily.js b/lib/routes/zhihu/daily.js index 1dd693e08d..92f6e0a48e 100644 --- a/lib/routes/zhihu/daily.js +++ b/lib/routes/zhihu/daily.js @@ -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(/
([\s\S]*?)<\/div>/g, '$1').replace(//g, '')); - ctx.cache.set(key, item.description, 24 * 60 * 60); + ctx.cache.set(key, item.description, 24 * 60 * 60, true); } return Promise.resolve(item);