Files
RSSHub/lib/middleware/cache/memory.js
dependabot[bot] 3a62de1cdc chore(deps): bump lru-cache from 8.0.5 to 9.1.1 (#12383)
* chore(deps): bump lru-cache from 8.0.5 to 9.1.1

Bumps [lru-cache](https://github.com/isaacs/node-lru-cache) from 8.0.5 to 9.1.1.
- [Release notes](https://github.com/isaacs/node-lru-cache/releases)
- [Changelog](https://github.com/isaacs/node-lru-cache/blob/main/CHANGELOG.md)
- [Commits](https://github.com/isaacs/node-lru-cache/compare/v8.0.5...v9.1.1)

---
updated-dependencies:
- dependency-name: lru-cache
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: use named export

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-04-25 23:01:18 +08:00

37 lines
959 B
JavaScript

const { LRUCache } = require('lru-cache');
const config = require('@/config').value;
const status = { available: false };
const memoryCache = new LRUCache({
ttl: config.cache.routeExpire * 1000,
max: config.memory.max,
});
status.available = true;
module.exports = {
get: (key, refresh = true) => {
if (key && status.available) {
let value = memoryCache.get(key, { updateAgeOnGet: refresh });
if (value) {
value = value + '';
}
return value;
}
},
set: (key, value, maxAge = config.cache.contentExpire) => {
if (!value || value === 'undefined') {
value = '';
}
if (typeof value === 'object') {
value = JSON.stringify(value);
}
if (key && status.available) {
return memoryCache.set(key, value, { ttl: maxAge * 1000 });
}
},
clients: { memoryCache },
status,
};