Rust

cache

Module

Purging cached content: the Adapter every provider implements, the Cache front that validates what it is given, and the providers (Cloudflare, Fastly, and Balancer, which fans out over several).

Contents

3 submodules, and what the module defines itself.

use utopia_cdn::cache::Cache;
Rust
pub struct Cache<A>

Purges through an adapter, checking the domain and paths first.

Rust
use utopia_cdn::cache::{Cache, Cloudflare};

let cache = Cache::new(Cloudflare::new("zone-id", "api-token"));
cache.purge_paths("Example.com", &["/index.html".into()]).await?;
cache.purge_domain("example.com").await?;
Compiles in CI; needs a running service to execute.
use utopia_cdn::cache::Adapter;
Rust
pub trait Adapter: Send + Sync

The purge operations every provider offers.

Providers differ in what they expose natively (Cloudflare purges a hostname directly, Fastly has to be told which surrogate key stands for one), but a caller does not have to know which: every adapter implements every operation, or fails with Error::UnsupportedOperation when it cannot serve one with the configuration it was given. Implement it to add a provider.

The futures borrow their arguments, so the trait is object safe: a Balancer holds Arc<dyn Adapter>s.

Implemented byBalancerCloudflareFastlyArcBox

cache::balancer

Own page

A purge fanned out over every option a balancer's filters leave.

use utopia_cdn::cache::balancer::Balancer;
Rust
pub struct Balancer<A>

Purges through every CdnOption a balancer's filters leave standing.

The balancer holds every configured provider and the filters that narrow them down, so a caller states what to purge and which options qualify, never which API to call. A domain cached by several providers is purged from all of them.

Options are purged one after the other, and one failing does not stop the rest: the failures are reported together, as Error::Purge, once every option has been tried. An option that cannot serve the operation (Error::UnsupportedOperation) is skipped; when every option skips, the purge fails with Error::UnsupportedOperation. No option left after the filters is Error::Configuration.

Rust
use std::sync::Arc;

use utopia_balancer::{Balancer as Options, First};
use utopia_cdn::CdnOption;
use utopia_cdn::cache::{Balancer, Cache, Cloudflare, Fastly};

let mut options = Options::new(First);
options
    .add_option(CdnOption::new(Arc::new(Fastly::new("token", "domain-").with_service_id("edge")), CdnOption::PROVIDER_FASTLY, true))
    .add_option(CdnOption::new(Arc::new(Cloudflare::new("zone", "token")), CdnOption::PROVIDER_CLOUDFLARE, false))
    .add_filter(|option: &CdnOption| !option.is_edge());

let cache = Cache::new(Balancer::new(options));
assert_eq!(cache.adapter().options().filtered().len(), 1);
// cache.purge_domain("example.com").await purges through Cloudflare only.

cache::cloudflare

Own page

Cloudflare's purge API.

use utopia_cdn::cache::cloudflare::Cloudflare;
Rust
pub struct Cloudflare<C = DefaultClient>

Purges a Cloudflare zone through its purge_cache endpoint: URLs, hostnames, cache tags, or everything.

Rust
use utopia_cdn::cache::{Adapter, Cloudflare};

let cloudflare = Cloudflare::new("zone-id", "api-token");
cloudflare.purge_keys(&["product-42".into()]).await?;
Compiles in CI; needs a running service to execute.

cache::fastly

Own page

Fastly's purge API.

use utopia_cdn::cache::fastly::Fastly;
Rust
pub struct Fastly<C = DefaultClient>

Purges a Fastly service: single URLs, surrogate keys, or everything.

Fastly cannot purge by host, so a domain is addressed by the surrogate key the origin attaches to every response it serves for it: the domain_key_prefix followed by the hostname (pass "" when the key is the bare hostname). Key, domain and zone purges need a service ID; without one they fail with Error::UnsupportedOperation, while URL purges still work.

Rust
use utopia_cdn::cache::{Adapter, Fastly};

let fastly = Fastly::new("api-token", "domain-").with_service_id("service-id").with_soft_purge(true);
fastly.purge_domain("example.com").await?; // purges the key `domain-example.com`
Compiles in CI; needs a running service to execute.