Cloudflare机器人缓存问题

发布于 2025-02-07 10:17:12 字数 848 浏览 4 评论 0原文

我正在使用CloudFlare Worker从GitHub存储库中获取数据并缓存。工人似乎可以缓存文件并将其发送给用户,但以某种怪异的顺序发送。

github repo

<h1>Rocket</h1>
<img src=rocket.png>
<img src=rocket2.png>

​强>“ r” 是GitHub的响应; “ req” event.request

let cache = caches.default;
var cacheData = {body: req.body,method: req.method}

添加到缓存中

await cache.put(new Request(req.url,cacheData), await fetch(r.download_url));

以获得高速缓存(从技术上讲是最重要的)

var pCache = await cache.match(new Request(req.url,cacheData))
if(!!pCache)return pCache

对这会导致的任何想法?

I am using a Cloudflare worker to get data from a GitHub repository and cache it. The worker appears to cache the files and send them to the user, but in some weird order.

GitHub Repo

<h1>Rocket</h1>
<img src=rocket.png>
<img src=rocket2.png>

Response

screenshot

code for caching

"r" is the response from GitHub; "req" is event.request

let cache = caches.default;
var cacheData = {body: req.body,method: req.method}

To add to cache

await cache.put(new Request(req.url,cacheData), await fetch(r.download_url));

To get cache (technically on top)

var pCache = await cache.match(new Request(req.url,cacheData))
if(!!pCache)return pCache

Any ideas for what would cause this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2025-02-14 10:17:12

而不是使用缓存API,您还可以通过通过cf将Caching设置传递到fetch(...)来允许CF处理缓存。

export default {
  fetch(req, env, ctx) {
    const url = new URL(req.url);
    const targetUrl = `https://github.com${url.pathname}${url.search}`;

    return fetch(targetUrl, {
      cf: {
        cacheTtl: 1209600,
        cacheEverything: true
      }
    });
  }
}

Instead of using Cache API you could also allow CF handle caching by passing caching settings via cf to fetch(...).

export default {
  fetch(req, env, ctx) {
    const url = new URL(req.url);
    const targetUrl = `https://github.com${url.pathname}${url.search}`;

    return fetch(targetUrl, {
      cf: {
        cacheTtl: 1209600,
        cacheEverything: true
      }
    });
  }
}
绿萝 2025-02-14 10:17:12

问题归因于某个文件的多个请求。
我没有使用fetch,而是使用了缓存。

The issue was due to multiple requests to a certain file.
Instead of using fetch, I used the cache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文