repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / pgs
Eric Bower · 10 Dec 24

web_cache.go

 1package pgs
 2
 3import (
 4	"context"
 5	"fmt"
 6	"log/slog"
 7	"time"
 8
 9	"github.com/picosh/pico/shared"
10	"github.com/picosh/utils/pipe"
11)
12
13func getSurrogateKey(userName, projectName string) string {
14	return fmt.Sprintf("%s-%s", userName, projectName)
15}
16
17func createPubCacheDrain(ctx context.Context, logger *slog.Logger) *pipe.ReconnectReadWriteCloser {
18	info := shared.NewPicoPipeClient()
19	send := pipe.NewReconnectReadWriteCloser(
20		ctx,
21		logger,
22		info,
23		"pub to cache-drain",
24		"pub cache-drain -b=false",
25		100,
26		-1,
27	)
28	return send
29}
30
31func createSubCacheDrain(ctx context.Context, logger *slog.Logger) *pipe.ReconnectReadWriteCloser {
32	info := shared.NewPicoPipeClient()
33	send := pipe.NewReconnectReadWriteCloser(
34		ctx,
35		logger,
36		info,
37		"sub to cache-drain",
38		"sub cache-drain -k",
39		100,
40		-1,
41	)
42	return send
43}
44
45// purgeCache send a pipe pub to the pgs web instance which purges
46// cached entries for a given subdomain (like "fakeuser-www-proj"). We set a
47// "surrogate-key: <subdomain>" header on every pgs response which ensures all
48// cached assets for a given subdomain are grouped under a single key (which is
49// separate from the "GET-https-example.com-/path" key used for serving files
50// from the cache).
51func purgeCache(cfg *shared.ConfigSite, send *pipe.ReconnectReadWriteCloser, surrogate string) error {
52	cfg.Logger.Info("purging cache", "surrogate", surrogate)
53	time.Sleep(1 * time.Second)
54	_, err := send.Write([]byte(surrogate + "\n"))
55	return err
56}
57
58func purgeAllCache(cfg *shared.ConfigSite, send *pipe.ReconnectReadWriteCloser) error {
59	return purgeCache(cfg, send, "*")
60}