Skip to content

[Feature] Bindings for the Workers Cache purge API (ctx.cache.purge) #1024

Description

@chrishiguto

Is there an existing issue for this?

  • I have searched the existing issues

Description

The runtime now exposes the newer per-Worker Workers Cache (announcement: https://blog.cloudflare.com/workers-cache/, docs: https://developers.cloudflare.com/workers/cache/), which is driven by Cache-Control / Cache-Tag response headers and has one imperative API: ctx.cache.purge() (also available as the cache export of cloudflare:workers). This is distinct from the Service-Worker Cache API already covered by worker::Cache (caches.default).

There's currently no way to call purge from Rust — worker::Context doesn't expose ctx.cache, and worker-sys has no binding for the CacheContext object. I'd like to add bindings for it.

Proposed API:

use worker::CachePurgeOptions;

// From the execution context (mirrors JS `ctx.cache`)
if let Some(cache) = ctx.cache() {
    let result = cache.purge(CachePurgeOptions::tags(["blog-posts"])).await?;
    if !result.success {
        console_error!("purge failed: {:?}", result.errors);
    }
}

// Or without a `ctx` in scope, via the `cloudflare:workers` export
let cache = worker::cache();
cache.purge(CachePurgeOptions::everything()).await?;

Shape of the types:

  • Context::cache() -> Option<CacheContext>None when the feature isn't enabled or the runtime predates it.
  • worker::cache() -> CacheContext — the cloudflare:workers module export, for code with no ctx in scope.
  • CachePurgeOptions with constructors tags(...), path_prefixes(...), and everything(), serialized to the runtime's camelCase keys (tags, pathPrefixes, purgeEverything). purge_everything is validated as mutually exclusive with the other two before crossing the FFI boundary.
  • CachePurgeResult { success: bool, errors: Vec<CachePurgeError> } mirroring the runtime's result object; a rejected promise (e.g. permission error) surfaces as Err.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions