cdn

package
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cdn purges CDN cache entries by URL. Callers use only the Purger interface; New picks the provider: "cloudflare" or "none" (no purge, the cache expires by its TTL). It purges by URL only, not by tag or host name: those are plan features of some CDNs and have no common equivalent.

It depends on the standard library only.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v0.2.4

type Option func(*options)

Option configures New.

func WithOrigins added in v0.2.4

func WithOrigins(origins ...string) Option

WithOrigins also purges, for every URL, the copies cached for each of these request origins, e.g. "https://example.com". A CDN keys a cached object by its Origin header too, and a purge by URL alone clears only the copy cached without an Origin. Pass the origins that read the URLs cross-origin with fetch: the CORS origins of the bucket. A URL loaded by <img> or by a page navigation has no Origin and needs none.

Example

ExampleWithOrigins also purges the copies cached for each origin that reads the URLs with fetch. Every origin must be scheme://host[:port].

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/cdn"
)

func main() {
	_, err := cdn.New("cloudflare", "zone-id", "token",
		cdn.WithOrigins("https://example.com", "http://localhost:5173"))
	fmt.Println(err)

	_, err = cdn.New("cloudflare", "zone-id", "token", cdn.WithOrigins("https://example.com/"))
	fmt.Println(err)
}
Output:
<nil>
cdn: origin "https://example.com/" is not scheme://host[:port]

type Purger

type Purger interface {
	PurgeURLs(ctx context.Context, urls []string) error
}

Purger removes cached copies of URLs from the CDN.

Example

ExamplePurger uses a Purger behind an interface and treats a purge as best effort.

package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/phcp-tech/common-library-golang/cdn"
)

// recorder is a test Purger: callers depend on the interface only, so a unit
// test needs no CDN.
type recorder struct {
	urls []string
	err  error
}

func (r *recorder) PurgeURLs(_ context.Context, urls []string) error {
	r.urls = append(r.urls, urls...)
	return r.err
}

// publish changes data, then purges its URLs. A purge failure is only
// reported: the cache expires by its TTL, so the change itself succeeded.
func publish(ctx context.Context, p cdn.Purger, slug string) error {

	if err := p.PurgeURLs(ctx, []string{"https://example.com/articles/" + slug}); err != nil {
		fmt.Println("purge failed, the cache expires by its TTL:", err)
	}
	return nil
}

func main() {
	ctx := context.Background()

	ok := &recorder{}
	fmt.Println(publish(ctx, ok, "hello"), ok.urls)

	failing := &recorder{err: errors.New("HTTP 403")}
	fmt.Println(publish(ctx, failing, "hello"))
}
Output:
<nil> [https://example.com/articles/hello]
purge failed, the cache expires by its TTL: HTTP 403
<nil>

func New

func New(provider, zoneID, token string, opts ...Option) (Purger, error)

New returns the Purger of provider: "cloudflare" or "none".

Example

ExampleNew picks the purger from configuration. "none" purges nothing, for local runs without a token; "cloudflare" needs a zone id and a purge token.

package main

import (
	"context"
	"fmt"

	"github.com/phcp-tech/common-library-golang/cdn"
)

func main() {
	p, err := cdn.New("none", "", "")
	fmt.Println(err, p.PurgeURLs(context.Background(), []string{"https://example.com/a"}))

	_, err = cdn.New("cloudflare", "zone-id", "")
	fmt.Println(err)

	_, err = cdn.New("cloudfront", "zone-id", "token")
	fmt.Println(err)
}
Output:
<nil> <nil>
cdn: cloudflare needs a zone id and a purge token
cdn: unknown provider "cloudfront" (cloudflare|none)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL