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