...
1 package stmtcache
2
3 import (
4 "math"
5
6 "github.com/jackc/pgx/v5/pgconn"
7 )
8
9
10 type UnlimitedCache struct {
11 m map[string]*pgconn.StatementDescription
12 invalidStmts []*pgconn.StatementDescription
13 }
14
15
16 func NewUnlimitedCache() *UnlimitedCache {
17 return &UnlimitedCache{
18 m: make(map[string]*pgconn.StatementDescription),
19 }
20 }
21
22
23 func (c *UnlimitedCache) Get(sql string) *pgconn.StatementDescription {
24 return c.m[sql]
25 }
26
27
28 func (c *UnlimitedCache) Put(sd *pgconn.StatementDescription) {
29 if sd.SQL == "" {
30 panic("cannot store statement description with empty SQL")
31 }
32
33 if _, present := c.m[sd.SQL]; present {
34 return
35 }
36
37 c.m[sd.SQL] = sd
38 }
39
40
41 func (c *UnlimitedCache) Invalidate(sql string) {
42 if sd, ok := c.m[sql]; ok {
43 delete(c.m, sql)
44 c.invalidStmts = append(c.invalidStmts, sd)
45 }
46 }
47
48
49 func (c *UnlimitedCache) InvalidateAll() {
50 for _, sd := range c.m {
51 c.invalidStmts = append(c.invalidStmts, sd)
52 }
53
54 c.m = make(map[string]*pgconn.StatementDescription)
55 }
56
57
58 func (c *UnlimitedCache) GetInvalidated() []*pgconn.StatementDescription {
59 return c.invalidStmts
60 }
61
62
63
64
65 func (c *UnlimitedCache) RemoveInvalidated() {
66 c.invalidStmts = nil
67 }
68
69
70 func (c *UnlimitedCache) Len() int {
71 return len(c.m)
72 }
73
74
75 func (c *UnlimitedCache) Cap() int {
76 return math.MaxInt
77 }
78
View as plain text