Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): caching enhancements #9227

Merged
merged 3 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/en/joinus/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ const description = await ctx.cache.tryGet(link, async () => {
});
```

The implementation of tryGet can be seen [here](https://github.com/DIYgod/RSSHub/blob/master/lib/middleware/cache/index.js#L58). The first parameter is the cache key, the second parameter is the cache data acquisition method, and the third parameter is the cache time, it should not be passed in normally. The cache time defaults to [CACHE_CONTENT_EXPIRE](/en/install/#cache-configurations), and each time accessing the cache will recalculate the expiration time
The implementation of tryGet can be seen [here](https://github.com/DIYgod/RSSHub/blob/master/lib/middleware/cache/index.js#L58). The 1st parameter is the cache key; the 2nd parameter is the cache data acquisition method (executed when cache miss); the 3rd parameter is the cache time, it should not be passed in normally and defaults to [CACHE_CONTENT_EXPIRE](/en/install/#cache-configurations); the 4th parameter determines whether to recalculate the expiration time ("renew" the cache) when the current attempt cache hits, `true` is on, `false` is off, default is on

---

Expand Down
2 changes: 1 addition & 1 deletion docs/joinus/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ const description = await ctx.cache.tryGet(link, async () => {
});
```

tryGet 的实现可以看[这里](https://github.com/DIYgod/RSSHub/blob/master/lib/middleware/cache/index.js#L58)第一个参数为缓存的 key,第二个参数为缓存数据获取方法,第三个参数为缓存时间,正常情况不应该传入,缓存时间默认为 [CACHE_CONTENT_EXPIRE](/install/#缓存配置),且每次访问缓存会重新计算过期时间
tryGet 的实现可以看[这里](https://github.com/DIYgod/RSSHub/blob/master/lib/middleware/cache/index.js#L58)第一个参数为缓存的 key;第二个参数为缓存未命中时的数据获取方法;第三个参数为缓存时间,正常情况不应该传入,缓存时间默认为 [CACHE_CONTENT_EXPIRE](/install/#缓存配置);第四个参数为控制本次尝试缓存命中时是否需要重新计算过期时间(给缓存「续期」)的开关,`true` 为打开,`false` 为关闭,默认为打开

* * *

Expand Down
4 changes: 2 additions & 2 deletions lib/middleware/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ module.exports = function (app) {
const { get, set, status } = cacheModule;
app.context.cache = {
...cacheModule,
tryGet: async (key, getValueFunc, maxAge = config.cache.contentExpire) => {
let v = await get(key);
tryGet: async (key, getValueFunc, maxAge = config.cache.contentExpire, refresh = true) => {
let v = await get(key, refresh);
if (!v) {
v = await getValueFunc();
set(key, v, maxAge);
Expand Down
19 changes: 17 additions & 2 deletions lib/middleware/cache/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@ redisClient.on('connect', () => {
logger.info('Redis connected.');
});

const getCacheTtlKey = (key) => {
if (key.startsWith('cacheTtl:')) {
throw Error('"cacheTtl:" prefix is reserved for the internal usage, please change your cache key'); // blocking any attempt to get/set the cacheTtl
}
return `cacheTtl:${key}`;
};

module.exports = {
get: async (key, refresh = true) => {
if (key && status.available) {
let value = await redisClient.get(key);
const cacheTtlKey = getCacheTtlKey(key);
let [value, cacheTtl] = await redisClient.mget(key, cacheTtlKey);
if (value && refresh) {
redisClient.expire(key, config.cache.contentExpire);
if (!cacheTtl) {
cacheTtl = config.cache.contentExpire;
redisClient.set(cacheTtlKey, cacheTtl, 'EX', cacheTtl);
} else {
redisClient.expire(cacheTtlKey, cacheTtl);
}
redisClient.expire(key, cacheTtl);
value = value + '';
}
return value;
Expand All @@ -40,6 +54,7 @@ module.exports = {
value = JSON.stringify(value);
}
if (key) {
redisClient.set(getCacheTtlKey(key), maxAge, 'EX', maxAge);
return redisClient.set(key, value, 'EX', maxAge); // setMode: https://redis.io/commands/set
}
},
Expand Down