如何使用 ‘apicache’ 包清除特定路由的缓存。

huangapple go评论121阅读模式
英文:

How to clear cache for a route with the 'apicache' package

问题

以下是您提供的代码的中文翻译:

  1. 我正在为我的MERN应用程序使用NPM安装API缓存我希望我的其中一个请求被缓存然后当对另一个路由发出请求时它会刷新缓存获取新数据)。我已经成功使其中一个请求被缓存但我无法清除它
  2. 这是我的代码
  3. ```javascript
  4. const apicache = require("apicache");
  5. let cache = apicache.middleware;
  6. app.get("/api/users/checkJWT", cache("2 minutes"), async (req, res) => {
  7. const token = req.headers.authorization?.split(" ")[1];
  8. const decoded = jwt.verify(token, SECRET_KEY);
  9. if (decoded != null && (await User.findById(decoded.id)) != null) {
  10. return res.status(200).json({ valid: true, user: await User.findById(decoded.id) });
  11. } else {
  12. return res.status(400).json({ valid: false });
  13. }
  14. });
  15. app.patch("/api/users/user", authenticateJWTUser, async (req, res) => {
  16. apicache.clear('/api/users/checkJWT');
  17. const user = await User.findOne({ name: req.body.username });
  18. if (req.body.name != null) {
  19. user.name = req.body.name;
  20. }
  21. if (req.body.email != null) {
  22. user.email = req.body.email;
  23. }
  24. if (req.body.password != null) {
  25. user.password = await bcrypt.hash(req.body.password, 10);
  26. }
  27. if (req.body.tasks != null) {
  28. user.tasks = req.body.tasks;
  29. }
  30. try {
  31. const updatedUser = await user.save();
  32. res.json(updatedUser);
  33. } catch (error) {
  34. res.status(400).json({ message: error.message });
  35. }
  36. });

我的Git仓库链接: https://github.com/JesseOgunlaja/Task-Tracker-MERN/tree/ae1b0027bfa40f90ab7a3d6208c05a9eb41a4478

  1. 请注意,我只提供了代码的翻译部分,没有包含其他内容。
  2. <details>
  3. <summary>英文:</summary>
  4. I&#39;m using NPM install API cache for my MERN application. I want one of my requests to be cached and then when a request is made to another route it refreshes the cache (it fetches the new data). I&#39;ve managed to get one of my requests to be cached but I can&#39;t manage to clear it.
  5. This is my code

const apicache = require("apicache")

let cache = apicache.middleware

app.get("/api/users/checkJWT",cache("2 minutes"), async (req,res) => {
const token = req.headers.authorization?.split(" ")[1]

const decoded = jwt.verify(token,SECRET_KEY)

if(decoded != null && await User.findById(decoded.id) != null) {
return res.status(200).json({valid: true, user: await User.findById(decoded.id)})
}
else {
return res.status(400).json({valid: false})
}
})

app.patch("/api/users/user",authenticateJWTUser, async (req, res) => {
apicache.clear('/api/users/checkJWT')
const user = await User.findOne({ name: req.body.username });
if (req.body.name != null) {
user.name = req.body.name;
}
if (req.body.email != null) {
user.email = req.body.email;
}
if (req.body.password != null) {
user.password = await bcrypt.hash(req.body.password, 10);
}
if (req.body.tasks != null) {
user.tasks = req.body.tasks;
}
try {
const updatedUser = await user.save();
res.json(updatedUser);
} catch (error) {
res.status(400).json({ message: error.message });
}
});

  1. Link to my Git repo: https://github.com/JesseOgunlaja/Task-Tracker-MERN/tree/ae1b0027bfa40f90ab7a3d6208c05a9eb41a4478
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. I was able to get this work using the [apicache-plus package](https://www.npmjs.com/package/apicache-plus?activeTab=readme).
  6. ```javascript
  7. const apicache = require("apicache-plus");
  8. router.get(
  9. "/api/users/getName/1",
  10. apicache.middleware("10 minutes"),
  11. async (req, res, next) => {
  12. req.apicacheGroup = "toClear";
  13. const someData = { someName: "Amy" };
  14. res.json(someData);
  15. }
  16. );
  17. router.get("/api/users/user/1/2", async (req, res, next) => {
  18. console.log("something");
  19. apicache.clear("toClear");
  20. const user = { name: "Jeff" };
  21. res.json(user);
  22. });

(Note: Code parts are not translated.)

英文:

I was able to get this work using the apicache-plus package.

  1. const apicache = require(&quot;apicache-plus&quot;);
  2. router.get(
  3. &quot;/api/users/getName/1&quot;,
  4. apicache.middleware(&quot;10 minutes&quot;),
  5. async (req, res, next) =&gt; {
  6. req.apicacheGroup = &quot;toClear&quot;;
  7. const someData = { someName: &quot;Amy&quot; };
  8. res.json(someData);
  9. }
  10. );
  11. router.get(&quot;/api/users/user/1/2&quot;, async (req, res, next) =&gt; {
  12. console.log(&quot;something&quot;);
  13. apicache.clear(&quot;toClear&quot;);
  14. const user = { name: &quot;Jeff&quot; };
  15. res.json(user);
  16. });

huangapple
  • 本文由 发表于 2023年6月2日 01:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定