Blame


1 1081cf75 2024-11-04 o package lemmy
2 1081cf75 2024-11-04 o
3 1081cf75 2024-11-04 o import (
4 1081cf75 2024-11-04 o "fmt"
5 1081cf75 2024-11-04 o "net/http"
6 1081cf75 2024-11-04 o "strconv"
7 1081cf75 2024-11-04 o "strings"
8 1081cf75 2024-11-04 o "sync"
9 1081cf75 2024-11-04 o "time"
10 1081cf75 2024-11-04 o )
11 1081cf75 2024-11-04 o
12 1081cf75 2024-11-04 o type cache struct {
13 1081cf75 2024-11-04 o post map[int]entry
14 1081cf75 2024-11-04 o community map[string]entry
15 1081cf75 2024-11-04 o mu *sync.Mutex
16 1081cf75 2024-11-04 o }
17 1081cf75 2024-11-04 o
18 1081cf75 2024-11-04 o type entry struct {
19 1081cf75 2024-11-04 o post Post
20 1081cf75 2024-11-04 o community Community
21 1081cf75 2024-11-04 o expiry time.Time
22 1081cf75 2024-11-04 o }
23 1081cf75 2024-11-04 o
24 1081cf75 2024-11-04 o func (c *cache) store(p Post, com Community, dur time.Duration) {
25 1081cf75 2024-11-04 o c.mu.Lock()
26 1081cf75 2024-11-04 o defer c.mu.Unlock()
27 1081cf75 2024-11-04 o t := time.Now().Add(dur)
28 1081cf75 2024-11-04 o entry := entry{expiry: t}
29 1081cf75 2024-11-04 o if p.Name() != "" {
30 1081cf75 2024-11-04 o entry.post = p
31 1081cf75 2024-11-04 o c.post[p.ID] = entry
32 1081cf75 2024-11-04 o }
33 1081cf75 2024-11-04 o if com.Name() != "" {
34 1081cf75 2024-11-04 o entry.community = com
35 1081cf75 2024-11-04 o c.community[com.Name()] = entry
36 1081cf75 2024-11-04 o }
37 1081cf75 2024-11-04 o }
38 1081cf75 2024-11-04 o
39 1081cf75 2024-11-04 o func (c *cache) delete(p Post, com Community) {
40 1081cf75 2024-11-04 o c.mu.Lock()
41 1081cf75 2024-11-04 o defer c.mu.Unlock()
42 1081cf75 2024-11-04 o delete(c.post, p.ID)
43 1081cf75 2024-11-04 o delete(c.community, com.Name())
44 1081cf75 2024-11-04 o }
45 1081cf75 2024-11-04 o
46 1081cf75 2024-11-04 o // max-age=50
47 1081cf75 2024-11-04 o func parseMaxAge(s string) (time.Duration, error) {
48 1081cf75 2024-11-04 o var want string
49 1081cf75 2024-11-04 o elems := strings.Split(s, ",")
50 1081cf75 2024-11-04 o for i := range elems {
51 1081cf75 2024-11-04 o elems[i] = strings.TrimSpace(elems[i])
52 1081cf75 2024-11-04 o if strings.HasPrefix(elems[i], "max-age") {
53 1081cf75 2024-11-04 o want = elems[i]
54 1081cf75 2024-11-04 o }
55 1081cf75 2024-11-04 o }
56 1081cf75 2024-11-04 o _, num, found := strings.Cut(want, "=")
57 1081cf75 2024-11-04 o if !found {
58 1081cf75 2024-11-04 o return 0, fmt.Errorf("missing = separator")
59 1081cf75 2024-11-04 o }
60 1081cf75 2024-11-04 o n, err := strconv.Atoi(num)
61 1081cf75 2024-11-04 o if err != nil {
62 1081cf75 2024-11-04 o return 0, fmt.Errorf("parse seconds: %w", err)
63 1081cf75 2024-11-04 o }
64 1081cf75 2024-11-04 o return time.Duration(n) * time.Second, nil
65 1081cf75 2024-11-04 o }
66 1081cf75 2024-11-04 o
67 1081cf75 2024-11-04 o // Cache-Control: public, max-age=50
68 1081cf75 2024-11-04 o func extractMaxAge(header http.Header) string {
69 1081cf75 2024-11-04 o cc := header.Get("Cache-Control")
70 1081cf75 2024-11-04 o if !strings.Contains(cc, "max-age=") {
71 1081cf75 2024-11-04 o return ""
72 1081cf75 2024-11-04 o }
73 1081cf75 2024-11-04 o return cc
74 1081cf75 2024-11-04 o }